Swagger Edtior Post Request Example: 😱 Could not render n, see the console
Asked Answered
P

3

9

I'm having trouble rendering an example request body in openapi. I've distilled it down to this simple example:

openapi: 3.0.2
info:
  title: Test
  version: "1"
paths:
  /Users:
    post:
      requestBody:
        content:
          application/json:
            example: 
              name: "John"
              
      responses:
        "200":
          description: Fetches them
          content:
            application/json:
              example:
                - name: John Doe
                

https://editor.swagger.io/# and a few other tools can't seem to render the Request body. All I get is:

😱 Could not render n, see the console.

Although, the response body renders fine as expected.

What am I doing wrong here

Pyorrhea answered 27/7, 2020 at 7:9 Comment(1)
It's a Swagger UI bug caused by requestBody.content.application/json having no schema. Please open an issue at github.com/swagger-api/swagger-ui/issuesStaples
C
2

Exactly i am not sure why you want to add only example without schema, yes we can say its a kind of bug in swagger-ui,

See the console error in swagger editor,

swagger-editor-bundle.js:sourcemap:33 TypeError: Cannot read property 'toJS' of undefined
    at c (swagger-editor-bundle.js:sourcemap:33)
    at t.default (swagger-editor-bundle.js:sourcemap:33)
    at n.value (swagger-editor-bundle.js:sourcemap:33)
    at n.R.t.render (swagger-editor-bundle.js:sourcemap:33)
    at u._renderValidatedComponentWithoutOwnerOrContext (swagger-editor-bundle.js:sourcemap:100)
    at u._renderValidatedComponent (swagger-editor-bundle.js:sourcemap:100)
    at u.performInitialMount (swagger-editor-bundle.js:sourcemap:100)
    at u.mountComponent (swagger-editor-bundle.js:sourcemap:100)
    at Object.mountComponent (swagger-editor-bundle.js:sourcemap:13)
    at u.performInitialMount (swagger-editor-bundle.js:sourcemap:100)

This is strange its working when specify schema type in requestBody, it will help you for temporary fix, like this,

requestBody:
  content:
    application/json:
      schema:
        type: object
      example: 
        name: "John"

For more details Media Type Specification

Canales answered 27/7, 2020 at 9:54 Comment(2)
Thanks, this gets me over the line, although I don't understand why the schema is required to display the example, I assume its a bugPyorrhea
Yes i have updated answer, you are right, i reviewed these kind of errors in swagger ui github today, realized this is a kind of bug in swagger-ui.Canales
P
0

I got it resolved as there was some extra space in my openapi.yml. I removed that space and got it resolved

Plated answered 3/3, 2023 at 1:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Dahl
S
0

I encountered the same issue because there was a mistake in the request body parameters. Here's the corrected version:

put:
  tags:
    - products
  ...
  requestBody:
    required: true
    content:
      application/json:
        schema:
          $ref: "./objects/body/product.yaml"
  responses:
    ...

The mistake in the product object was identified as follows:

type: object
properties:
  name:
    type: string
  description:
    type: string
  type:
    $ref: "./product.yaml"
  country_id:
    type: string
  brand: string  # The issue was here
  is_company_product:
    type: string

required:
  - name

Upon correction:

brand:
  type: string

With this correction, everything functioned perfectly.

Spark answered 3/5, 2024 at 14:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.