cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
399 stars 134 forks source link

Wrong blob type for text/* mime type with format: binary #180

Closed drenda closed 2 years ago

drenda commented 2 years ago

I've this endpoint in my backend:

@Operation(summary = "Download the ics file for Apple/Outlook")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "Successful operation"),
            @ApiResponse(responseCode = "400", description = "Invalid input"),
            @ApiResponse(responseCode = "401", description = "Not authorized"),
            @ApiResponse(responseCode = "500", description = "Internal server error"),
    })
    @GetMapping(value = "/appointments/{id}/ics", produces = "text/calendar")
    ResponseEntity<Resource> downloadAppointmentIcs(@Parameter(description = "The appointment id", required = true) @PathVariable(value = "id") long id);

with this OpenApi desc:

"/api/v1/appointments/{id}/ics": {
      "get": {
        "tags": [
          "Appointment"
        ],
        "summary": "Download the ics file for Apple/Outlook",
        "operationId": "downloadAppointmentIcs",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The appointment id",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "500": {
            "description": "Internal server error",
            "content": {
              "text/calendar": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "401": {
            "description": "Not authorized",
            "content": {
              "text/calendar": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "400": {
            "description": "Invalid input",
            "content": {
              "text/calendar": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "200": {
            "description": "Successful operation",
            "content": {
              "text/calendar": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          }
        }
      }
    },

Running ng-openapi-gen the service is created:

    downloadAppointmentIcs$Response(params: {

    id: number;}): Observable<StrictHttpResponse<Blob>> {

    const rb = new RequestBuilder(this.rootUrl, AppointmentService.DownloadAppointmentIcsPath, 'get');
    if (params) {
      rb.path('id', params.id, {});
    }

    return this.http.request(rb.build({
      responseType: 'text',
      accept: 'text/calendar'
    })).pipe(
      filter((r: any) => r instanceof HttpResponse),
      map((r: HttpResponse<any>) => {
        return r as StrictHttpResponse<Blob>;
      })
    );
  }

When I manage the result Blob I get some trouble because it seems justa string and not a real Blob. I think the problem is the responseTypethat should be "blob" and not "text" as you can see in the generated service. Am I missing something or is there a way to instruct ng-openapi-gen to consider text/calendar as a blob? I'm using 0.17.3 version

luisfpg commented 2 years ago

The Blob type is generated because you have "format": "binary". However, there's still a bug that the generated responseType should be blob and not text.

drenda commented 2 years ago

The Blob type is generated because you have "format": "binary". However, there's still a bug that the generated responseType should be blob and not text.

Thanks for your quick reply. Binary format is good in my case, I expect to have a blob on the client; I don't expect the responseType to be text "text" but blob instead.

luisfpg commented 2 years ago

Released in 0.19.2