openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] array parameters in FormData should not be join to string by csv

Open rainmanhhh opened this issue 1 year ago • 0 comments

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

api templates use wrong way to deal with array parameters in form body. for example: line 165-177 at typescript-rxjs/apis.mustache:

        const formData = new FormData();
        {{#formParams}}
        {{#isArray}}
        if ({{> paramNamePartial}} !== undefined) {
            {{#isCollectionFormatMulti}}
            {{> paramNamePartial}}.forEach((element) => formData.append('{{baseName}}', element as any))
            {{/isCollectionFormatMulti}}
            {{^isCollectionFormatMulti}}
            formData.append('{{baseName}}', {{> paramNamePartial}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
            {{/isCollectionFormatMulti}}
        }

        {{/isArray}}

only if isCollectionFormatMulti is true, the array parameter will be add to FormData as an array, otherwise it will be join to a string. according to DefaultCodegen::getCollectionFormat, isCollectionFormatMulti is true when style=form and explode=true, BUT "style" and "explode" are not valid for request body parameters. ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#style-values style and explode are for Parameter Object which define parameters in path, query, header or cookie, so you can never set style=form, explode=true for an array parameter in request body(FormData). manually add them will cause a "Schema validation: Property 'style' is not allowed" warning at ide.

openapi-generator version

checked 5.0.0, 6.0.0 and 7.5.0, I think the problem never been fixed at any version

OpenAPI declaration file content or url
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

remove isCollectionFormatMulti checking code for array parameters in FormData, like this:

        const formData = new FormData();
        {{#formParams}}
        {{#isArray}}
        if ({{> paramNamePartial}} !== undefined) {
            {{> paramNamePartial}}.forEach((element) => formData.append('{{baseName}}', element as any))
        }

        {{/isArray}}

rainmanhhh avatar Apr 26 '24 07:04 rainmanhhh