Badgerati / Pode

Pode is a Cross-Platform PowerShell web framework for creating REST APIs, Web Sites, and TCP/SMTP servers
https://badgerati.github.io/Pode
MIT License
830 stars 92 forks source link

Add configuration parameter `Web.OpenApi.UsePodeYamlInternal` #1340

Closed mdaneri closed 2 months ago

mdaneri commented 2 months ago

Description

This pull request adds a new configuration parameter Web.OpenApi.UsePodeYamlInternal to the Pode module. This parameter forces the use of the internal YAML converter even if PSYaml or powershell-yaml modules are available. Additionally, it optimizes the YAML conversion process by caching the module import status.

Changes

New Code

<#
.SYNOPSIS
    creates a YAML description of the data in the object - based on https://github.com/Phil-Factor/PSYaml

.DESCRIPTION
    This produces YAML from any object you pass to it.

.PARAMETER Object
    The object that you want scripted out. This parameter accepts input via the pipeline.

.PARAMETER Depth
    The depth that you want your object scripted to

.EXAMPLE
    Get-PodeOpenApiDefinition|ConvertTo-PodeYaml
#>
function ConvertTo-PodeYaml {
    [CmdletBinding()]
    [OutputType([string])]
    param (
        [parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
        [AllowNull()]
        $InputObject,

        [parameter()]
        [int]
        $Depth = 16
    )

    begin {
        $pipelineObject = @()
    }

    process {
        $pipelineObject += $_
    }

    end {
        if ($pipelineObject.Count -gt 1) {
            $InputObject = $pipelineObject
        }

        if ($PodeContext.Server.Web.OpenApi.UsePodeYamlInternal) {
            return ConvertTo-PodeYamlInternal -InputObject $InputObject -Depth $Depth -NoNewLine
        }

        if ($null -eq $PodeContext.Server.InternalCache.YamlModuleImported) {
            $PodeContext.Server.InternalCache.YamlModuleImported = ((Test-PodeModuleInstalled -Name 'PSYaml') -or (Test-PodeModuleInstalled -Name 'powershell-yaml'))
        }

        if ($PodeContext.Server.InternalCache.YamlModuleImported) {
            return ($InputObject | ConvertTo-Yaml)
        }
        else {
            return ConvertTo-PodeYamlInternal -InputObject $InputObject -Depth $Depth -NoNewLine
        }
    }
}

Reason for Change

The addition of this configuration parameter allows users to force the use of the internal YAML converter, which can be necessary for portability or compatibility reasons.