Reputation: 1888
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
Upvotes: 9
Views: 8881
Reputation: 8388
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.
Upvotes: 0
Reputation: 1
I got it resolved as there was some extra space in my openapi.yml. I removed that space and got it resolved
Upvotes: 0
Reputation: 36094
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
Upvotes: 2