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] Can't pass data as a json for array of object in typescript-axios generator #16659

Closed
5 of 6 tasks
Kavan72 opened this issue Sep 25, 2023 · 3 comments
Closed
5 of 6 tasks

Comments

@Kavan72
Copy link
Contributor

Kavan72 commented Sep 25, 2023

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

Cannot pass a data-type object as an array of items in a form using Axios. It works perfectly for a normal object, but it's not working as expected for a form.

openapi-generator version

7.0.1

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Backend
  version: 1.0.0
paths:
  /company:
    put:
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/UpdateCompanyDetails'
            encoding:
              industry:
                contentType: application/json
      responses:
        '204':
          description: The company data successfully updated

components:
  schemas:
    UpdateCompanyDetails:
      type: object
      properties:
        name:
          type: string
          description: the name of the company
        industry:
          type: array
          items:
            $ref: '#/components/schemas/Industry'
        currency:
          $ref: '#/components/schemas/Currency'
    Industry:
      type: object
      properties:
        id:
          type: integer
          description: The unique identifier of the industry
        name:
          type: string
          description: The name of the industry
    Currency:
      type: object
      properties:
        id:
          type: integer
          description: the unique identifier of the currency
        name:
          type: string
          description: the long name of the currency (i.e. US Dollars)
Suggest a fix
      {{#formParams}}
        {{#isArray}}
            if ({{paramName}}) {
          {{#isCollectionFormatMulti}}
            {{#contentType}}
                localVarFormParams.append('{{baseName}}', new Blob([JSON.stringify({{paramName}})], { type: "{{contentType}}", }));
            {{/contentType}}
            {{^contentType}}
              {{paramName}}.forEach((element) => {
                localVarFormParams.{{#multipartFormData}}append{{/multipartFormData}}{{^multipartFormData}}set{{/multipartFormData}}('{{baseName}}', element as any);
                })
            {{/contentType}}
          {{/isCollectionFormatMulti}}
          {{^isCollectionFormatMulti}}
              localVarFormParams.{{#multipartFormData}}append{{/multipartFormData}}{{^multipartFormData}}set{{/multipartFormData}}('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS.{{collectionFormat}}));
          {{/isCollectionFormatMulti}}
            }
        {{/isArray}}
        {{^isArray}}
            if ({{paramName}} !== undefined) {
          {{^multipartFormData}}
              localVarFormParams.set('{{baseName}}', {{paramName}} as any);
          {{/multipartFormData}}
          {{#multipartFormData}}
            {{#isPrimitiveType}}
                localVarFormParams.append('{{baseName}}', {{paramName}} as any);
            {{/isPrimitiveType}}
            {{^isPrimitiveType}}
              {{#isEnumRef}}
                  localVarFormParams.append('{{baseName}}', {{paramName}} as any);
              {{/isEnumRef}}
              {{^isEnumRef}}
                  localVarFormParams.append('{{baseName}}', new Blob([JSON.stringify({{paramName}})], { type: "application/json", }));
              {{/isEnumRef}}
            {{/isPrimitiveType}}
          {{/multipartFormData}}
            }
        {{/isArray}}
      {{/formParams}}

Currently, I'm using a custom template to check if any 'contentType' is defined, and if so, pass it as a blob and send it like this.

localVarFormParams.append('industry', new Blob([JSON.stringify(industry)], { type: 'application/json' }))
Actual output vs Expected output

Actual output

                industry.forEach((element) => {
                    localVarFormParams.append('industry', element as any);
                })

Expected output

                localVarFormParams.append('industry', new Blob([JSON.stringify(industry)], { type: 'application/json' }))
@Kavan72
Copy link
Contributor Author

Kavan72 commented Oct 5, 2023

@borsch can you help on this issue, i'm happy to contribute in this issue

@Ross-H-Projects
Copy link

Also observing this bug. Would be great to get multipart working like this as I suspect it will also fix the issue where an array of files cannot be used in a multipart schema def

@przem
Copy link

przem commented Nov 2, 2023

+1

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

3 participants