swagger-api / swagger-api/swagger-ui

Nested/recursive schemas not showing correctly in documentation

Open
#7,506 0 comments 9 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

Example Swagger/OpenAPI definition:

openapi: 3.0.2
info:
  title: FastAPI
  version: 0.1.0
paths:
  /:
    post:
      summary: Root
      operationId: root__post
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/NodeInfo'
        required: true
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema: {}
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
components:
  schemas:
    ConnectionInfo:
      title: ConnectionInfo
      required:
        - id
        - values
      type: object
      properties:
        id:
          title: Id
          type: string
        values:
          title: Values
          anyOf:
            - type: array
              items:
                anyOf:
                  - $ref: '#/components/schemas/ValueInfo'
                  - $ref: '#/components/schemas/NodeInfo'
            - $ref: '#/components/schemas/ValueInfo'
            - $ref: '#/components/schemas/NodeInfo'
    HTTPValidationError:
      title: HTTPValidationError
      type: object
      properties:
        detail:
          title: Detail
          type: array
          items:
            $ref: '#/components/schemas/ValidationError'
    NodeInfo:
      title: NodeInfo
      type: object
      properties:
        connections:
          title: Connections
          type: array
          items:
            $ref: '#/components/schemas/ConnectionInfo'
    ValidationError:
      title: ValidationError
      required:
        - loc
        - msg
        - type
      type: object
      properties:
        loc:
          title: Location
          type: array
          items:
            type: string
        msg:
          title: Message
          type: string
        type:
          title: Error Type
          type: string
    ValueInfo:
      title: ValueInfo
      required:
        - value
      type: object
      properties:
        value:
          title: Value
          anyOf:
            - type: string
            - type: number
            - type: integer
            - type: boolean

Describe the bug you're encountering

I am using Python and FastAPI to generate an API, and I noticed that the Swagger UI documentation of the API had some errors when showing nested recursive schemas. While debugging this issue, I tried taking the OpenAPI spec generated by FastAPI and running it at Swagger Editor, and the same issue appeared there despite the OpenAPI spec looking correct (to me, at least). This leads me to believe the issue is a Swagger UI problem. The above YAML file is a simplified/genericized version of a specification that exhibits the same issue.

To reproduce...

Steps to reproduce the behavior:

  1. Go to Swagger Editor
  2. Paste the above YAML to generate the documentation.
  3. Inspect the ConnectionInfo Schema.
    • Expand the Values object inside it.
    • Expand the anyOf list inside that.
    • This should result in the first screenshot in the screenshots section below. Note that the intended type of Values is anyOf(NodeInfo ValueInfo, list(anyOf(NodeInfo, ValueInfo))), and that's exactly what it shows.
  4. Inspect the NodeInfo schema.
    • Expand the Connections object inside it.
    • Expand the ConnectionInfo object inside that.
    • Expand the Values object inside that.
    • Expand the anyOf list inside that.
    • This should result in the second screenshot in the screenshots section below. Note that the types shown do not match those observed in Step 3 above- the NodeInfo blocks are not represented correctly.
  5. Open and inspect the single API endpoint documentation.
    • Open the Schema representation of the request.
    • Expand the NodeInfo object.
    • Expand the Connections object inside it.
    • Expand the ConnectionInfo object inside that.
    • Expand the Values object inside that.
    • Expand the anyOf list inside that.
    • Note that the types shown are incorrect here too in the same way that it was incorrect in Step 4 above.
Expected behavior

I would expect that the representation seen in Step 3 above would appear in the representations in Step 4 and 5. Alternatively, because it is recursive, I would be happy with just showing the name NodeInfo without the ability to expand it so it is clear that NodeInfo is an acceptable type instead of the empty object.

Screenshots

The schema representation of ConnectionInfo seen during Step 3 above. The fields are correctly typed.
image

The schema representation of NodeInfo seen during Step 4 (which is effectively the same as during Step 5). The fields are incorrectly typed.
image

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 in Swagger Editor using the supplied OpenAPI YAML, following the ConnectionInfo and recursive NodeInfo expansion steps. Compare the schema representations for direct and nested recursive references, including the request schema. Done means recursive NodeInfo references remain correctly identified instead of appearing as incorrectly typed or empty objects.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, openapi
Domain
documentation, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.