| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds a tag to the OpenAPI document. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function adds a tag to the OpenAPI document using the provided parameters in the specified OpenAPI documents in |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance to which the OpenAPI tag will be added. |
| | | 8 | | If not specified, the function will attempt to resolve the current server context. |
| | | 9 | | .PARAMETER DocId |
| | | 10 | | An array of OpenAPI document IDs to which the tag will be added. Default is 'default'. |
| | | 11 | | .PARAMETER Name |
| | | 12 | | The name of the tag. |
| | | 13 | | .PARAMETER Description |
| | | 14 | | A description of the tag. |
| | | 15 | | .PARAMETER ExternalDocs |
| | | 16 | | An OpenAPI External Documentation object associated with the tag. |
| | | 17 | | .EXAMPLE |
| | | 18 | | # Add a tag to the default document |
| | | 19 | | Add-KrOpenApiTag -Name 'MyTag' -Description 'This is my tag.' ` |
| | | 20 | | -ExternalDocs (New-KrOpenApiExternalDoc -Description 'More info' -Url 'https://example.com/tag-info') |
| | | 21 | | .NOTES |
| | | 22 | | This cmdlet is part of the OpenAPI module. |
| | | 23 | | #> |
| | | 24 | | function Add-KrOpenApiTag { |
| | | 25 | | [KestrunRuntimeApi('Everywhere')] |
| | | 26 | | param( |
| | | 27 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 28 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 29 | | [Parameter()] |
| | | 30 | | [string[]]$DocId = [Kestrun.Authentication.IOpenApiAuthenticationOptions]::DefaultDocumentationIds, |
| | | 31 | | [Parameter(Mandatory = $true)] |
| | | 32 | | [string]$Name, |
| | | 33 | | [Parameter()] |
| | | 34 | | [string]$Description, |
| | | 35 | | [Parameter()] |
| | | 36 | | [Microsoft.OpenApi.OpenApiExternalDocs]$ExternalDocs |
| | | 37 | | ) |
| | | 38 | | begin { |
| | | 39 | | # Ensure the server instance is resolved |
| | 0 | 40 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 41 | | } |
| | | 42 | | process { |
| | | 43 | | # Add the server to the specified OpenAPI documents |
| | 0 | 44 | | foreach ($doc in $DocId) { |
| | 0 | 45 | | $docDescriptor = $Server.GetOrCreateOpenApiDocument($doc) |
| | 0 | 46 | | if ($null -eq $docDescriptor.Document.Tags) { |
| | | 47 | | # Initialize the Tags collection if null |
| | 0 | 48 | | $docDescriptor.Document.Tags = [System.Collections.Generic.HashSet[Microsoft.OpenApi.OpenApiTag]]::new() |
| | | 49 | | } |
| | 0 | 50 | | $tag = [Microsoft.OpenApi.OpenApiTag]::new() |
| | 0 | 51 | | $tag.Name = $Name |
| | 0 | 52 | | if ($PsBoundParameters.ContainsKey('Description')) { |
| | 0 | 53 | | $tag.Description = $Description |
| | | 54 | | } |
| | 0 | 55 | | if ($PsBoundParameters.ContainsKey('ExternalDocs')) { |
| | 0 | 56 | | $tag.ExternalDocs = $ExternalDocs |
| | | 57 | | } |
| | 0 | 58 | | $docDescriptor.Document.Tags.Add($tag) | Out-Null |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | } |