openapi-generator
openapi-generator copied to clipboard
openapi-generator-maven-plugin generates invalid Java code when using "oneOf:" property
Description
Maven test target in openapi-generator-maven-plugin will run successfully,
but the generated Java models are not valid Java code and cannot be compiled by Java compiler
and therefore cannot be used as a JavaClient.
Because of the following issues in the generated Java model Value.java
:
Array.validateJsonObject(jsonObject); // The method validateJsonObject(JsonObject) is undefined for the type Array
Is the generated Java code in the maven test target not validate by a Java compiler?
Note, the same error can be reproduced with the Java Types, String, Boolean, BigDecimal
Steps to reproduce
When using following modified schema in openapi\modules\openapi-generator-maven-plugin\src\test\resources\petstore-on-classpath.yaml
openapi: 3.0.0
servers:
- url: 'http://petstore.swagger.io/v2'
info:
description: >-
This is a sample server Petstore server. For this sample, you can use the api key
`special-key` to test the authorization
version: 1.0.0
title: OpenAPI Petstore
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
...
paths:
/values:
get:
tags:
- values
summary: Get some primitive values
description: ''
operationId: getSomeValues
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Value'
'400':
description: Invalid Value
...
schemas:
Value:
description: Value datatype
oneOf:
- $ref: "#/components/schemas/Scalar"
- $ref: "#/components/schemas/Array"
Scalar:
description: Scalar datatype
oneOf:
- type: string
maxLength: 1089
- type: number
- type: boolean
Array:
type: array
minItems: 1
items:
$ref: "#/components/schemas/Scalar"
And the following modified openapi\modules\openapi-generator-maven-plugin\pom.xml
,
the <library>jersey2</library>
was removed.
<configuration>
<inputSpec>petstore-on-classpath.yaml</inputSpec>
<generatorName>java</generatorName>
<skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
<configOptions>
<dateLibrary>joda</dateLibrary>
</configOptions>
<output>${basedir}/target/generated-sources/common-maven/remote-openapi</output>
<apiPackage>remote.org.openapitools.client.api</apiPackage>
<modelPackage>remote.org.openapitools.client.model</modelPackage>
<invokerPackage>remote.org.openapitools.client</invokerPackage>
</configuration>
openapi-generator version
Can be reproduced with the current "Master" version of this repository. But also the compile error was reproduced on a build server with the openapi-generator-maven-plugin:
<!-- RELEASE_VERSION -->
<version>6.0.0</version>
<!-- /RELEASE_VERSION -->
Suggest a fix
Maybe there should be a more strict check in this code section, https://github.com/OpenAPITools/openapi-generator/blob/69f79fb7892948590a9ffe46754c47ddd2634be1/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/oneof_model.mustache#L101-L123
It looks like this would generate .validateJsonObject(jsonObject) on every type which is not valid
for type Array, String, Boolean, BigDecimal
in Java.
I'm having the same issue with DigitalOceans's API - https://github.com/digitalocean/openapi
$openapi-generator --version openapi-generator-cli 6.0.0 commit : 69f79fb built : 2022-05-26T02:54:15Z source : https://github.com/openapitools/openapi-generator docs : https://openapi-generator.tech/
+1 seeing this issue with 6.0.0 as well on one of our internal apis @ adobe.
The same here with:
Command: openapi-generator-cli generate -i SWIFT-API-swift-banking-premium-1.1.2-swagger.yaml -g java -o out
YAML:
https://developer.swift.com/sites/default/files/specs/SWIFT-API-swift-banking-premium-1.1.2-swagger.yaml
I didn't specify any specific property and I got the code:
... // deserialize Object try { // validate the JSON object to see if any exception is thrown Object.validateJsonObject(jsonObject); actualAdapter = adapterObject; match++; log.log(Level.FINER, "Input data matches schema 'Object'"); ...
+1 seeing this issue with 6.1.0 as well on one of our internal apis
@wing328 Hi William, please can you prioritize this bug for 6.2.1. I'm trying to read the code and fix it myself but the code base is huge and I still don't get how things work :-)
It is not only oneOf
that is affected. I have the same problem with anyOf
. I assume this will gain relevance since swagger-api/swagger-core#4291 was merged.
We also have this issue with one of our builds running openapi-generator-maven-plugin
in version 6.2.1, which is the most recent one. The issue affects Boolean
, Double
, Float
, Integer
, Long
, String
. It would be great if this bug could be fixed very soon.
In #13897 I provided a potential fix for this problem regarding oneOf
. I think this PR would also be a good baseline for fixing the same problem concerning anyOf
.
I am experiencing the same problem with Fireblocks API, @StevenArzt it also affects BigDecimal
, on top of the other classes you listed.
error: cannot find symbol
[ERROR] symbol: method validateJsonObject(JsonObject)
[ERROR] location: class BigDecimal
Same here with 6.2.1
We are seing an LocalDate.validateJsonObject(...
which is completely bogus. It seems to be cause by an allOf
in this case
Is there a known workaround for this issue?
I think a workaround would be to wrap your Boolean
, Double
, Float
, Integer
, Long
, String
or LocalDate
Schema into an Object and then reference the object in your allOf
/anyOf
/oneOf
Still facing the same issues, it's latest versions (6., 7.) generating the invalid code like validateJsonObject
and java compiler failed to compile it.
Solution : I found 5.1.0
is the stable one and it is working for me in my case and doesn't generated any such invalid codes.
Still facing the same issues, it's latest versions (6., 7.) generating the invalid code like
validateJsonObject
and java compiler failed to compile it.Solution : I found
5.1.0
is the stable one and it is working for me in my case and doesn't generated any such invalid codes.
Facing the same issue with openapi 6.6.0
Facing the same issue with openapi 6.6.0 , and is there any workaround here, wrap as a object may not have the same behavior our request body must be "name": "123" or "name":{ "operation": true } name: oneOf: - type: string - $ref: "#/components/schemas/operation"
It (test spec provided in the issue description) works with okhttp-gson but not jersey2/3.
If anyone would like to contribute a PR to sponsor the fix, please reply to let us know. Thank you.