| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Exports the OpenAPI document for the specified Kestrun server in the desired format. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function exports the OpenAPI document for the specified Kestrun server in either JSON or YAML format. |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance for which the OpenAPI document will be exported. |
| | | 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 export. Default is 'default'. |
| | | 11 | | .PARAMETER Format |
| | | 12 | | The format in which to export the OpenAPI document. Valid values are 'Json' and 'Yaml'. Default is 'Json'. |
| | | 13 | | .PARAMETER Version |
| | | 14 | | The OpenAPI specification version to use for the export. Default is OpenApi3_1. |
| | | 15 | | .EXAMPLE |
| | | 16 | | # Export the OpenAPI document for the default document ID in JSON format |
| | | 17 | | $openApiJson = Export-KrGenerateOpenApiDocument -Server $myServer -DocId 'default' -Format 'Json' |
| | | 18 | | .OUTPUTS |
| | | 19 | | string representing the OpenAPI document in the specified format. |
| | | 20 | | This will be the JSON or YAML representation of the OpenAPI document. |
| | | 21 | | #> |
| | | 22 | | function Export-KrOpenApiDocument { |
| | | 23 | | [KestrunRuntimeApi('Everywhere')] |
| | | 24 | | [CmdletBinding()] |
| | | 25 | | param( |
| | | 26 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 27 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 28 | | [Parameter()] |
| | | 29 | | [string]$DocId = [Kestrun.Authentication.IOpenApiAuthenticationOptions]::DefaultSchemeName, |
| | | 30 | | [Parameter(Mandatory = $false)] |
| | | 31 | | [ValidateSet('Json', 'Yaml')] |
| | | 32 | | [string]$Format = 'Json', |
| | | 33 | | [Parameter(Mandatory = $false)] |
| | | 34 | | [Microsoft.OpenApi.OpenApiSpecVersion]$Version = [Microsoft.OpenApi.OpenApiSpecVersion]::OpenApi3_1 |
| | | 35 | | ) |
| | | 36 | | begin { |
| | | 37 | | # Ensure the server instance is resolved |
| | 0 | 38 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 39 | | } |
| | | 40 | | process { |
| | 0 | 41 | | if ( -not $Server.OpenApiDocumentDescriptor.ContainsKey($DocId)) { |
| | 0 | 42 | | throw "OpenAPI document with ID '$DocId' does not exist on the server." |
| | | 43 | | } |
| | 0 | 44 | | if ($Format -eq 'Json') { |
| | 0 | 45 | | return $Server.OpenApiDocumentDescriptor[$DocId].ToJson($Version) |
| | | 46 | | } else { |
| | 0 | 47 | | return $Server.OpenApiDocumentDescriptor[$DocId].ToYaml($Version) |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | } |