OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Java/Spring] Generated code doesn't compile(`cyclic inheritance`) when using oneOf when Component Name + attribute name ==Component name of oneOf ref.

Open
#14,627 3 comments 0 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

Generated code doesn't compile(cyclic inheritance) when using oneOf when Component Name + attribute name ==Component name of oneOf ref.

openapi-generator version

6.3.0 (openapi-generator-maven-plugin)

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Swagger Animal store - OpenAPI 3.0
  version: 1.0.11
paths:
  /animal:
    get:
      operationId: findAnimal
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Animal'
components:
  schemas:
    Animal:
      type: object
      required:
        - id
      properties:
        id:
          type: integer
          format: int64
        type:
          oneOf:
            - $ref: '#/components/schemas/AnimalType'
        breed:
          oneOf:
            - $ref: '#/components/schemas/AnimalBreed'
    AnimalBreed:
      properties:
        breedName:
          type: string
    AnimalType:
      type: string
      enum:
        - friendly
        - unfriendly
Generation Details

Maven config:

<plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.3.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/petstore.yml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <generateApis>false</generateApis>
                            <generateSupportingFiles>false</generateSupportingFiles>
                            <configOptions>
                                <dateLibrary>java8</dateLibrary>
                                <openApiNullable>false</openApiNullable>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce

mvn clean install

Complete code available at https://github.com/smilep/open-api-play

Issue:
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2023-02-06T09:26:53.541+05:30[Asia/Calcutta]")
public interface AnimalBreed extends AnimalBreed {
}

Generated interface and implementation class has same name : AnimalBreed.

Suggest a fix

Generated interface and implementation class should have different names.

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 with the supplied OpenAPI YAML and Maven configuration using the spring generator, then run mvn clean install to reproduce the cyclic AnimalBreed extends AnimalBreed output. Trace how the AnimalBreed schema and generated interface/class names are derived. Done means the generated Java code compiles and the interface and implementation have distinct names.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.