[英]Message examples in OpenAPI 3 YAML specification
OpenAPI v3 offers using JSON or YAML format. OpenAPI v3 提供使用 JSON 或 YAML 格式。
I prefer using YAML to create API specifications .我更喜欢使用YAML 来创建 API 规范。 YAML's readability is much better for me as an API designer.作为 API 设计者,YAML 的可读性对我来说要好得多。
However, sometimes I need to embed a message example whose type is object .但是,有时我需要嵌入一个类型为 object的消息示例。 I usually have the examples in JSON format .我通常有JSON 格式的示例。 So I would do the following:所以我会做以下事情:
examples:
singlePet:
summary: Single pet
description: A request containing a single pet
value: |
{
"pets" : [
{
"petType" : "DOG",
"name" : "Ben"
}
]
}
However, such value is a string, not an object, whereas OpenAPI expects an object .然而,这样的值是一个字符串,而不是一个对象,而OpenAPI 需要一个对象。
Do we have any options to embed a JSON as an example value in the YAML specification?我们是否可以选择将 JSON 作为示例值嵌入到 YAML 规范中?
Currently, I convert the JSON to YAML and embed the YAML:目前,我将 JSON 转换为 YAML 并嵌入 YAML:
examples:
singlePet:
summary: Single pet
description: A request containing a single pet
value:
pets:
- petType: DOG
name: Ben
YAML is a superset of JSON. YAML 是 JSON 的超集。 This means you can use JSON syntax for objects and arrays within a YAML document.这意味着您可以对 YAML 文档中的对象和数组使用 JSON 语法。
In your first example, you can just remove the |
在您的第一个示例中,您只需删除|
after the value:
to keep the example value as an object.在value:
将示例值保留为对象。
The following are equivalent:以下是等价的:
examples:
singlePet:
summary: Single pet
description: A request containing a single pet
value:
{
"pets" : [
{
"petType" : "DOG",
"name" : "Ben"
}
]
}
examples:
singlePet:
summary: Single pet
description: A request containing a single pet
value:
pets:
- petType: DOG
name: Ben
Alternatively, you can useexternalValue
to point to an external file containing sample JSON.或者,您可以使用externalValue
指向包含示例 JSON 的外部文件。
examples:
singlePet:
summary: Single pet
description: A request containing a single pet
externalValue: 'https://api.example.com/docs/examples/pet.json'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.