Skip to content

duplicate $ref object in swagger UI #7576 #10393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Mathias02 opened this issue Mar 31, 2025 · 0 comments
Open

duplicate $ref object in swagger UI #7576 #10393

Mathias02 opened this issue Mar 31, 2025 · 0 comments

Comments

@Mathias02
Copy link

Content & configuration

Swagger/OpenAPI definition:

# your YAML here

components:
 schemas:
   User:
     type: object
     properties:
       id:
         type: string
       name:
         type: string
   Error:
     type: object
     properties:
       code:
         type: integer
       message:
         type: string

paths:
 /users:
   get:
     summary: Retrieve a list of users
     responses:
       200:
         description: A list of users
         content:
           application/json:
             schema:
               $ref: '#/components/schemas/User'  # Correct usage of $ref
       400:
         description: Invalid request
         content:
           application/json:
             schema:
               $ref: '#/components/schemas/Error'  # Different schema for error response

Swagger-UI configuration options:

SwaggerUI({
  // your config options here
url: "https://my-api.com/openapi.json",  // The path to your OpenAPI definition
 dom_id: "#swagger-ui",  // ID of the HTML container
 presets: [SwaggerUI.presets.apis],
 layout: "BaseLayout"
})
?yourQueryStringConfig

Is your feature request related to a problem?

Yes, when Swagger UI detects duplicate $ref objects, it may cause issues where the same object is rendered multiple times. This can happen if the OpenAPI specification contains duplicate references to the same schema or component, leading to confusion and rendering issues. e.g,
paths:
/users:
get:
responses:
200:
description: A list of users
content:
application/json:
schema:
$ref: '#/components/schemas/User'
400:
description: Invalid request
content:
application/json:
schema:
$ref: '#/components/schemas/User' # Duplicate $ref causing multiple renderings of the User schema

Describe the solution you'd like

The solution is to ensure that $ref references in the OpenAPI spec are unique, meaning they should not be poiting to the same definition or path in your OpenAPI specification.

  1. Ensure that every $ref in the OpenAPI spec refers to a unique schema,
  2. If using $ref to reference the same schema in multiple places, ensure the references are necessary or differentiate them (e.g., UserDetails vs. UserSummary).
  3. Swagger UI should display schemas based on these unique references without duplication

Describe alternatives you've considered

  • Using manually unique field names rather than $ref and reuse components,
  • Relying on Postman for API testing instead of Swagger UI, but this would defeat the purpose of having a Swagger UI that is easy to use for front-end testing,
  • Writing custom front-end logic to handle multipart/form-data or custom $ref parsing, but this requires significant additional work.

Additional context

  • Tools like Swagger Editor or Speccy can help identify duplicate references in your OpenAPI spec before loading them in Swagger UI,
  • This feature improvement would make it easier to maintain large OpenAPI specifications without having to manually track $ref usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant