Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][JAVA] Model generated from allOf is unusable when combining $ref and inline schema (regression from 7.1.0) #17534

Closed
4 of 6 tasks
mthmulders opened this issue Jan 4, 2024 · 8 comments · Fixed by #19986

Comments

@mthmulders
Copy link

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

In an OpenAPI specification where schema components use allOf to combine a $ref with inline properties, openapi-generator generates Java classes that

  • cannot be parsed from JSON that adheres to the schema
  • cannot be instantiated to construct values that would adhere to the schema
openapi-generator version

This issue popped up when Renovate suggested an upgrade from openapi-generator 7.1.0 to 7.2.0. I therefor believe it is a regression in 7.2.0.

OpenAPI declaration file content or url

Full spec included in the attached test-sdk.zip. I believe the relevant part is this:

    MyResponseType:
      type: object
      allOf:
        - $ref: "#/components/schemas/MyBaseType"
        - properties:
            someProperty:
              type: string
              nullable: true
              allOf:
                - $ref: "#/components/schemas/StringBaseType"
Generation Details
Steps to reproduce
  • Unpack the attached test-sdk.zip
  • Run the following commands inside the test-sdk directory:
     mvn clean
     mvn generate-sources -Dopenapi-generator.version=7.1.0 -DpackageName=v710
     mvn generate-sources -Dopenapi-generator.version=7.2.0 -DpackageName=v720
     mvn verify

This generates two OpenAPI client in the same output directory (one with v7.1.0, one with v7.2.0). The ExampleTest illustrates the problem with the code that gets generated by v7.2.0.

Related issues/PRs
Suggest a fix
@Laurens-W
Copy link
Contributor

We are also running into issues regarding allof constructions specifically since 7.2!

@wing328
Copy link
Member

wing328 commented Jan 23, 2024

please fall back to v7.1.0 for the time being.

i've filed #17683 to fix the issue.

@mthmulders
Copy link
Author

I've tried my test case with #17683 and it did not fix the issue. Running the attached test project with mvn generate-sources -Dopenapi-generator.version=7.3.0-SNAPSHOT -DpackageName=v730 (which at that time resolved to 7.3.0-20240113.125051-56) fails with

com.fasterxml.jackson.databind.exc.InvalidTypeIdException:
Could not resolve type id 'FixedStringBaseType' as a subtype of `testsdk_v730.models.MyResponseTypeAllOfSomeProperty`: Class `testsdk_v730.models.FixedStringBaseType` not subtype of `testsdk_v730.models.MyResponseTypeAllOfSomeProperty`
 at [Source: (ByteArrayInputStream); line: 5, column: 30] (through reference chain: testsdk_v730.models.MyResponseType["someProperty"])
	at testsdk.ExampleTest.parse valid JSON with code generated with v730(ExampleTest.kt:49)

@wing328
Copy link
Member

wing328 commented Jan 24, 2024

            someProperty:
              type: string
              nullable: true
              allOf:
                - $ref: "#/components/schemas/StringBaseType"

what exact is this "type"? A string extending another string?

@wing328
Copy link
Member

wing328 commented Jan 24, 2024

if we look at the whole thing:

      allOf:
        - $ref: "#/components/schemas/MyBaseType"
        - properties:
            someProperty:
              type: string
              nullable: true
              allOf:
                - $ref: "#/components/schemas/StringBaseType"

looks like MyBaseType is the base type and this model has a single property called someProperty, which should be a nullable string but it has "allOf" with a single element referencing StringBaseType.

so what exactly is "someProperty"? just a nullable string?

@mthmulders
Copy link
Author

Hi @wing328, apologies for the confusion, I see a little mistake slipped into the sample project.

I have an updated version of the sample project, with the following changes:

  • The type: string is not there in the original spec. I've removed it from the sample project.
  • I've added changes to the pom.xml so you can run it with 7.3.0-SNAPSHOT, too.

The issue however is still there, also with type: string removed.

@wing328
Copy link
Member

wing328 commented Jan 24, 2024

👌

I'll take a look later this week instead

@vicentepinto98
Copy link

Hi, this issue seems to still be present in the latest version of the generator.
I am facing the error that the generated code defines fields as an unexistent NullableInterface type.

Example OAS that results in error:

"NullableModel": {
        "allOf": [
          {
            "$ref": "#/components/schemas/SomeString"
          }
        ],
        "nullable": true
      },
   "SomeString": {
        "type": "string"
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants