Skip to content

feat: add toggle to omit optional request body in Try It Out#5765

Open
IsuruDilanga wants to merge 1 commit intoswagger-api:mainfrom
IsuruDilanga:ft/4807-optional-request-body-toggle
Open

feat: add toggle to omit optional request body in Try It Out#5765
IsuruDilanga wants to merge 1 commit intoswagger-api:mainfrom
IsuruDilanga:ft/4807-optional-request-body-toggle

Conversation

@IsuruDilanga
Copy link
Copy Markdown

Summary

This PR does the following:

  • Adds a RequestBodyWrapper component that wraps swagger-ui's
    RequestBody component via wrapComponents
  • Shows a "Send request body" checkbox in Try It Out mode
    when requestBody.required is false or not set
  • When unchecked, no body and no Content-Type header is sent
    in the actual HTTP request
  • When checked (default), existing behavior is fully preserved

Closes #4807

Behavior

Scenario Checkbox visible Result
required: false + checkbox checked (default) ✅ Yes Body sent normally — no change for existing users
required: false + checkbox unchecked ✅ Yes No body, no Content-Type header sent
required: true ❌ No Checkbox never appears — existing behavior unchanged

Files changed

  • src/plugins/editor-preview-swagger-ui/components/RequestBodyWrapper.jsx ← new file
  • src/plugins/editor-preview-swagger-ui/index.js ← registers RequestBody wrapComponent

How to test

Setup

Paste the following YAML into the editor:

openapi: 3.1.0
info:
  title: One game API
  version: "1.0"
servers:
  - url: http://127.0.0.1:8000
    description: Local development server
paths:
  /v2/auth:
    post:
      summary: Create an authorization token (optional body)
      operationId: createAuthV2
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                email:
                  type: string
        required: false
      responses:
        "200":
          description: Authorization token created
  /v2/auth/required:
    post:
      summary: Create an authorization token (required body)
      operationId: createAuthV2Required
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                email:
                  type: string
        required: true
      responses:
        "200":
          description: Authorization token created

Test 1 — Optional body, checkbox unchecked (main feature)

  1. Click Try it out on POST /v2/auth
  2. Confirm the "Send request body" checkbox appears ✅
  3. Uncheck the checkbox → body textarea disappears
  4. Click Execute
  5. Open DevTools → Network tab → click the request
  6. Go to Headers → confirm no Content-Type: application/json
  7. Confirm Request Payload section does not appear

Test 2 — Optional body, checkbox checked (default behavior preserved)

  1. Click Try it out on POST /v2/auth
  2. Leave checkbox checked (default)
  3. Click Execute
  4. Open DevTools → Network tab → click the request
  5. Confirm Content-Type: application/json header is present
  6. Confirm Request Payload section shows the JSON body

Test 3 — Required body (no regression)

  1. Click Try it out on POST /v2/auth/required
  2. Confirm no checkbox appears — only the body textarea ✅
  3. Existing behavior is completely unchanged ✅

Adds a 'Send request body' checkbox that appears in Try It Out
mode when requestBody.required is false or not set.

When unchecked:
- Body textarea is hidden
- No body is sent in the actual HTTP request
- No Content-Type header is added

When checked (default):
- Existing behavior is preserved exactly

Fixes swagger-api#4807
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a way to optionally not send a request body if optional

1 participant