cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

Question: Is it possible to generate request with responseType:'arraybuffer'? #140

Closed felikf closed 3 years ago

felikf commented 3 years ago

I have a question if it is possible to generate a request with responseType:'arraybuffer' ?

Something like this:

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

Source code says this, but I may overlook something: (operation-variant.ts)

  private inferResponseType(mediaType: string): string {
    mediaType = mediaType.toLowerCase();
    if (mediaType.endsWith('/json') || mediaType.endsWith('+json')) {
      return 'json';
    } else if (mediaType.startsWith('text/')) {
      return 'text';
    } else {
      return 'blob';
    }
  }

So I guess not :)

pillsilly commented 3 years ago

Same needs here 🤔

my thought is to specify mapping logic in corresponding config.json

customizedResponseType: {
    api: 'a/b/c/d', {
        toUse: arraybuffer
    }
}

how do you think or are there any maintaincer here to give some comments?

@luisfpg if the idea is ok then I am willing to provide implementation.

luisfpg commented 3 years ago

@felikf, it is not currently possible. @pillsilly We could discuss it. There are options:

  1. We could define a default binary response type, so all binaries could be changed from blob to arraybuffer
  2. There could be a mapping per operation, as you have suggested.

Would 1 be enough for you? It would be way simpler.

pillsilly commented 3 years ago

1 Is simple though when it comes to specific path depended then it could be again insufficient ?

2 Just created PR, feel free to give some comments @luisfpg @felikf

Usage:

thanks.

andriivandakurov commented 2 years ago

Hi, is it possible to define customizedResponseType per http method/status code ?

"paths": {
        "/api/account": {
            "post": {
                "tags": [
                    "Account"
                ],
                "operationId": "postAccountControllerInvite",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/AccountInviteRequest"
                            }
                        }
                    }
                },
                "responses": {
                    "204": {
                        "description": "When new account invitation sending was successful"
                    },
                    "400": {
                        "description": "Returns 400 in case of an error",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/ErrorResponse"
                                }
                            }
                        }
                    }
                }
            }
        },

Let's say in case of 204 i return empty response body and after ng-openapi-gen i get this code with responseType: 'text' instead of responseType: 'json'

postAccountControllerInvite$Response(params?: {
        context?: HttpContext;
        body?: AccountInviteRequest;
    }): Observable<StrictHttpResponse<void>> {
        const rb = new RequestBuilder(this.rootUrl, AccountService.PostAccountControllerInvitePath, 'post');
        if (params) {
            rb.body(params.body, 'application/json');
        }

        return this.http
            .request(
                rb.build({
                    responseType: 'text',
                    accept: '*/*',
                    context: params?.context,
                })
            )
            .pipe(
                filter((r: any) => r instanceof HttpResponse),
                map((r: HttpResponse<any>) => {
                    return (r as HttpResponse<any>).clone({ body: undefined }) as StrictHttpResponse<void>;
                })
            );
    }

As i know json should be a default responseType, but for some reason i got responseType: 'text'.

Is there any other option to achieve this then adding @OA\MediaType annotation to the response ?

     *     @OA\Response(
     *          response="204",
     *          content={
     *             @OA\MediaType(
     *                 mediaType="application/json"
     *             )
     *         },
     *         description="Update successful, no response data",
     *     ),

@luisfpg @pillsilly