swagger-api / swagger-api/swagger-codegen

[JAVA] Nested ref generates extra class definition import

Open
#9,874 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have a couple of API describing files that share the same data placed in other files. When schema definition references are nested an import of non-existing class and reference to it is created in model package although the intended model class is already there.
Please see the gist to see the mangled import and property type.

Swagger-codegen version

3.0.13

Swagger declaration file content or url

Top-level ref.yaml:

openapi: 3.0.1
info:
  title: Sample API
  description: API description in Markdown.
  version: 1.0.0
servers:
  - url: 'https://api.example.com'
paths:
  /users:
    get:
      summary: Returns a list of users.
      description: Optional extended description in Markdown.
      responses:
        '500':
          $ref: '#/components/responses/Error500'
          # References a reference in `Resposes`
components:
  schemas:
    DisplayMessage:
      $ref: './common/ref_schemas.yaml#/DisplayMessage'
    Error:
      $ref: './common/ref_errors.yaml#/Error'
  responses:
    Error500:
      $ref: './common/ref_responses.yaml#/Error500'
      # A reference to definition

The files used to reproduce the error along with created model are available as a gist

Command line used for generation
java -jar swagger-codegen-cli.jar generate -i ref.yaml -l java -o generated/clients/java
Steps to reproduce

Download the gist and run the build command

Related issues/PRs

#7797

Suggest a fix/enhancement

I assume all $refs that resolve to the same model should resolve to the same class

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 gist's top-level ref.yaml and referenced common files, then run the provided swagger-codegen-cli Java generation command. Compare the generated model imports and property types with the existing model classes; the fix is complete when nested $ref values resolving to the same model no longer create nonexistent duplicate class imports or references.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.