question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

openapi-generator-maven-plugin generates invalid Java code when using "oneOf:" property

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
lostinicelandcommented, Nov 17, 2022

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

0reactions
karzangcommented, Nov 18, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

openapi-generator /maven plugin generates invalid java code ...
I am using the generator through the maven plugin. I need it to generate a client for an external API (outside my control),...
Read more >
Swagger OpenAPI Code Gen "oneOf" : How to generate...
I am creating the spring/java server side rest api code. And when I send a request. The request object is not parsed correctly....
Read more >
To provide nicer logging when a Spring property is invalid ...
Coding example for the question To provide nicer logging when a Spring property is invalid-Springboot.
Read more >
Generate Server Code Using OpenAPI Generator
You can read here my impressions of that day. One of the talks I attended described how to design your API first and...
Read more >
Open API Server Implementation Using OpenAPI Generator
Learn how to generate a Spring-based server stub from a YAML specification using the OpenAPI generator's Maven plugin.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found