swagger-api / swagger-api/swagger-ui

Impossible to omit an optional JSON request body (POST)

Open
#7,690 0 comments 1 reaction 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
  • OS: macOS
  • Browser: Firefox (or any)
  • Version: 94.0.2
  • Method of installation: npm/yarn
  • Swagger-UI version: 3.18.2
  • Swagger/OpenAPI version: Swagger 2.0
Content & configuration

Screen Shot 2021-12-02 at 2 20 39 PM

Example Swagger/OpenAPI definition:

    "/api/users/{userId}/greetings" : {
      "post" : {
        "tags" : [
          "Users"
        ],
        "summary" : "Greets a user",
        "operationId" : "greetUsingPOST",
        "consumes" : [
          "application/json"
        ],
        "produces" : [
          "application/json"
        ],
        "parameters" : [
          {
            "name" : "userId",
            "in" : "path",
            "description" : "userId",
            "required" : true,
            "type" : "string"
          },
          {
            "in" : "body",
            "name" : "greeting",
            "required" : false,
            "schema" : {
              "$ref" : "#/definitions/Greeting"
            }
          }
        ],
        ...
      }

Swagger-UI configuration options:

            SwaggerUi({
                spec,
                domNode: el,
                swagger: "2.0",
                validatorUrl: null,
                docExpansion: "list",
                operationsSorter: compareOps,
                defaultModelExpandDepth: 5,
                defaultModelsExpandDepth: -1,
            })
Describe the bug you're encountering

I’d like to send a POST request with no request body. The request body is defined as optional, not required. The request body text field is prefilled, however, and whenever I delete the prefilled JSON, code in the page immediately inserts {}.

To reproduce...

Steps to reproduce the behavior:

  1. Define a POST operation that takes an optional JSON request body.
  2. Try to send a test request using swagger-ui that includes no request body.
Expected behavior

If I delete the JSON in the request body field and click “Execute”, then a POST request should be sent with no request body.

Screenshots

See above.

Additional context or thoughts

Thanks in advance for fixing! 😄

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 UI 3.18.2 with a Swagger 2.0 POST operation whose JSON body parameter is optional, then trace the request-body editor behavior after its prefilled JSON is deleted. Done means clicking Execute sends the request without a body instead of inserting {}; add or update coverage for this scenario if the existing test structure provides a suitable place.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.