swagger-ui: Path parameter issues: "Required field is not provided" or interpolates to a ','

Q&A

  • OS: Windows Sub-system for Linux (WSL): Ubuntu 18.04.3 LTS
$ cat /etc/os-release | head -2
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
  • Browser: Chrome 79.0.3945.88 & Firefox 68.4.0esr
  • Method of installation:
$ npm -v
6.13.4
  • Swagger-UI version: 3.24.3
  • Swagger/OpenAPI version: OpenAPI 3.0.1

Content & configuration

Example Swagger/OpenAPI definition: Stripped down version of PetStore example in OpenAPI 3.0.1

{
    "openapi": "3.0.1",
    "info": {
        "description": "This is a sample server Petstore server.  You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the api key `special-key` to test the authorization filters.",
        "version": "2.0.0",
        "title": "Swagger Petstore",
        "termsOfService": "http://swagger.io/terms/",
        "contact": {
            "email": "apiteam@swagger.io"
        },
        "license": {
            "name": "Apache 2.0",
            "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
        }
    },
    "servers": [
        {
            "url": "https://petstore.swagger.io/v2"
        }
    ],
    "tags": [
        {
            "name": "pet",
            "description": "Everything about your Pets",
            "externalDocs": {
                "description": "Find out more",
                "url": "http://swagger.io"
            }
        }
    ],
    "paths": {
        "/pet/{petId}": {
            "get": {
                "tags": [
                    "pet"
                ],
                "summary": "Find pet by ID",
                "description": "Returns a single pet",
                "operationId": "getPetById",
                "parameters": [
                    {
                        "name": "petId",
                        "in": "path",
                        "description": "ID of pet to return",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Pet"
                                }
                            },
                            "application/xml": {
                                "schema": {
                                    "$ref": "#/components/schemas/Pet"
                                }
                            }
                        }
                    },
                    "400": {
                        "description": "Invalid ID supplied"
                    },
                    "404": {
                        "description": "Pet not found"
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Category": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer",
                        "format": "int64"
                    },
                    "name": {
                        "type": "string"
                    }
                },
                "xml": {
                    "name": "Category"
                }
            },
            "Pet": {
                "type": "object",
                "required": [
                    "name",
                    "photoUrls"
                ],
                "properties": {
                    "id": {
                        "type": "integer",
                        "format": "int64"
                    },
                    "category": {
                        "$ref": "#/components/schemas/Category"
                    },
                    "name": {
                        "type": "string",
                        "example": "doggie"
                    },
                    "photoUrls": {
                        "type": "array",
                        "xml": {
                            "wrapped": true
                        },
                        "items": {
                            "type": "string",
                            "xml": {
                                "name": "photoUrl"
                            }
                        }
                    },
                    "tags": {
                        "type": "array",
                        "xml": {
                            "wrapped": true
                        },
                        "items": {
                            "xml": {
                                "name": "tag"
                            },
                            "$ref": "#/components/schemas/Tag"
                        }
                    },
                    "status": {
                        "type": "string",
                        "description": "pet status in the store",
                        "enum": [
                            "available",
                            "pending",
                            "sold"
                        ]
                    }
                },
                "xml": {
                    "name": "Pet"
                }
            },
            "Tag": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer",
                        "format": "int64"
                    },
                    "name": {
                        "type": "string"
                    }
                },
                "xml": {
                    "name": "Tag"
                }
            }
        }
    }
}

Swagger-UI configuration options:

    SwaggerUI({
      docExpansion: 'list',
      domNode: document.getElementById(config.swaggerElementID),
      spec: swaggerJsonObject,
    });

Your query string config: N/A

Describe the bug you’re encountering

When the path parameter required property is set to true, all input is rejected with error: Required field is not provided (screenshot 1).

If I set the path parameter required property to false, the path parameter interpolates to a ‘,’ (screenshot 2). I’ve tried defining the path parameter inline as well as in the components section and various other things, but always get the comma.

Example:

Curl

curl -X GET "https://petstore.swagger.io/v2/pet/," -H "accept: */*"

Request

https://petstore.swagger.io/v2/pet/,

