OpenAPITools / OpenAPITools/openapi-generator

application/pdf doesn't work

Open
#12,023 0 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

This is a similar issue to https://github.com/OpenAPITools/openapi-generator/issues/2035

Instead of text/plain not being handled, it's application/pdf that isn't handled. The response comes back perfectly fine from the API (works in postman and I see it in dev tools). However, once it goes through the generated code, the response get's wiped out and set to undefined. My hunch is that because the responseType is set to json opposed to 'blob'

Generated code

/**
     * @param id 
     * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
     * @param reportProgress flag to report request and response progress.
     */
    public downloadReportAsync(id?: string, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/pdf'}): Observable<object>;
    public downloadReportAsync(id?: string, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/pdf'}): Observable<HttpResponse<object>>;
    public downloadReportAsync(id?: string, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/pdf'}): Observable<HttpEvent<object>>;
    public downloadReportAsync(id?: string, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/pdf'}): Observable<any> {

        let queryParameters = new HttpParams({encoder: this.encoder});
        if (id !== undefined && id !== null) {
          queryParameters = this.addToHttpParams(queryParameters,
            <any>id, 'id');
        }

        let headers = this.defaultHeaders;

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


        let responseType: 'text' | 'json' = 'json';  // <-- Issue seems to be here
        if(httpHeaderAcceptSelected && httpHeaderAcceptSelected.startsWith('text')) {
            responseType = 'text';
        }

        return this.httpClient.get<object>(`${this.configuration.basePath}/api/testing`,
            {
                params: queryParameters,
                responseType: <any>responseType, // <-- should be 'blob' but ends up as 'json'
                withCredentials: this.configuration.withCredentials,
                headers: headers,
                observe: observe,
                reportProgress: reportProgress
            }
        );
    }
openapi-generator version

4.3.1

OpenAPI declaration file content or url
"/api/testing": {
      "get": {
        "tags": [
          "SomeTag"
        ],
        "operationId": "DownloadAsync",
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "schema": {
              "type": "string",
              "nullable": true
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/pdf": {
                "schema": {
                  "$ref": "#/components/schemas/File"
                }
              }
            }
          }
        }
      }
    },
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

It looks like a fix has been suggested in similar issue (https://github.com/OpenAPITools/openapi-generator/issues/11494). However, it would be nice to have this functionality work out of the box. If there was a way to allow a response type to be passed in, I think it would work just fine.

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 Angular client method shown in the issue and the OpenAPI declaration for /api/testing, then trace how the generator selects the response type for application/pdf. Reproduce the generated client behavior and confirm completion when a PDF response remains available instead of becoming undefined.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.