swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.56k stars 8.96k forks source link

Erroneous errors in latest Swagger UI version #9208

Closed chuck-flowers closed 1 year ago

chuck-flowers commented 1 year ago

Q&A (please complete the following information)

Content & configuration

Example Swagger/OpenAPI definition (redacted):

{
  "openapi": "3.0.0",
  "info": {
    "title": "ACME API",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://localhost:8080"
    }
  ],
  "security": [
    {
      "BasicAuth": []
    }
  ],
  "paths": {
    "/v1/customer/group/{foo}/pieces/completedAt": {
      "parameters": [],
      "get": {
        "operationId": "foo",
        "parameters": [
          {
            "name": "foo",
            "in": "path",
            "schema": {
              "type": "string"
            },
            "required": true
          },
          {
            "name": "start",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date-time"
            },
            "required": true
          },
          {
            "name": "end",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date-time"
            },
            "required": true
          },
          {
            "name": "xService",
            "in": "query",
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "default": []
            },
            "required": false
          }
        ],
        "responses": {
          "200": {
            "description": "TODO",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/v1P"
                  }
                }
              }
            }
          },
          "400": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/v1PD"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BasicAuth": {
        "type": "http",
        "scheme": "basic"
      }
    },
    "schemas": {
      "v1P": {
        "type": "object",
        "properties": {
        },
        "additionalProperties": false
      },
      "v1PD": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "description": "A URI reference that identifies the problem type. "
          },
          "title": {
            "type": "string",
            "description": "A short, human-readable summary of the problem type."
          },
          "status": {
            "type": "integer",
            "description": "The HTTP status code generated by the origin server for this occurrence of the problem."
          },
          "detail": {
            "type": "string",
            "description": "A human-readable explanation specific to this occurrence of the problem."
          },
          "instance": {
            "type": "string",
            "description": "A URI reference that identifies the specific occurrence of the problem."
          }
        },
        "required": [
          "title"
        ],
        "additionalProperties": false,
        "description": "The problem details data modeled defined at https://datatracker.ietf.org/doc/html/rfc7807#section-3.1 "
      }
    }
  }
}

Swagger-UI configuration options:

SwaggerUI({
   url: '/docs/openapi.json'
})
Not sure what query string config means

Describe the bug you're encountering

Erroneous error messages are showing up at the the top of the documenation page. This happens in 5.7.0 but not in 5.6.2.

To reproduce...

Steps to reproduce the behavior:

  1. Go to the Swagger UI page
  2. Click the endpoint documenation

Expected behavior

No errors should appear

Screenshots

image image

Additional context or thoughts

I'm having trouble tracing where the error is happening based on the browser console but based on the stack trace it seems to be originating from the @swagger-api/apidom-reference in the url.mjs module

minizwergi commented 1 year ago

There might be an issue with the latest update of the swagger-client and related code in /core/index.js https://github.com/swagger-api/swagger-ui/commit/d89726a24b88d7f5044da61309a6074965bb7a5d

char0n commented 1 year ago

Hi everybody,

The regression will most likely be in swagger-client which was recently heavily refactored. Specifically we've changed how URLs are resolved. Instead of legacy and deprecated url we now use https://url.spec.whatwg.org/.

Let me investigate what went wrong and why.

char0n commented 1 year ago

I can reproduce by passing spec option directly to SwaggerUI.

SwaggerUI({
   spec: {...},
})
char0n commented 1 year ago

Addressed in https://github.com/swagger-api/swagger-ui/pull/9214.

Explanation: if we pass spec option without url option, or if we pass a relative URI Reference as a value of url option, we make it absolute just before the resolution. This is aligned with OpenAPI and JSON Schema specs.

Before we were running resolution on relative URI References, which was just wrong.

char0n commented 1 year ago

Resolved in https://github.com/swagger-api/swagger-ui/releases/tag/v5.7.1