Response body

{
  "code": 404,
  "type": "unknown",
  "message": "java.lang.NumberFormatException: For input string: \",\""
}

To reproduce…

Steps to reproduce the behavior:

  1. npm run-script start, which executes react-scripts start
  2. Click on ‘Try it now’
  3. Enter an ID
  4. If the path parameter required property is set to true, see error: Required field is not provided, else see server response HTTP 404 Not Found for https://petstore.swagger.io/v2/pet/,

Expected behavior

The path parameter should interpolate properly regardless of whether or not it is required.

Screenshots

Path parameter required property set to true

image

Path parameter required property set to false

image

Additional context or thoughts

$ node -v
v12.14.0

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 7
  • Comments: 30 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I have the sme issue. In my case with de headers. This is my header filter: headerRequiredTrue

And then… image

Problem solved! In my case, I change “String” for “string”, and now it’s works!

@domdomegg @hkosova This problem occur when swagger-ui and its dependency react-debounce-input use different versions of react. This happens because react-debounce-input has "react": "^15.3.0 || ^16.0.0", so if your project use react v16, it will use it too, but swagger-ui will use always v15. In that case, for some reason, setState’s callback in react-debounce-input is not working, so onChange prop is not firing. So swagger-ui-dist works, because it has all dependencies in one file and forces react-debounce-input to use react v15.

For example, if you have react 16 in your project, then your node_modules will look like this:

-node_modules └ react(16) └ react-debounce-input(3.2.2) <- will use project react version └ swagger-ui …└ node_modules …└ react(15)

In this case, the problem occur, to fix this, you can force react-debounce-input to be installed in swagger-ui node_modules by installing older version of react-debounce-input, like this:

npm i --save-dev react-debounce-input@3.1.0

-node_modules └ react(16) └ react-debounce-input(3.1.0) └ swagger-ui …└ node_modules …└ react(15) …└ react-debounce-input(3.2.2) <- will use the same react version as swagger-ui

the proper solution would be to change somehow react-debounce-input dependency to "react": "^15.3.0"

swagger-ui-react doesn’t help, its forced to use v16, but has a lot of deprecated stuff and i have a lot of warnings in console

Can confirm that both swagger-ui and swagger-ui-react have this issue with the onChange events not triggering (and therefore never detecting input and required never clearing) when loaded in a React 16 project. I think it’s high time this project is switched to React 16.

In fact, this isn’t unique to path parameters or OpenAPI, and actually breaks all free-text string parameters. Not sure what has changed recently (maybe just more people using React) to mean this was only discovered now, cause it looks like it’s been a problem for a while. I’ve checked, and it is still a problem in 3.25.0.

This and #1499 are caused by the onChange method of DebounceInput in JsonSchema_string not firing.

I’d be up for trying to fix this but am not sure how to build in the way that swagger-ui is (npm run build builds in the way swagger-ui-dist is, which does not have this bug).

What does work as a bit of a gross workaround for now is using the swagger-ui-dist package instead. The README warns this will result in more data having to be transferred - which is true, but it’s pretty marginal. In a very simple test case this changed the page size from 577 KB to 642 KB.

I’ve expanded a bit on @tlindsay42’s example repo at https://github.com/domdomegg/swagger-ui-5778, which also shows the swagger-ui-dist workaround working (checkout https://github.com/domdomegg/swagger-ui-5778/commit/b511fd2a17eb8ca870f040cf0e57013f199317eb to see the buggy version).

Hello?

Changing the type from “String” to “string” worked for me. https://swagger.io/docs/specification/data-models/data-types/

Try to remove the "type":"integer" for the path parameter(setting to string also works). None of the examples at official docs have a type attribute for the path parameter

This helped me to solve an example below. Example of not working .yml file:

Return a user key information based on his name
---
tags:
  - user
parameters:
  - name: id
    in: path
    required: true
    type: integer # <--- had to get rid of this to make it work
    description: the id of the user to get
responses:
  200:
    description: The user's information was successfully retrieved
    schema:
      example:
        user:
          last_name: Doe
          first_name: John
          age: 30