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
865 stars 92 forks source link

Fix OpenAPI Route Path Conversion for Placeholder Unescaping and Relocate Function (#1422) #1423

Closed mdaneri closed 1 month ago

mdaneri commented 1 month ago

Description:

This pull request addresses issue #1422 regarding the handling of route placeholders for OpenAPI in Pode. The main issue was the improper unescaping of route placeholders, which affected OpenAPI route path generation.

Code Changes:

  1. Function Rename:
    The function ConvertTo-PodeOpenApiRoutePath has been renamed to ConvertTo-PodeOARoutePath for consistency with Pode's OpenAPI helper naming conventions.

  2. Placeholder Unescaping:
    Placeholders (e.g., :potato) are now correctly unescaped using [regex]::Unescape to handle special characters properly in OpenAPI paths.

  3. Function Relocation:
    The ConvertTo-PodeOARoutePath function has been moved from ./Private/Route.ps1 to ./Private/OpenApi.ps1 to better align with its intended purpose and OpenAPI-specific functionality.

  4. New Pester Test:
    A new Pester 5.5 test has been added to validate the function's behavior. This test ensures that the function correctly converts routes like '/v4.2/:potato' and '/:potato' into OpenAPI-compliant formats (e.g., '/v4.2/{potato}' and '/{potato}').

Previous Implementation:

function ConvertTo-PodeOpenApiRoutePath {
    param(
        [Parameter(Mandatory = $true)]
        [string]
        $Path
    )

    return (Resolve-PodePlaceholder -Path $Path -Pattern '\:(?<tag>[\w]+)' -Prepend '{' -Append '}')
}

Updated Implementation:

function ConvertTo-PodeOARoutePath {
    param(
        [Parameter(Mandatory = $true)]
        [string]
        $Path
    )

    return ([regex]::Unescape((Resolve-PodePlaceholder -Path $Path -Pattern '\:(?<tag>[\w]+)' -Prepend '{' -Append '}')))
}

Testing: