我正在使用在线Swagger Editor为我的API创建一个Swagger规范。
我的API有一个GET请求端点,我使用以下YAML代码来描述输入参数:
paths:
/fooBar:
get:
tags:
- foobar
summary: ''
description: ''
operationId: foobar
consumes:
- application/x-www-form-urlencoded
produces:
- application/json
parameters:
- name: address
in: query
description: Address to be foobared
required: true
type: string
example: 123, FakeStreet
- name: city
in: query
description: City of the Address
required: true
type: string
example: New York
如果我输入example
标签,我会收到错误消息:
不是<#/ definitions / parameter>,<#/ definitions / jsonReference>中的一个
如何在Swagger中编写GET参数时设置示例?
OpenAPI / Swagger 2.0没有非身体参数的example
关键字。您可以在参数description
中指定示例。一些工具,如Swagger UI v2,v3.12 +和Dredd也支持x-example
扩展属性用于此目的:
parameters:
- name: address
in: query
description: Address to be foobared. Example: `123, FakeStreet`. # <-----
required: true
type: string
x-example: 123, FakeStreet # <-----
OpenAPI 3.0本身支持参数示例:
parameters:
- name: address
in: query
description: Address to be foobared
required: true
schema:
type: string
example: 123, FakeStreet # <----
example: 456, AnotherStreet # Overrides schema-level example