-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Description
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,
Lines 101 to 123 in 69f79fb
| {{#oneOf}} | |
| // deserialize {{{.}}} | |
| try { | |
| // validate the JSON object to see if any exception is thrown | |
| {{.}}.validateJsonObject(jsonObject); | |
| actualAdapter = adapter{{.}}; | |
| match++; | |
| log.log(Level.FINER, "Input data matches schema '{{{.}}}'"); | |
| } catch (Exception e) { | |
| // deserialization failed, continue | |
| errorMessages.add(String.format("Deserialization for {{{.}}} failed with `%s`.", e.getMessage())); | |
| log.log(Level.FINER, "Input data does not match schema '{{{.}}}'", e); | |
| } | |
| {{/oneOf}} | |
| if (match == 1) { | |
| {{classname}} ret = new {{classname}}(); | |
| ret.setActualInstance(actualAdapter.fromJsonTree(jsonObject)); | |
| return ret; | |
| } | |
| throw new IOException(String.format("Failed deserialization for {{classname}}: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonObject.toString())); | |
| } |
It looks like this would generate .validateJsonObject(jsonObject) on every type which is not valid
for type Array, String, Boolean, BigDecimal in Java.