OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] oneOf/anyOf validateJsonElement cannot find symbol
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
The java generator using okhttp-gson generates code that doesn't compile because of .validateJsonElement calls on Objects that don't have this method.
This happens with both oneOf and anyOf types.
// validate the json string with OffsetDateTime
try {
OffsetDateTime.validateJsonElement(jsonElement);
return;
} catch (Exception e) {
errorMessages.add(String.format("Deserialization for OffsetDateTime failed with `%s`.", e.getMessage()));
// continue to the next one
}
Gradle logs:
https://gist.github.com/Bethibande/9582795e2e1fd47695019d8f1c685a8b
openapi-generator version
latest/master
OpenAPI declaration file content or url
https://gist.github.com/Bethibande/55942a3f4e671d7352257c3833b154d6
Suggest a fix
When validating json elements, the fallback for types that do not have any special handling like arrays or primitives is always to call
the static .validateJsonElement of the type. That doesn't work for non-generated types like OffsetDateTime though.
{{^isPrimitiveType}}
{{{dataType}}}.validateJsonElement(jsonElement);
actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isPrimitiveType}}
It has to be checked whether or not we are dealing with a type that has a validateJsonElement method (a generated model class?),
if not, other validation checks have to be performed.
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
Start with the okhttp-gson generator template containing the shown oneOf/anyOf validateJsonElement snippet, then compare handling for generated model types with non-generated types such as OffsetDateTime. Generate the supplied OpenAPI declaration and run the Gradle build; done means the generated Java code compiles without calling missing validateJsonElement methods.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100