OpenAPITools / OpenAPITools/openapi-generator

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

Open
#18,427 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior with the PowerShell console examples and the generated AnotherFakeApi client. Inspect the PowerShell generator's mustache files and the code producing $LocalVarBodyParameter. Done means array-typed parameters remain JSON arrays even when they contain one element, with the relevant generator tests passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.