Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Powershell] Array context not maintained when converting a parameter to JSON #18427

Open
3 of 6 tasks
condorcorde opened this issue Apr 18, 2024 · 0 comments
Open
3 of 6 tasks

Comments

@condorcorde
Copy link
Contributor

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Consider the function Invoke-PS123TestSpecialTags of PSPetstore. It has a parameter $Client of type [PSCustomObject]:

        [Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
        [PSCustomObject]
        ${Client},

which is correctly translated into JSON by the line

$LocalVarBodyParameter = $Client | ConvertTo-Json -Depth 100

Now suppose that $Client is defined as being an array [PSCustomObject[]]. The conversion to JSON is done in the same way, but the JSON output changes depending on whether $Client is an array of one or multiple elements, because the parameter type declaration does not force an array context when the array contains a single element.

openapi-generator version

Up to 7.5

OpenAPI declaration file content or url

The issue was first noticed using a PowerShell client generated from Connectwise PSA API specs. The specs are too complex (over 4K endpoints) to be used for an example.

Generation Details

openapi-generator generate --input-spec SPECS/2022.2/All.json --generator-name powershell --output PSClient --additional-properties=packageName=CWPSA,packageVersion=2022.2,apiNamePrefix=PSA --skip-validate-spec

Steps to reproduce

Reproducing the issue requires changing AnotherFakeApi specs, but the same behaviour can be seen from the console:

PS /> $Client = [PSCustomObject]@{client = 'X'}
# Array of two elements
PS /> $c = @($Client, $Client)                                                                                                                          
PS /> $c | ConvertTo-Json
[
  {
    "client": "X"
  },
  {
    "client": "X"
  }
]
# Array of one element
PS /> $c = @($Client)
PS /> $c | ConvertTo-Json
{
  "client": "X"
}
# Array of one element forcing array context
PS /> ,$c | ConvertTo-Json
[
  {
    "client": "X"
  }
]
Related issues/PRs

None found

Suggest a fix

The optimal solution would be to force an array context when the parameter is declared as being an array, as in:

$LocalVarBodyParameter = ,$Client | ConvertTo-Json -Depth 100

I would not know what changes to the mustache files are required to obtain this result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant