OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.6k stars 6.29k forks source link

[BUG] Unable to generate shared external reference DTOs for open API 3.1 #18361

Open pkernevez opened 2 months ago

pkernevez commented 2 months ago
Description

I try to move from openapi 3.0.3 to 3.1, but I have a strange behaviour with ref schema. With 3.0.3 ref schemas are generated (java/spring) with a dedicated Dto, example a schema common.Amount creates a Dto CommonAmount that use as is from all the places. With 3.1 it now creates a new schema for all the places it’s used. It’s not related to the generator, but to the parsing: It changed in file OpenAPIV3Parser, L211 if (result.getOpenAPI().getOpenapi() != null && result.getOpenAPI().getOpenapi().startsWith("3.1")) {

I had a maven/springboot project to reproduce it: https://github.com/pkernevez/pb-openapi The only difference in swaggers “3.0” and “3.1" are the version in the first line. In my example, I have a share type : common.Amount (a quantity and a currency). With 3.0.3 I have a shared type CommonAmount, with 3.1 I now have a new type for each usage (here market value). Why this behaviour changed ? Is there a way to restore the previous way ?

Having a new class for each usage is an issue to define only once shared code, like mapping Entity <-> Dto

image

openapi-generator version

Tested with :

OpenAPI declaration file content or url
Generation Details

APIs are the same, the issues is for Dtos.

image
Steps to reproduce
mvn clean install
find target/generated-sources/src/main/java/net/kernevez/generated
pkernevez commented 2 months ago

Additionally and probably related to this: On top of it, I have a share ErrorDto type. Before (3.0.3) it was uses as is : CommonError for the different Error declarations, but now it creates a new type for the first usage, meaning my error 400 and 404 are returning a type named GetPosition400Response

wing328 commented 2 months ago

did you have a chance to try the latest master? we've merged a couple of fixes related to 3.1 spec

https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/7.5.0-SNAPSHOT/

pkernevez commented 2 months ago

Yes, as mentioned in my ticket, I tested it with 7.4.0 & 7.5.0-SNAPSHOT on April 11th. I recheck now with 7.5.0-20240412.172223 and I have the same behavior.

wing328 commented 2 months ago

i'm able to repeat the issue:

-openapi: 3.0.3
+openapi: 3.1.0
 info:
   title: An example
   version: "1.0"
@@ -12,11 +12,15 @@ paths:
     get:
       operationId: getPosition
       parameters:
-      - in: query
+      - explode: true
+        in: query
         name: accountId
         required: true
         schema:
-          $ref: '#/components/schemas/common.Tsid'
+          example: 0FHP0N1QTKMBS
+          format: b32
+          type: string
+        style: form
       - example: 2024-01-05
         explode: true
         in: query
@@ -37,118 +41,89 @@ paths:
           content:
             application/json:
               schema:
-                $ref: '#/components/schemas/common.ErrorDto'
+                description: Generic container for errors
+                example:
+                  errorCode: ErrorCode
+                  errorMessage: Error message
+                  errorDetails: Error details
+                  correlationId: 08d3b3e0-a300-4fac-9837-dd8c2fd1e899
+                properties:
+                  errorCode:
+                    type: string
+                  errorMessage:
+                    type: string
+                  errorDetails:
+                    type: string
+                  correlationId:
+                    type: string
           description: Invalid request data
         "404":
           content:
             application/json:
               schema:
-                $ref: '#/components/schemas/common.ErrorDto'
+                description: Generic container for errors
+                example:
+                  errorCode: ErrorCode
+                  errorMessage: Error message
+                  errorDetails: Error details
+                  correlationId: 08d3b3e0-a300-4fac-9837-dd8c2fd1e899
+                properties:
+                  errorCode:
+                    type: string
+                  errorMessage:
+                    type: string
+                  errorDetails:
+                    type: string
+                  correlationId:
+                    type: string

I suspect it's swagger parser issue for 3.1 spec (even though I use setResolveFully(false) when debugging this issue) as you can it seems to force setResolveFully to true for 3.1 spec (but not 3.0.x spec).

can you please open an issue in the swagger parser repo when you've time?