swagger-api / swagger-api/swagger-ui

$ref renders schema type as any instead of object in modular YAML config

Open
#10,157 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

Description

Q&A (please complete the following information)
Content & configuration

To reproduce, create a simple file structure in a Spring Boot project with application.yaml, users.yaml (OAS 3.1), and user.yaml (schema file).

application.yaml

springdoc:
  api-docs:
    version: openapi_3_1
  swagger-ui:
    urls:
      - name: users-v1
        url: openapis/users.yaml

resources/static/openapis/users.yaml (path from content root)

openapi: 3.1.0

# (...)

components:
  schemas:
    User:
      # Separate the schema specification into a separate file and reference it using $ref
      $ref: 'components/schemas/users/user.yaml#/components/schemas/User'

resources/static/openapis/components/schemas/users/user.yaml (path from content root)

components:
  schemas:
    User:
      # The type was specified in the separated schema file,
      # but it is not read during the initial rendering in Swagger UI 
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
        email:
          type: string
          format: email
        password:
          type: string
Describe the bug you're encountering

I'm developing an API server based on Spring Boot and providing OAS 3.1 with Springdoc. Instead of using annotations, I configured OAS 3.1 in YAML format. I successfully modularized the schema specifications by separating them into different files and using the $ref property.

However, as shown in the screenshot below, the initial type rendered in the Schema tab of Swagger UI appears as any. At first, I thought it might be because Any is the top-level type in Kotlin, but when toggling the schema, the any type changes to object. I suspect that before finding the file via the $ref property, the modularized schema is not read, resulting in the initial type being any.

When I first modularized, my intention was to set only the $ref property in the OAS file. However, due to the issue mentioned above, I'm temporarily setting the type property as well. I'm registering this issue to see if there is a nicer solution.

To reproduce...

Steps to reproduce the behavior:

  1. Run the Spring Boot application.
  2. Navigate to the default path {origin}:{port}/swagger-ui/index.html.
  3. Scroll down to the Schemas tab.
  4. Verify that the type in the schema defined with OAS 3.1 is set to any.
  5. Select 'Expand all'.
  6. Confirm that the type has changed to object, then select 'Collapse all' again.
  7. From this point on, the type will continue to be displayed as object.
Expected behavior

The expected result is that the specified schema type should be rendered as object, but the actual result is that it is rendered as any.

Screenshots
Initial Swagger UI rendering When selecting Expand All When selecting Collapse again
initial-rendering-user-schemas expand-user-schemas collapse-user-schemas
Additional context or thoughts

I initially registered this issue with springdoc-openapi. I was advised to seek feedback here since the issue appears to occur during UI rendering.

I confirmed that the issue persists with the following dependencies as well:

  • Kotlin 1.9 / Spring Boot 3.3.3
  • org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0 (Swagger UI 5.17.14)

I also found a temporary solution as shown below, but I'm not sure if I should continue using it this way.

resources/static/openapis/users.yaml (path from content root)

openapi: 3.1.0

# (...)

components:
  schemas:
    User:
      # Instead of specifying the type in the separated file,
      # it works if the type is specified at the same location as the $ref property.
      type: object
      $ref: 'components/schemas/users/user.yaml#/components/schemas/User'

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 application.yaml, users.yaml, and user.yaml from the report, then trace Swagger UI's initial schema rendering and its handling of the external $ref. Verify the fix by confirming the collapsed Schema view initially displays object and remains correct without expanding the schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, openapi
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.