Purpose: Add examples to yaml file so it will be exposed in Postman collection.
I'm trying to add 2 simple example but keep getting errors. The current error is: missed comma between flow collection entries.
Can you please assist?
swagger: "2.0" info: description: "Test" version: "1.4.4" title: "my-test" host: "somehost" basePath: "/somepath" schemes: - "https" tags: - name: Recommend description: helloWorld paths: /hello: post: description: "some description" operationId: "example-test" tags: ["example-test"] produces: "application/json" parameters: name: "myRequest" in: "body" description: "bla" required: true schema: $ref: "#/definitions/myRequest" examples: {ex1: $ref: "#/components/examples/test1", ex2: $ref: "#/components/examples/test2" } responses: "204": description: "All good" definitions: myRequest: type: object required: - name - lastname properties: name: type: string lastname: type: string components: examples: test1: value: name: test1 lastname: test1 test2: value: name: test2 lastname: test2
You are mixing up OpenAPI 2.0 and 3.0 syntax.
Multiple examples
are supported in OpenAPI 3.0 only. The correct syntax is:
openapi: 3.0.0
...
paths:
/hello:
post:
description: "some description"
operationId: "example-test"
tags: ["example-test"]
requestBody: # <------
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/myRequest"
examples: # <------
ex1:
$ref: "#/components/examples/test1"
ex2:
$ref: "#/components/examples/test2"
responses:
"204":
description: "All good"
components:
schemas:
myRequest:
...
examples:
test1:
value:
name: test1
lastname: test1
test2:
value:
name: test2
lastname: test2
If you use OpenAPI 2.0 ( swagger: '2.0
), you can only define a single example, and this example must be specified inline, it cannot be $ref
erenced.
swagger: "2.0"
...
paths:
/hello:
post:
description: "some description"
operationId: "example-test"
tags: ["example-test"]
produces:
- "application/json" # <---- Note the leading dash here
parameters:
- name: "myRequest" # <---- Note the leading dash here
in: "body"
description: "bla"
required: true
schema:
$ref: "#/definitions/myRequest"
responses:
"204":
description: "All good"
definitions:
myRequest:
type: object
required:
- name
- lastname
properties:
name:
type: string
lastname:
type: string
example: # <---- Example value for a schema
name: test1
lastname: test1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.