| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds an inline OpenAPI element (Example or Link) to the specified OpenAPI document(s). |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This cmdlet adds an inline OpenAPI element, either an Example or a Link, to one or more OpenAPI documents managed by |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance where the OpenAPI documents are managed. If not specified, the default server instance i |
| | | 8 | | .PARAMETER DocId |
| | | 9 | | An array of OpenAPI document IDs to which the element will be added. Defaults to the standard documentation IDs. |
| | | 10 | | .PARAMETER Name |
| | | 11 | | The name of the inline element to be added. This can be provided via the pipeline or as a parameter. |
| | | 12 | | .PARAMETER Element |
| | | 13 | | The OpenAPI inline element object to be added. This can be an OpenApiExample or OpenApiLink object. |
| | | 14 | | .PARAMETER IfExists |
| | | 15 | | Specifies the conflict resolution strategy if an element with the same name already exists in the document. Options |
| | | 16 | | .EXAMPLE |
| | | 17 | | $example = New-KrOpenApiExample -Summary "User Example" -Description "An example of a user object." -Value @{ id = 1 |
| | | 18 | | Add-KrOpenApiInline -Name "UserExample" -Element $example -DocId "MyApiDoc" |
| | | 19 | | This example creates a new OpenAPI Inline Example and adds it to the "MyApiDoc" OpenAPI document. |
| | | 20 | | .EXAMPLE |
| | | 21 | | $link = New-KrOpenApiLink -OperationId "getUser" -Description "Link to get user details" -Parameters @{ "userId" = " |
| | | 22 | | Add-KrOpenApiInline -Name "GetUserLink" -Element $link -DocId "MyApiDoc" |
| | | 23 | | This example creates a new OpenAPI Inline Link and adds it to the "MyApiDoc" OpenAPI document. |
| | | 24 | | #> |
| | | 25 | | function Add-KrOpenApiInline { |
| | | 26 | | [KestrunRuntimeApi('Definition')] |
| | | 27 | | [CmdletBinding()] |
| | | 28 | | param( |
| | | 29 | | [Parameter(Mandatory = $false )] |
| | | 30 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 31 | | |
| | | 32 | | [Parameter()] |
| | | 33 | | [string[]]$DocId = [Kestrun.OpenApi.OpenApiDocDescriptor]::DefaultDocumentationIds, |
| | | 34 | | |
| | | 35 | | [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] |
| | | 36 | | [Alias('Key', 'Id')] |
| | | 37 | | [ValidatePattern('^[A-Za-z0-9\.\-_]+$')] |
| | | 38 | | [string] $Name, |
| | | 39 | | |
| | | 40 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] |
| | | 41 | | [object] $Element, |
| | | 42 | | |
| | | 43 | | [Parameter()] |
| | | 44 | | [Kestrun.OpenApi.OpenApiComponentConflictResolution] $IfExists = [Kestrun.OpenApi.OpenApiComponentConflictResolu |
| | | 45 | | ) |
| | | 46 | | begin { |
| | | 47 | | # Ensure the server instance is resolved |
| | 0 | 48 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 49 | | } |
| | | 50 | | process { |
| | 0 | 51 | | if ($Server.IsConfigured) { |
| | 0 | 52 | | Write-KrError -ErrorMessage 'The Kestrun server configuration is already set. Please add components before e |
| | | 53 | | return |
| | | 54 | | } |
| | | 55 | | # Add the server to the specified OpenAPI documents |
| | 0 | 56 | | foreach ($doc in $DocId) { |
| | 0 | 57 | | $docDescriptor = $Server.GetOrCreateOpenApiDocument($doc) |
| | | 58 | | |
| | 0 | 59 | | if ($Element -is [Microsoft.OpenApi.OpenApiExample]) { |
| | 0 | 60 | | $docDescriptor.AddInlineExample($Name, $Element, $IfExists) |
| | 0 | 61 | | } elseif ($Element -is [Microsoft.OpenApi.OpenApiLink]) { |
| | 0 | 62 | | $docDescriptor.AddInlineLink($Name, $Element, $IfExists) |
| | | 63 | | } else { |
| | 0 | 64 | | throw [System.ArgumentException]::new( |
| | 0 | 65 | | "Unsupported inline element type: $($Element.GetType().FullName). Supported: OpenApiExample, OpenApi |
| | | 66 | | 'Element' |
| | | 67 | | ) |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |