SwaggerModule.psm1
function Get-ObjectMember { [CmdletBinding()] Param( [Parameter(Mandatory = $True, ValueFromPipeline = $True)] [PSCustomObject]$obj ) process { $obj | Get-Member -MemberType NoteProperty | ForEach-Object { $key = $_.Name [PSCustomObject]@{Key = $key; Value = $obj."$key" } } } } function Get-ParameterInfo { param ( # Parameter help description [Parameter(Mandatory=$true)] [PSCustomObject] $parameterObject ) $parameterInfo = "" try { switch ($parameterObject.Count) { 0 { break; } 1 { $parameterInfo = $parameterobject | Format-List Name, Type, Required, Description | Out-String break; } Default { $parameterInfo = ($parameterObject | format-table name, required, type, description -autosize | out-string ); break; } } } catch { Write-Verbose "Error retrieving parameterinfo (possible explanation: No parameters for the current path)" } return $parameterInfo.Trim() } function Get-RestURI{ [CmdletBinding(DefaultParameterSetName='File')] [OutputType([String])] param ( [Parameter(Mandatory=$true, ParameterSetName='File', Position=0)] [string] $filePath, # Parameter help description [Parameter(Mandatory=$true, ParameterSetName='Object')] [PSCustomObject] $swagger ) if ($filePath){ $swagger = Get-Content $filePath -Raw | ConvertFrom-Json } if ($swagger.servers.url){ return $swagger.servers.url; } if ($swagger.schemes -and $swagger.host -and $swagger.basePath){ return ("{0}://{1}{2}" -f $swagger.schemes[0], $swagger.host, $swagger.basePath) } return "Unknown" } function Get-SwaggerFile { [CmdletBinding()] Param( [Parameter(Mandatory = $True, ValueFromPipeline = $True)] $path ) process{ # TODO: File info (filnavn, størrelse, antal linjer/ord, datoer) if (-not (Test-Path $path -PathType Leaf)) { Write-Error "File not found ($path)"; return } try { $swagger = Get-Content -Path $path -Encoding UTF8 -Raw | ConvertFrom-Json -ErrorAction SilentlyContinue #$swagger = Get-Content -Path $path -Encoding UTF8 -Raw -erroraction Stop | ConvertFrom-Json } catch [System.ArgumentException] { Write-Error "Invalid JSON file: $path ($_)" return } catch { Write-Error "Unknown error: $($_.Exception.GetType().FullName)" Write-Error $_.Exception.Message return } $fileInfo = Get-ChildItem $path $restURI = Get-RestURI -swagger $swagger # if ($swagger.openapi){ # $restURI = $swagger.servers.url # } else { # $restURI = ("{0}://{1}{2}" -f $swagger.schemes[0], $swagger.host, $swagger.basePath) # } $security = switch ($swagger) { {$_.components.securitySchemes.'x-api-key'} { "x-api-key"; break } {$_.securityDefinitions.'x-api-key'} { "x-api-key"; break } {$_.securityDefinitions.custom_scheme} {$_.securityDefinitions.custom_scheme.type; break} Default {"unknown"} } $paths = Get-SwaggerPath -swagger $swagger $serviceInfo = [ordered]@{ Title = $swagger.info.title Version = $swagger.info.version BaseURL = $restURI Security = $security Paths = ($paths | Select-Object path, @{N='parameters';E={$_.parameters -join ","}}) PathsExpanded = ($paths | Format-List | Out-String) SwaggerFile = $fileInfo.FullName SwaggerDate = $fileInfo.LastWriteTime SwaggerLength = $fileInfo.Length } return New-Object -TypeName psobject -Property $serviceInfo # Output base info # $serviceInfo # | select Title, Version, BaseURL, Security } } function Get-SwaggerPath{ [CmdletBinding(DefaultParameterSetName='File')] # [OutputType([String])] param ( [Parameter(Mandatory=$true, ParameterSetName='File', Position=0)] [string] $filePath, # Parameter help description [Parameter(Mandatory=$true, ParameterSetName='Object')] [PSCustomObject] $swagger ) if ($filePath){ $swagger = Get-Content $filePath -Raw | ConvertFrom-Json } if (-not $swagger.paths){ Write-Verbose "No paths found" return } $restURI = Get-RestURI -swagger $swagger $swagger.paths | Get-ObjectMember | ForEach-Object { # For every path $path = $_.Key # $methodJSON = $_.Value | Get-ObjectMember | ForEach-Object { $parameterInfo = ""; if ($_.value.parameters) { $parameterInfo = Get-ParameterInfo ($_.value.parameters); } [pscustomobject]@{ path = $path fullPath = $restURI+$path method = $_.Key parameters = $_.Value.parameters.name parameterCount = $_.Value.parameters.count parameterInfo = $parameterInfo } } } } Export-ModuleMember -Function Get-SwaggerFile |