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.

[BUG][JavaSpring] using "oneof" does not generate "OneOf*.java" models (openapi-generator-maven-plugin 4.3.0/4.3.1)

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What’s the version of OpenAPI Generator used? (openapi-generator-maven-plugin 4.3.0 and 4.3.1)
  • Have you search for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

While generating code with oneOf two models as a response (or request body) with Spring generator (openapi-generator-maven-plugin) code for the oneOf model is not generated. For the given spec below the only generated models are Model1 and Model2 but the interface uses a model named OneOfModel1Model2 and a file named OneOfModel1Model2.java is not generated which cause a compilation error:

import api.generated.models.OneOfModel1Model2;

. . .

@Validated
@Api(value = "Default", description = "the Default API")
public interface DefaultApi {

    /**
     * GET /test : Your GET endpoint
     *
     * @return OK (status code 200)
     */
    @ApiOperation(value = "Your GET endpoint", nickname = "getTest", notes = "", response = OneOfModel1Model2.class, tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "OK", response = OneOfModel1Model2.class) })
    @RequestMapping(value = "/test",
        produces = { "application/json" }, 
        method = RequestMethod.GET)
    ResponseEntity<OneOfModel1Model2> getTest();

}
openapi-generator version

Tried with openapi-generator-maven-plugin 4.3.0 and 4.3.1.

OpenAPI declaration file content or url
{
  "openapi": "3.0.0",
  "info": {
    "title": "testAPI",
    "version": "1.0"
  },
  "servers": [
    {
      "url": "http://localhost:3000"
    }
  ],
  "paths": {
    "/test": {
      "get": {
        "summary": "Your GET endpoint",
        "tags": [],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "$ref": "#/components/schemas/Model1"
                    },
                    {
                      "$ref": "#/components/schemas/Model2"
                    }
                  ]
                }
              }
            }
          }
        },
        "operationId": "get-test"
      }
    }
  },
  "components": {
    "schemas": {
      "Model1": {
        "title": "Model1",
        "type": "object",
        "properties": {
          "id1": {
            "type": "string"
          }
        }
      },
      "Model2": {
        "title": "Model2",
        "type": "object",
        "properties": {
          "id2": {
            "type": "integer"
          }
        }
      }
    }
  }
}
Steps to reproduce

Plugin configuration

    <properties>
        <kotlin.vesion>1.3.41</kotlin.vesion>
        <pact.artifactory.folder />
        <!--openapi generator-->
        <openapi-generator.version>4.3.1</openapi-generator.version>
        <openapi-generator.resources>/src/main/resources</openapi-generator.resources>
        <openapi-generator.specfile>api.json</openapi-generator.specfile>
        <openapi-generator.language>spring</openapi-generator.language>
        <openapi-generator.datelibrary>java8</openapi-generator.datelibrary>
        <openapi-generator.basepackage>api</openapi-generator.basepackage>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>${openapi-generator.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}${openapi-generator.resources}/${openapi-generator.specfile}
                            </inputSpec>
                            <output>${project.build.directory}/generated-sources</output>
                            <generatorName>${openapi-generator.language}</generatorName>
                            <skipValidateSpec>true</skipValidateSpec>
                            <configOptions>
                                <sourceFolder>.</sourceFolder>
                                <dateLibrary>java8</dateLibrary>
                                <interfaceOnly>true</interfaceOnly>
                                <skipDefaultInterface>true</skipDefaultInterface>
                                <useTags>true</useTags>
                            </configOptions>
                            <modelPackage>${openapi-generator.basepackage}.generated.models</modelPackage>
                            <apiPackage>${openapi-generator.basepackage}.generated.api</apiPackage>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <generateApiDocumentation>false</generateApiDocumentation>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <generateApiDocumentation>false</generateApiDocumentation>
                            <generateSupportingFiles>false</generateSupportingFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

Thanks 😃

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:13
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
j3dencommented, Jun 30, 2020

Hello. I am also getting the same. I can only guess that the implementation for Spring should be a Response Entity containing an “Object” type as opposed to the generated model.

I could of course for now just have it return “Object” but then the other aspects like documentation and client library generation will not be as accurate.

Thanks.

Screenshot from 2020-06-30 15-29-25

Screenshot from 2020-06-30 15-30-04

    parameters:
      - schema:
          type: string
          format: uuid
        name: purchaseID
        in: path
        required: true
        description: The ProductPurchaseID - UUID
    post:
      summary: Create a Payment Intent for a Product Purchase
      operationId: createPaymentIntentForPurchase
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/StripePaymentIntent'
      description: |-
        Create a payment intent for the product purcahse for the UX to handle payment.
        For example, passing the psp=STRIPE parameter will create a Stripe-Payment Intent for the product purchase.
      parameters:
        - schema:
            type: string
            enum:
              - STRIPE
              - PAYPAL
              - SAGE
          in: query
          name: psp
          description: The payment service provider to use.
          required: true
      tags:
        - Web

Screenshot from 2020-06-30 15-30-38

2reactions
ericamerchantcommented, Oct 6, 2020

I tried the following and as a result I got 4 classes. ObjectA, ObjectC, and ObjectD are all as defined. ObjectB is a combination of ObjectA and ObjectB, containing properties from both, but without any “oneof” type validation nor any inheritance with ObjectC and ObjectD.

      "ObjectA": {
        "type": "object",
        "properties": {
          "FancyProperty": {
            "$ref": "#/components/schemas/ObjectB"
          }
        }
      },
      "ObjectB": {
        "oneOf": [
          {
            "type": "string"
          },
          {
            "$ref": "#/components/schemas/ObjectC"
          },
          {
            "$ref": "#/components/schemas/ObjectD"
          }
        ]
      },
      "ObjectC": {
        "type": "object",
        "properties": {
          "title": {
            "type": "string"
          },
          "value": {
            "type": "object"
          },
          "message": {
            "type": "string"
          }
        }
      },
      "ObjectD": {
        "type": "object",
        "properties": {
          "something": {
            "type": "string"
          }
        }
      }
Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI models not being generated when using oneOf
I have oneOf generation working for java using openapi-generator 5.4.0 but I do add a discriminator field (like requestType: type: string enum: ...
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 >
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 >
Using the OpenAPI Generator for Spring Boot - mimacom blog
The idea of an API-first approach is to treat APIs as first-class citizens by building the software product around APIs.
Read more >
Plugins - OpenAPI Generator
<artifactId>openapi-generator-maven-plugin</artifactId> ... This dependency is not binary compatible with Swagger v2 annotations.
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