OpenAPITools / OpenAPITools/openapi-generator

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

Open
#6,816 10 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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 :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the supplied OpenAPI JSON and openapi-generator-maven-plugin configuration using the Spring generator. Compare the generated model files with DefaultApi.java and verify whether OneOfModel1Model2.java is missing; done means the generated project contains that model and compiles without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.