swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Field name "entries" in a schema causes swagger-ui javascript error and fails to load

Open mke1ms opened this issue 6 years ago • 9 comments

  • OS: ubuntu
  • Browser: chrome, firefox
  • Method of installation: python fastapi
  • Swagger-UI version: https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js
  • Swagger/OpenAPI version: OpenAPI 3.0

Problem

The openapi definition below has a schema with a field name called "entries". This causes the ui to throw a javascript error "e.entries is not a function" (see the screenshot below).

This behavior only started Friday May 15th 2020 at around 15:00 CET. Before that swagger-ui had no problems loading a schema containing a field called entries, so I am assuming something changed in the bundle I use from the url above.

Example Swagger/OpenAPI definition:

{
  "openapi": "3.0.2",
  "info": {
    "title": "FastAPI",
    "version": "0.1.0"
  },
  "paths": {
    "/test/": {
      "get": {
        "summary": "Test",
        "operationId": "test_test__get",
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Testmodel"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Testmodel": {
        "title": "Testmodel",
        "required": [
          "name",
          "entries"
        ],
        "type": "object",
        "properties": {
          "name": {
            "title": "Name",
            "type": "string"
          },
          "entries": {
            "title": "Entries",
            "type": "integer"
          }
        }
      }
    }
  }
}

Screenshot

image

To reproduce...

Try to load swagger ui using the openapi.json above

mke1ms avatar May 17 '20 13:05 mke1ms

Specifying version 3.25.2 (I'm using unpkg, I assume jsdelivr.net supports that too) fixes the problem.

provegard avatar May 18 '20 08:05 provegard

Actually I have the same error:

When I type the field name entries, I received a message saying: "Unable to render this definition".

paths:
  /pet:
    post:
      summary: "Add a new pet to the store"
      description: ""
      parameters:
      - in: "body"
        name: "body"
        schema:
          $ref: "#/definitions/Pet"
      responses:
        "405":
          description: "Invalid input"
definitions:
  Pet:
    type: "object"
    properties:
      id:
        type: "integer"
      entries:
        type: "array"
        items:
          type: "string"
Screen Shot 2020-05-18 at 17 14 21

I need to write entries as a field since my API requires this name.

If I change entries to entry, it works as expected.

paths:
  /pet:
    post:
      summary: "Add a new pet to the store"
      description: ""
      parameters:
      - in: "body"
        name: "body"
        schema:
          $ref: "#/definitions/Pet"
      responses:
        "405":
          description: "Invalid input"
definitions:
  Pet:
    type: "object"
    properties:
      id:
        type: "integer"
      entry:
        type: "array"
        items:
          type: "string"

Can someone help me with this problem, why it fails when I use entries as a field name?

jmmolina796 avatar May 18 '20 22:05 jmmolina796

I have the same problem with Version 3.23.3

vivvooren avatar May 19 '20 06:05 vivvooren

We found the issue is likely related to this change, where swagger fragments with an entries field are interpreted as a FormData object and swagger-ui attempts to invoke entries().

  • https://github.com/swagger-api/swagger-ui/blame/96c7b4cbf5e7208e74e18a6cf2d9a0260756914b/src/core/utils.js#L100-L106

Exiting early when entries is not a function should resolve this.

  • https://github.com/swagger-api/swagger-ui/blame/96c7b4cbf5e7208e74e18a6cf2d9a0260756914b/src/core/utils.js#L83
  • https://github.com/swagger-api/swagger-ui/blame/96c7b4cbf5e7208e74e18a6cf2d9a0260756914b/src/core/utils.js#L100

stiliyan avatar May 19 '20 17:05 stiliyan

Looks like this is caused by #5999

jlsjonas avatar May 20 '20 11:05 jlsjonas

@mke1ms @jlsjonas @stiliyan @jmmolina796 Thanks for reporting with details and suggestions! A fix is incoming and will be included in the next release.

tim-lai avatar May 20 '20 17:05 tim-lai

I have a question about this change. The change has been merged but the error still occurs when using petstore. Is there another repository that needs to adopt swagger before using 'entries' as a parameter works when rendering?

RSkorina avatar May 20 '20 20:05 RSkorina

@RSkorina A new release of SwaggerUI will make this change visible on petstore.swagger.io

tim-lai avatar May 20 '20 21:05 tim-lai

Your API definition is missing the OpenAPI/Swagger version number, in this case "swagger": "2.0" . Add it at the beginning, like so:

{ "swagger": "2.0", "title" : "Music API Documentation", ...

this worked for me, details on https://copyprogramming.com/howto/swagger-unable-to-render-this-definition-the-provided-definition-does-not-specify-a-valid-version-field?utm_content=cmp-true

katsisaac50 avatar Mar 14 '23 16:03 katsisaac50