| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Retrieves the OpenAPI document for the specified Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function retrieves the OpenAPI document for the specified Kestrun server using the discovered components. |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance for which the OpenAPI document will be retrieved. |
| | | 8 | | If not specified, the function will attempt to resolve the current server context. |
| | | 9 | | .PARAMETER DocId |
| | | 10 | | The ID of the OpenAPI document to retrieve. Default is 'default'. |
| | | 11 | | .PARAMETER Version |
| | | 12 | | The OpenAPI specification version to use for retrieval. Default is OpenApi3_1. |
| | | 13 | | .PARAMETER Yaml |
| | | 14 | | If specified, the function will return the OpenAPI document in YAML format. |
| | | 15 | | .PARAMETER Json |
| | | 16 | | If specified, the function will return the OpenAPI document in JSON format. |
| | | 17 | | If neither Yaml nor Json is specified, the function will return the document as a PowerShell ordered hashtable. |
| | | 18 | | .EXAMPLE |
| | | 19 | | # Retrieve the OpenAPI document as a hashtable |
| | | 20 | | $openApiDoc = Get-KrOpenApiDocument -Server $myServer -DocId 'default' |
| | | 21 | | .EXAMPLE |
| | | 22 | | # Retrieve the OpenAPI document in JSON format |
| | | 23 | | $openApiJson = Get-KrOpenApiDocument -Server $myServer -Doc Id 'default' -Json |
| | | 24 | | .EXAMPLE |
| | | 25 | | # Retrieve the OpenAPI document in YAML format |
| | | 26 | | $openApiYaml = Get-KrOpenApiDocument -Server $myServer -DocId 'default' -Yaml |
| | | 27 | | .OUTPUTS |
| | | 28 | | [string] (if Yaml or Json is specified) |
| | | 29 | | [System.Management.Automation.OrderedHashtable] (if neither Yaml nor Json is specified) |
| | | 30 | | #> |
| | | 31 | | function Get-KrOpenApiDocument { |
| | | 32 | | [KestrunRuntimeApi('Everywhere')] |
| | | 33 | | [CmdletBinding(DefaultParameterSetName = 'HashTable')] |
| | | 34 | | [OutputType([string])] |
| | | 35 | | [OutputType([System.Management.Automation.OrderedHashtable])] |
| | | 36 | | param( |
| | | 37 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 38 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 39 | | |
| | | 40 | | [Parameter()] |
| | | 41 | | [string]$DocId = [Kestrun.Authentication.IOpenApiAuthenticationOptions]::DefaultSchemeName, |
| | | 42 | | |
| | | 43 | | [Parameter()] |
| | | 44 | | [Microsoft.OpenApi.OpenApiSpecVersion]$Version = [Microsoft.OpenApi.OpenApiSpecVersion]::OpenApi3_1, |
| | | 45 | | |
| | | 46 | | [Parameter(ParameterSetName = 'Yaml')] |
| | | 47 | | [switch]$Yaml, |
| | | 48 | | |
| | | 49 | | [Parameter(ParameterSetName = 'Json')] |
| | | 50 | | [switch]$Json |
| | | 51 | | ) |
| | | 52 | | begin { |
| | | 53 | | # Ensure the server instance is resolved |
| | 0 | 54 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 55 | | } |
| | | 56 | | process { |
| | 0 | 57 | | if ( -not $Server.OpenApiDocumentDescriptor.ContainsKey($DocId)) { |
| | 0 | 58 | | throw "OpenAPI document with ID '$DocId' does not exist on the server." |
| | | 59 | | } |
| | | 60 | | # Log the start of the validation process |
| | 0 | 61 | | Write-KrLog -Level Information -Message "Starting OpenAPI document retrieval for DocId: '{DocId}' Version: '{Ver |
| | | 62 | | # Retrieve the document descriptor |
| | 0 | 63 | | $doc = $Server.OpenApiDocumentDescriptor[$DocId] |
| | 0 | 64 | | if ( $Yaml.IsPresent) { |
| | 0 | 65 | | return $doc.ToYaml($Version) |
| | 0 | 66 | | } elseif ( $Json.IsPresent) { |
| | 0 | 67 | | return $doc.ToJson($Version) |
| | | 68 | | } |
| | 0 | 69 | | return $doc.ToJson($Version) | ConvertFrom-Json -AsHashtable |
| | | 70 | | } |
| | | 71 | | } |