| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds an OpenAPI component (Example or Link) to the specified OpenAPI document(s). |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This cmdlet adds an OpenAPI component, either an Example or a Link, to one or more OpenAPI documents managed by the |
| | | 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 component will be added. Defaults to the standard documentation IDs. |
| | | 10 | | .PARAMETER Name |
| | | 11 | | The name of the component to be added. This can be provided via the pipeline or as a parameter. |
| | | 12 | | .PARAMETER Component |
| | | 13 | | The OpenAPI component object to be added. This can be an OpenApiExample or OpenApiLink object. |
| | | 14 | | .PARAMETER IfExists |
| | | 15 | | Specifies the conflict resolution strategy if a component 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-KrOpenApiComponent -Name "UserExample" -Component $example -DocId "MyApiDoc" |
| | | 19 | | This example creates a new OpenAPI Component 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-KrOpenApiComponent -Name "GetUserLink" -Component $link -DocId "MyApiDoc" |
| | | 23 | | This example creates a new OpenAPI Link and adds it to the "MyApiDoc" OpenAPI document. |
| | | 24 | | .EXAMPLE |
| | | 25 | | New-KrOpenApiExample -Summary "Product Example" -Value @{ id = 101; name = "Widget"; price = 9.99 } | Add-KrOpenApiC |
| | | 26 | | This example creates a new OpenAPI Component Example for a product and pipes it directly to the Add-KrOpenApiCompone |
| | | 27 | | .EXAMPLE |
| | | 28 | | New-KrOpenApiLink -OperationId "getOrder" -Description "Link to get order details" -Parameters @{ "orderId" = "$resp |
| | | 29 | | This example creates a new OpenAPI Link for getting order details and pipes it directly to the Add-KrOpenApiComponen |
| | | 30 | | .OUTPUTS |
| | | 31 | | None |
| | | 32 | | #> |
| | | 33 | | function Add-KrOpenApiComponent { |
| | | 34 | | [KestrunRuntimeApi('Definition')] |
| | | 35 | | [CmdletBinding()] |
| | | 36 | | param( |
| | | 37 | | [Parameter(Mandatory = $false )] |
| | | 38 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 39 | | |
| | | 40 | | [Parameter()] |
| | | 41 | | [string[]]$DocId = [Kestrun.OpenApi.OpenApiDocDescriptor]::DefaultDocumentationIds, |
| | | 42 | | |
| | | 43 | | [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] |
| | | 44 | | [Alias('Key', 'Id')] |
| | | 45 | | [ValidatePattern('^[A-Za-z0-9\.\-_]+$')] |
| | | 46 | | [string] $Name, |
| | | 47 | | |
| | | 48 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] |
| | | 49 | | [object] $Component, |
| | | 50 | | |
| | | 51 | | [Parameter()] |
| | | 52 | | [Kestrun.OpenApi.OpenApiComponentConflictResolution] $IfExists = [Kestrun.OpenApi.OpenApiComponentConflictResolu |
| | | 53 | | ) |
| | | 54 | | begin { |
| | | 55 | | # Ensure the server instance is resolved |
| | 0 | 56 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 57 | | } |
| | | 58 | | process { |
| | 0 | 59 | | if ($Server.IsConfigured) { |
| | 0 | 60 | | Write-KrError -ErrorMessage 'The Kestrun server configuration is already set. Please add components before e |
| | | 61 | | return |
| | | 62 | | } |
| | | 63 | | # Add the server to the specified OpenAPI documents |
| | 0 | 64 | | foreach ($doc in $DocId) { |
| | 0 | 65 | | $docDescriptor = $Server.GetOrCreateOpenApiDocument($doc) |
| | | 66 | | |
| | 0 | 67 | | if ($Component -is [Microsoft.OpenApi.OpenApiExample]) { |
| | 0 | 68 | | $docDescriptor.AddComponentExample($Name, $Component, $IfExists) |
| | 0 | 69 | | } elseif ($Component -is [Microsoft.OpenApi.OpenApiLink]) { |
| | 0 | 70 | | $docDescriptor.AddComponentLink($Name, $Component, $IfExists) |
| | 0 | 71 | | } elseif ($Component -is [Microsoft.OpenApi.OpenApiHeader]) { |
| | 0 | 72 | | $docDescriptor.AddComponentHeader($Name, $Component, $IfExists) |
| | | 73 | | } else { |
| | 0 | 74 | | Write-KrError -ErrorMessage "OpenApi $($doc): Unsupported component type: $($Component.GetType().FullNam |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |