OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Invalid validateJsonObject code with okhttp-gson
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?
see below
Description
We frequently specialize base types in order to indicate specific domain meaning and to apply domain constraints centrally.
E.g.:
Uri:
type: string
format: uri
We also frequently "abuse" allOf to combine property-specific documentation with schema references:
Callback:
properties:
href:
description: property description here
allOf:
- $ref: '#/components/schemas/Uri'
When these two (string subtype plus allOf) come together, it breaks the generated validateJsonObject:
public static void validateJsonObject(JsonObject jsonObj) throws IOException {
//...
URI.validateJsonObject(jsonObj.getAsJsonObject("href"));
// ^^^ cannot find symbol
}
oneOf is similarly broken.
openapi-generator version
6.2.1
Regression: I upgraded from 5.4.0, where it still worked, to 6.2.1.
The error goes back to 6.0.0 with the new okhttp-gson-nextgen templates
OpenAPI declaration file content or url
openapi: 3.0.1
info:
title: validateJsonObject
version: "0"
description: Minimal example for invalid validateJsonObject
paths:
/:
get:
responses:
'200':
description: example path
content:
application/json:
schema:
$ref: '#/components/schemas/Callback'
components:
schemas:
Uri:
type: string
format: uri
Callback:
properties:
href:
description: this allOf triggers the bug
allOf:
- $ref: '#/components/schemas/Uri'
Generation Details
openapi-generator generate \
-i /project/src/main/resources/openapi.yaml \
-o /tmp/openapi \
-g java \
--api-package org.example.api \
--model-package org.example.model \
-p java8=true,dateLibrary=java8,hideGenerationTimestamp=true,sourceFolder=java
Steps to reproduce
- Generate code for the given schema using the above generator options
- Try to compile the code / have a look at generated
Callback.javaclass
Suggest a fix
The generator needs to check if the target type of a field to be validated is actually under its control, or skip the nested validate code.
Also, in our case, the additional validation isn't needed at all (validation happens on the returned values anyway). It would be awesome to have a generator option to skip the whole thing, including the CustomTypeAdapterFactory with its double conversion (JsonObject, bean).
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 by generating the supplied OpenAPI declaration with the Java generator and the okhttp-gson-nextgen templates, then inspect the generated Callback.java and its validateJsonObject method. Trace how the href field's allOf reference to the Uri string subtype is handled. Done means the generated Java code compiles without calling URI.validateJsonObject on an incompatible JsonObject, with the oneOf case considered as well.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100