Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #12556
Closed
Open
Issue created Jun 08, 2022 by Administrator@rootContributor

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

Created by: R4NN-BAE

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.

Assignee
Assign to
Time tracking