Skip to content

Response model expand/collapse does not work on initial render with OAS 3.1 #10628

@scottohara

Description

@scottohara

Q&A (please complete the following information)

  • OS: macOS
  • Browser: chrome
  • Version: 142
  • Method of installation: npm
  • Swagger-UI version: 5.30.2
  • Swagger/OpenAPI version: OpenAPI 3.1

Content & configuration

Example Swagger/OpenAPI definition:

{
  "openapi": "3.1.0",
  "info": {
    "version": "1.0.0",
    "title": "Swagger Petstore",
    "license": {
      "name": "MIT",
      "url": "https://opensource.org/licenses/MIT"
    }
  },
  "servers": [
    {
      "url": "http://petstore.swagger.io/v1"
    }
  ],
  "paths": {
    "/pets": {
      "get": {
        "summary": "List all pets",
        "operationId": "listPets",
        "tags": ["pets"],
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "description": "How many items to return at one time (max 100)",
            "required": false,
            "schema": {
              "type": "integer",
              "format": "int32"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "A paged array of pets",
            "headers": {
              "x-next": {
                "description": "A link to the next page of responses",
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pets"
                }
              }
            }
          },
          "default": {
            "description": "unexpected error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
		}
  },
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "required": ["id", "name"],
        "properties": {
          "id": {
            "type": "integer",
            "format": "int64"
          },
          "name": {
            "type": "string"
          },
          "tag": {
            "type": "string"
          }
        }
      },
      "Pets": {
        "type": "array",
        "items": {
          "$ref": "#/components/schemas/Pet"
        }
      },
      "Error": {
        "type": "object",
        "required": ["code", "message"],
        "properties": {
          "code": {
            "type": "integer",
            "format": "int32"
          },
          "message": {
            "type": "string"
          }
        }
      }
    }
  }
}

Swagger-UI configuration options:

SwaggerUI({
  defaultModelRendering: "model"
})

Describe the bug you're encountering

To reproduce...

Steps to reproduce the behavior:

  1. Render any Open API 3.1 spec with defaultModelRendering: "model" configuration
  2. Click on an operation to expand it and show the Responses section
  3. In the "Schema" section, click on any part of the model that should expand/collapse
  4. Observe that clicking does nothing
  5. Click "Example Value" to toggle to the example view
  6. Click "Schema" to toggle back to the schema view
  7. Observe that expanding/collapsing the model now works

Expected behavior

When the "Schema" view is rendered by default (defaultModelRendering: "model"), the expand/collapse should work

Additional context or thoughts

The bug only seems to present itself when using OAS 3.1. Using OAS 3.0 or earlier does not exhibit the same problem.

I suspect that the problem could be here:

useEffect(() => {
pathMutator(
parentState === JSONSchemaIsExpandedState.DeeplyExpanded
? JSONSchemaIsExpandedState.DeeplyExpanded
: isExpandedState
)
}, [parentState])

This effect depends on pathMutator, parentState and isExpandedState, but the dependencies array only includes [parentState]. This means that the initial value of isExpandedState is captured when the effect runs on initial render, but the effect won't re-run if that value later changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions