openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [typescript-angular] [5.4.0] - Wrong default response type 'blob' instead of JSON (OpenAPI model schema)

Open MaurizioCasciano opened this issue 3 years ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?

OpenAPI-Generator-maven-plugin

Description

Generating a typescript-angular client with OpenAPI-Generator-maven-plugin 5.4.0 has wrong default response type 'blob' instead of the OpenAPI model schema.

-        let responseType_: 'text' | 'json' = 'json';
-        if(localVarHttpHeaderAcceptSelected && localVarHttpHeaderAcceptSelected.startsWith('text')) {
-            responseType_ = 'text';
+        let responseType_: 'text' | 'json' | 'blob' = 'json';
+        if (localVarHttpHeaderAcceptSelected) {
+            if (localVarHttpHeaderAcceptSelected.startsWith('text')) {
+                responseType_ = 'text';
+            } else if (this.configuration.isJsonMime(localVarHttpHeaderAcceptSelected)) {
+                responseType_ = 'json';
+            } else {
+                responseType_ = 'blob';
+            }
        }

Printing the response gives:

Blob
size: 161
type: "application/json"
[[Prototype]]: Blob
arrayBuffer: ƒ arrayBuffer()
size: (...)
slice: ƒ slice()
stream: ƒ stream()
text: ƒ text()
type: (...)
constructor: ƒ Blob()
Symbol(Symbol.toStringTag): "Blob"
get size: ƒ size()
get type: ƒ type()
[[Prototype]]: Object

instead of the actual object defined in the schemas.

openapi-generator version

5.4.0

OpenAPI declaration file content or url
responses:
        "200":
          description: OK
          content:
            */*:

instead of

responses:
        "200":
          description: OK
          content:
            application/json:
Generation Details

This is my pom.xml plugin config:

			<plugin>
				<groupId>org.openapitools</groupId>
				<artifactId>openapi-generator-maven-plugin</artifactId>
				<version>5.4.0</version>
				<executions>
					<execution>
						<id>angular</id>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<skip>false</skip>
							<skipIfSpecIsUnchanged>false</skipIfSpecIsUnchanged>
							<inputSpec>${project.basedir}/src/main/resources/api/API.yaml</inputSpec>
							<output>${project.basedir}/src/main/resources/api-client/ng-client</output>
							<generatorName>typescript-angular</generatorName>
							<configOptions>
								<npmName>@project/api-client</npmName>
								<npmVersion>1.0.0</npmVersion>
								<ngVersion>12.2.15</ngVersion>
								<apiModulePrefix>Project</apiModulePrefix>
							</configOptions>
						</configuration>
					</execution>
				</executions>
			</plugin>
Steps to reproduce
Related issues/PRs
Suggest a fix

The fix required the setting of this external application property, of SpringDoc, for generating the OpenApi yaml:

springdoc:
  default-produces-media-type: application/json

to override the default, which is

springdoc:
  default-produces-media-type: */*

Maybe the generated client should continue to default to JSON as the default httpHeaderAccepts even if the response content is */*.

Otherwise, this behaviour needs to be documented.

MaurizioCasciano avatar Feb 02 '22 11:02 MaurizioCasciano

Hi, we are facing the same problem. Would be great if this bug can be fixed. Thank you very much

helloworld121 avatar Aug 08 '22 11:08 helloworld121

Thanks!!

JayAhn2 avatar Oct 25 '22 09:10 JayAhn2

We have same problem

doox911 avatar Nov 18 '22 12:11 doox911

Can anyone fix this on urgent bases?

amitmahida92 avatar Feb 06 '23 16:02 amitmahida92

Hello, The same here. Is there any fix or at least a workaround to fix it temporaly ? Thank you

Krazymins avatar Mar 09 '23 12:03 Krazymins

Hello, The same here. Is there any fix or at least a workaround to fix it temporaly ? Thank you

You can force your service to return json.

instead of

else {
                responseType_ = 'blob';
            }

Use

else {
                responseType_ = 'json';
            }

ugurkoysuren avatar Aug 10 '23 13:08 ugurkoysuren

Thank you for your anwser. Indeed we can change the generated code, but this has to be done again each time the code is regenerated. I was wondering if a workaround can be done on the java side for instance, within the REST operation definition. Thanks.

Krazymins avatar Aug 10 '23 14:08 Krazymins

Hello, just a quick update. After trying several things, I managed to solve it on my side. This is what I did :

Bug Report Checklist

instead of the actual object defined in the schemas.

openapi-generator version

5.4.0

OpenAPI declaration file content or url
responses:
        "200":
          description: OK
          content:
            */*:

instead of

responses:
        "200":
          description: OK
          content:
            application/json:

... Maybe the generated client should continue to default to JSON as the default httpHeaderAccepts even if the response content is */*.

To force httpHeaderAccepts on Angular side to be set as 'application/json', we need indeed to set the content on the backend side to 'application/json'.

I tried first to override the @ApiResponse and set the @Content value in my RestController, but then other issues came (return objects were not generated for instance, and Angular Service had 'any' types in services signature.

So the solution here is just to define the produced media_type: @RequestMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)

and not override the @ApiResponse with a content value. (If you do this you may need to explicitely describe the returned object.)

In that way, you'll well get this generated api :

 responses:
         "200":
           description: OK
           content:
             application/json:

and on the Angular side, you'll well get httpHeaderAccepts set to "application/json", so no need to override the generated code anymore.

Krazymins avatar Sep 20 '23 10:09 Krazymins