[BUG][JAVA] Model generated from allOf is unusable when combining $ref and inline schema (regression from 7.1.0)
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?
- [ ] 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
We are also running into issues regarding allof constructions specifically since 7.2!
please fall back to v7.1.0 for the time being.
i've filed https://github.com/OpenAPITools/openapi-generator/pull/17683 to fix the issue.
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)
someProperty:
type: string
nullable: true
allOf:
- $ref: "#/components/schemas/StringBaseType"
what exact is this "type"? A string extending another string?
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?
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: stringis 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.
👌
I'll take a look later this week instead
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"
},