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

[BUG][Typescript][Angular] Wrong generated code for array of items using FormData (multipart/form-data)

Open jeandonaldroselin opened this issue 1 year ago • 0 comments

Bug Report Checklist

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

I use open api spec 3 to define api to define two attributes :

  • mediasToAdd which is an array of files to upload

  • mediasToDelete which is an array of files uuids to delete


But Angular Typescript generators generates a code that does not build properly theses array attributes.

This is the generated code :

Capture d’écran 2024-04-24 à 18 05 11

But it should be :

Capture d’écran 2024-04-24 à 18 05 31

-> If I use the generated method to upsert my medias, I can observe this in the chrome developers network section

Capture d’écran 2024-04-24 à 18 13 54

There are multiple formData attributes with the same name which are sent,

the consequence is that on API side theses values are received as simple attributes instead as arrays.


Following the previous screenshot the API will receive :

  • mediasToAdd as a single binary (latest value provided)

  • mediasToDelete as a single string (latest value provided)

openapi-generator version

I use exactly @openapitools/[email protected]

OpenAPI declaration file content or url

Capture d’écran 2024-04-24 à 17 54 45

Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

The problem is located in this file => https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache

The attributes are appended as simple attributes instead as array :

        {{#isArray}}
        if ({{paramName}}) {
        {{#isCollectionFormatMulti}}
            {{paramName}}.forEach((element) => {
                localVarFormParams = localVarFormParams.append('{{baseName}}', <any>element) as any || localVarFormParams;
            })
        {{/isCollectionFormatMulti}}
        {{^isCollectionFormatMulti}}
            if (localVarUseForm) {
                {{paramName}}.forEach((element) => {
                    localVarFormParams = localVarFormParams.append('{{baseName}}', <any>element) as any || localVarFormParams;
            })
            } else {
                localVarFormParams = localVarFormParams.append('{{baseName}}', [...{{paramName}}].join(COLLECTION_FORMATS['{{collectionFormat}}'])) as any || localVarFormParams;
            }
        {{/isCollectionFormatMulti}}
        }
        {{/isArray}}

But it should be this

        {{#isArray}}
        if ({{paramName}}) {
        {{#isCollectionFormatMulti}}
            {{paramName}}.forEach((element) => {
                localVarFormParams = localVarFormParams.append('{{baseName}}[]', <any>element) as any || localVarFormParams;
            })
        {{/isCollectionFormatMulti}}
        {{^isCollectionFormatMulti}}
            if (localVarUseForm) {
                {{paramName}}.forEach((element) => {
                    localVarFormParams = localVarFormParams.append('{{baseName}}[]', <any>element) as any || localVarFormParams;
            })
            } else {
                localVarFormParams = localVarFormParams.append('{{baseName}}[]', [...{{paramName}}].join(COLLECTION_FORMATS['{{collectionFormat}}'])) as any || localVarFormParams;
            }
        {{/isCollectionFormatMulti}}
        }
        {{/isArray}}

jeandonaldroselin avatar Apr 24 '24 16:04 jeandonaldroselin