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] Angular service multipart not supported #547

Closed
emanuelegaleotti-quix opened this issue Nov 17, 2021 · 3 comments
Closed

[BUG] Angular service multipart not supported #547

emanuelegaleotti-quix opened this issue Nov 17, 2021 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@emanuelegaleotti-quix
Copy link

🐛 Bug Report:

Describe the bug

When an Angular service is generated with a method that requires a multipart / formData the result is incorrect

Steps to Reproduce

Steps to reproduce the behavior:
1 - Given a definition like this:

 post:
tags:
      - Document
      summary: Consente il salvataggio di un documento
      description: Crea un nuovo documento collegato alla FK company_id. Vengono ritornate
        tutte le info di dettaglio del documento
      operationId: documentCreate
      parameters:
      - name: id
        in: path
        description: Identificativo impresa
        required: true
        schema:
          $ref: '#/components/schemas/UUID'
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  format: binary
                  type: string
                fileName:
                  type: string
                name:
                  type: string
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DocumentDTO'
        "401":
          description: Not Authorized
        "403":
          description: Not Allowed

2 - Run the script:
3 - The Angular service generated is:

 public documentCreate(id: string, file?: Blob, fileName?: string, name?: string, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<DocumentDTO>;
    public documentCreate(id: string, file?: Blob, fileName?: string, name?: string, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<HttpResponse<DocumentDTO>>;
    public documentCreate(id: string, file?: Blob, fileName?: string, name?: string, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<HttpEvent<DocumentDTO>>;
    public documentCreate(id: string, file?: Blob, fileName?: string, name?: string, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<any> {
        if (id === null || id === undefined) {
            throw new Error('Required parameter id was null or undefined when calling documentCreate.');
        }

        let localVarHeaders = this.defaultHeaders;

        let localVarCredential: string | undefined;
        // authentication (SecurityScheme) required
        localVarCredential = this.configuration.lookupCredential('SecurityScheme');
        if (localVarCredential) {
            localVarHeaders = localVarHeaders.set('Authorization', 'Bearer ' + localVarCredential);
        }

        let localVarHttpHeaderAcceptSelected: string | undefined = options && options.httpHeaderAccept;
        if (localVarHttpHeaderAcceptSelected === undefined) {
            // to determine the Accept header
            const httpHeaderAccepts: string[] = [
                'application/json'
            ];
            localVarHttpHeaderAcceptSelected = this.configuration.selectHeaderAccept(httpHeaderAccepts);
        }
        if (localVarHttpHeaderAcceptSelected !== undefined) {
            localVarHeaders = localVarHeaders.set('Accept', localVarHttpHeaderAcceptSelected);
        }

        let localVarHttpContext: HttpContext | undefined = options && options.context;
        if (localVarHttpContext === undefined) {
            localVarHttpContext = new HttpContext();
        }

        // to determine the Content-Type header
        const consumes: string[] = [
            'multipart/form-data'
        ];

        const canConsumeForm = this.canConsumeForm(consumes);

        let localVarFormParams: { append(param: string, value: any): any; };
        let localVarUseForm = false;
        let localVarConvertFormParamsToString = false;
        if (localVarUseForm) {
            localVarFormParams = new FormData();
        } else {
            localVarFormParams = new HttpParams({encoder: this.encoder});
        }


        let responseType_: 'text' | 'json' = 'json';
        if(localVarHttpHeaderAcceptSelected && localVarHttpHeaderAcceptSelected.startsWith('text')) {
            responseType_ = 'text';
        }

        return this.httpClient.post<DocumentDTO>(`${this.configuration.basePath}/v1/companies/${encodeURIComponent(String(id))}/documents`,
            localVarConvertFormParamsToString ? localVarFormParams.toString() : localVarFormParams,
            {
                context: localVarHttpContext,
                responseType: <any>responseType_,
                withCredentials: this.configuration.withCredentials,
                headers: localVarHeaders,
                observe: observe,
                reportProgress: reportProgress
            }
        );
    }

Expected behavior

 let localVarUseForm = true
    let localVarConvertFormParamsToString = false
    if (localVarUseForm) {
      localVarFormParams = new FormData()
      localVarFormParams.append('fileName', fileName)
      localVarFormParams.append('file', file)
      localVarFormParams.append('name', name)
    } else {
      localVarFormParams = new HttpParams({ encoder: this.encoder })
    }

Operation System:

  • OS: Window
  • Version 10

Package System:

  • Version 2.4.16

Additional context

  • Angular 12
  • Quarkus 2
@rahulahoop
Copy link

rahulahoop commented Dec 10, 2021

we are also expierencing the same issue, generated code produces dead if condition:


      // to determine the Content-Type header
        const consumes: string[] = [
            'multipart/form-data'
        ];

        const canConsumeForm = this.canConsumeForm(consumes); // this is unused

        let localVarFormParams: { append(param: string, value: any): any; };
        let localVarUseForm = false; // this is set to false
        let localVarConvertFormParamsToString = false;
        if (localVarUseForm) { // this
            localVarFormParams = new FormData(); // this never executes
        } else {
            localVarFormParams = new HttpParams({encoder: this.encoder});
        }
        

Angular 11.0.4
MacOS

cli version: 2.4.18

@LetMeSleepAlready
Copy link

Same.
I work around it by dropping a few versions.

5.3 has a delete bug (expected 1-2 parameters, received 3), so it does not even compile (
OpenAPITools/openapi-generator#10864 )
This was fixed in 5.4, but we got the file/form bug back..

so the only thing that works for me is:

openapi-generator-cli version-manager set 5.2.0

@kay-schecker
Copy link
Contributor

please see #31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants