OpenAPITools / OpenAPITools/openapi-generator

[BUG][PowerShell] Parameter of type array passed in pipeline: only first element is processed

Open
#19,663 3 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

The issue affects functions New-PSUsersWithArrayInput and New-PSUsersWithListInput in PSPetStore API.
The parameter $User has type PsCustomObject[] and has the attribute ValueFromPipeline = $true.
The body of the function consists of a single Process {} block that ends with a return statement.

In PowerShell the Process {} block is executed once for each input object that reaches the function, so the actual behaviour is the serialization the first element, the execution of Invoke-PSApiClient and a return to the caller.

openapi-generator version
$  openapi-generator --version
openapi-generator-cli 7.8.0
  commit : 6bdc452
  built  : -999999999-01-01T00:00:00+18:00
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url

user.yml

Generation Details

Standard generation of PSPetStore client library

Steps to reproduce
Import-Module -Name PSPetstore
$a = Initialize-PSUser -Id 1 -Username foo
$b = Initialize-PSUser -Id 2 -Username bar
$c = @($a, $b)
$c.GetType()
$DebugPreference = 'Continue'
$c | New-PSUsersWithArrayInput

Both New-PSUsersWithArrayInput and Invoke-PSApiClient show a single object being processed (*):

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

DEBUG: Calling method: New-PSUsersWithArrayInput
DEBUG: Parameter Value
DEBUG: --------- -----
DEBUG: User      @{id=1; username=foo; firstName=; lastName=; email=; password=; phone=; userStatus=;
DEBUG:           objectWithNoDeclaredProps=; objectWithNoDeclaredPropsNullable=; anyTypeProp=; anyTypePropNullable=}
DEBUG: Calling method: Invoke-PSApiClient
DEBUG: Parameter        Value
DEBUG: ---------        -----
DEBUG: Method           POST
DEBUG: Uri              /user/createWithArray
DEBUG: Accepts
DEBUG: ContentTypes     application/json
DEBUG: Body             [
DEBUG:                    {
DEBUG:                      "id": 1,
DEBUG:                      "username": "foo",
DEBUG:                      "firstName": "",
DEBUG:                      "lastName": "",
DEBUG:                      "email": "",
DEBUG:                      "password": "",
DEBUG:                      "phone": "",
DEBUG:                      "userStatus": null,
DEBUG:                      "objectWithNoDeclaredProps": null,
DEBUG:                      "objectWithNoDeclaredPropsNullable": null,
DEBUG:                      "anyTypeProp": null,
DEBUG:                      "anyTypePropNullable": null
DEBUG:                    }
DEBUG:                  ]
DEBUG: HeaderParameters {}
DEBUG: QueryParameters  {}
DEBUG: FormParameters   {}
DEBUG: CookieParameters {}
DEBUG: ReturnType
DEBUG: IsBodyNullable   False

(*) The function Out-DebugParameter was modified by adding the parameter -Wrap to Format-Table in order to show the full body.

Related issues/PRs

Array context not maintained when converting a parameter to JSON #18427

Suggest a fix

The main product of the function produced by api.mustache is a JSON serialization of the input, and this requires the whole input object to be passed at once to ConvertTo-JSON. Actually, in PowerShell 5 the behavior of ConvertTo-JSON is such that the output may not be correct when the input object is passed from the pipeline.

IMHO there are two options:

  1. Remove the ValueFromPipeline attribute when the main parameter is an array type.
  2. Use the Process {}block to reconstruct the input array, and pass this variable to ConvertTo-Json in the End {}block of the function. This option shall ensure that the serialization of the reconstructed collection is the same as the original one.

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

Start with the generated PowerShell functions New-PSUsersWithArrayInput and New-PSUsersWithListInput and the api.mustache template, using user.yml and the reproduction commands to observe pipeline processing. Check how the Process block and return statement interact with Invoke-PSApiClient. Done means array input is serialized and sent as a complete collection rather than only its first element.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.