OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Spring] OneOf base class not generated

Open
#9,981 0 comments 4 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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

While generating a Java server code with polymorphism (oneOf), we get a compilation error while the base class is imported due to it was not generated.

openapi-generator version

5.2.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: OneOf example
  version: '1.0.0'
paths:
  '/vegetables':
    get:
      summary: Get vegetables
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                oneOf:
                - $ref: '#/components/schemas/Tomato'
                - $ref: '#/components/schemas/Carrot'
        '404':
          description: Not Found
      operationId: getVegetables
components:
  schemas:
    Tomato:
      type: object
      properties:
        property1:
          type: integer
        property2:
          type: string
    Carrot:
      type: object
      properties:
        property3:
          type: integer
        property4:
          type: string  

Plugin configuration:

<plugin>
	<version>5.2.0</version>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>generate-rest-api</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/openapi-rest.yml</inputSpec>
                <generatorName>spring</generatorName>
                <generateSupportingFiles>true</generateSupportingFiles>                
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                    <java8>true</java8>
                    <interfaceOnly>true</interfaceOnly>
                    <apiPackage>com.example</apiPackage>                    
                    <modelPackage>com.example.dto</modelPackage>
                    <sourceFolder>src/gen/java/main</sourceFolder>                    
                </configOptions>
            </configuration>
        </execution>       
    </executions>    
</plugin>
Generation Details
Steps to reproduce

Generate code with openapi-generator-maven-plugin.

  • Add the openapi-generator-maven-plugin to a pom.xml
  • Add the previous OpenAPI to a file named openapi-rest.yml at ${project.basedir}/src/main/resources/
  • Execute mvn clean compile
  • Check the compilation error at DefaultApi.java file. It's trying to import a OneOfTomatoCarrot, but it has not been generated.
Related issues/PRs
Suggest a fix

Following the provided example, a OneOfTomatoCarrot class must be generated.

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

Reproduce the issue with the provided OpenAPI declaration and Spring generator configuration, then inspect the generated DefaultApi.java import and the missing OneOfTomatoCarrot class. Trace the oneOf model generation path and verify that the base class is generated and the resulting project compiles with mvn clean compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, 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.