OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java] Java generator fails with nested external schemas
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
I am getting errors when I codegen a model with the java generator where an external schema loads an external schema.
I have traced in the debugger and am unsure whether cascading external schemas are supported. I think they should be and am willing to help to make this happen.
openapi-generator version
6.3.0
OpenAPI declaration file content or url
{
"openapi": "3.0.3",
"info": {
"title": "The title",
"version": "1.0",
"description": "The description",
"contact": {
"name": "Chris Giblin",
"email": "name@example.com"
}
},
"paths": {},
"components": {
"schemas": {
"Risk": {
"$ref": "./risk.json"
}
}
}
}
Generation Details
openapi-generator generate --global-property models -i pf-model-system.json -g java -o .. -c codegen.config
{
"basePackage": "com.example.model.system",
"modelPackage": "com.example.model.system",
"dateLibrary": "java8",
"generateBuilders": false,
"openApiNullable": false,
"interfaceOnly": false,
"library": "jersey2",
"sourceFolder": "src/main/java",
"serializationLibrary": "jackson",
"annotationLibrary": "none",
"swaggerAnnotations": false,
"generateAliasAsModel": true,
"disallowAdditionalPropertiesIfNotPresent": false
}
risk.json:
{
"$id": "id",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "risk entity",
"type": "object",
"additionalProperties": true,
"properties": {
"risk_value": {
"type": "string",
"default": null
}
},
"required": [],
"allOf": [
{"$ref": "./entity.json"}
]
}
Steps to reproduce
Run above openapi-generator command from the command line.
Expected output:
Successful execution of the command, no exceptions, and generated Risk.java class incorporating the entity.json properties due to the allOf declaration.
Actual output:
Output produces many warnings about entity.json before throwing an exception and terminating without generating code.
The full output is in this gist:
https://gist.github.com/chris-giblin/1df11906d87d9904eb22d3806cdcc05d
Related issues/PRs
Suggest a fix
I have checked out the code and run in the debugger. What I notice is that risk.json is indeed "resolved" (parsed) but its $ref, entity.json, is not. This contrasts with when I run under spec version 3.1.0 which indeed correctly resolves both risk.json and entity.json but fails during codegen at a different location. For some reason in 3.1.0 risk.json is resolved to class JsonSchema whereas under 3.0.3 it resolves to ComposedSchema. During 3.1.0 codegen, the JsonSchema instance is not regarded as "composed" so risk.json's allOf element is not generated, despite its $ref, entity.json, actually having been correctly parsed. Under 3.0.3, risk.json's $ref is left as is, as a string, and not parsed into a Schema instance which eventually results in the exception shown in the gist link, above.
I understand that spec 3.1.0 is a work in progress. The swagger parsing is working correctly in handling the cascading external refs however the generator does not take advantage of this.
I am stuck because both 3.0.3 and 3.1.0 fail for different reasons. I am willing to make code changes to openapi-generator but am unsure what the best next step is, what is supported, what is intended, whether cascading external refs are even supported, etc.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with pf-model-system.json, risk.json, entity.json, and the shown Java generator command. Compare external-reference resolution under OpenAPI 3.0.3 and 3.1.0, then trace the generator's handling of the resolved schemas; done means cascading refs generate Risk.java successfully with the entity.json properties.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 32/100