swagger-api / swagger-api/swagger-ui

OAS2: Cannot render a primitive body parameter with an integer example

Open
#7,990 0 comments 2 reactions 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 (please complete the following information)
  • OS: Windows 8.1
  • Browser: Chrome (latest)
  • Method of installation: https://editor.swagger.io
  • Swagger-UI version: 4.10.3
  • Swagger/OpenAPI version: OpenAPI 2.0 (no issue with 3.0)
Content & configuration

Example Swagger/OpenAPI definition:

swagger: '2.0'
info:
  title: Primitive body example
  version: 1.0.0
host: httpbin.org
schemes: [https]
consumes:
  - application/json
paths:
  /post:
    post:
      parameters:
        - in: body
          name: foo
          required: true
          schema:
            type: integer
            example: 1
      responses:
        200:
          description: ok
Describe the bug you're encountering

This issue was originally reporter on Stack Overflow.

If an operation in an OAS2 file has a primitive body parameter (in this case, integer) with an example defined, Swagger UI fails to render this body parameter. The console error is:

Error: Expected `string` for value, got `1`
    at t (index.js:29:12)
    at Object.a [as highlight] (core.js:39:11)
    at L.astGenerator (highlight.js:296:27)
    at ru (highlight.js:399:20)
    at sa (react-dom.production.min.js:157:137)
    at Gs (react-dom.production.min.js:267:460)
    at Pl (react-dom.production.min.js:250:347)
    at Ol (react-dom.production.min.js:250:278)
    at Cl (react-dom.production.min.js:250:138)
    at bl (react-dom.production.min.js:243:163) 

If you comment/remove the example value, the body parameter example is rendered as {}, which is incorrect since it's type: integer.

A similar OAS3 definition is rendered correctly:

openapi: 3.0.3
info:
  title: Primitive body example
  version: 1.0.0
servers:
- url: https://httpbin.org
paths:
  /post:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: integer
              example: 1
        required: true
      responses:
        '200':
          description: ok

To reproduce...
  1. Go to https://editor.swagger.io.
  2. Paste the OAS2 definition from the "Content & configuration" section.
  3. Open the browser console.
  4. In Swagger UI, expand the operation.

Actual behavior:

😱 Could not render r, see the console.

Expected behavior: The body parameter and its example value (1) should be rendered correctly.

Additional context or thoughts

n/a

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

Start by loading the provided OpenAPI 2.0 definition in Swagger UI and inspect the browser-console error when expanding the operation. Compare the primitive integer body parameter with the working OpenAPI 3.0 example; done means the OAS2 parameter renders with the example value 1 and without the reported error.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.