简体   繁体   English

OpenAPI 3 YAML 规范中的消息示例

[英]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.

相关问题 如何从 OpenAPI 3.0 yaml 文件生成 JSON 示例? - How to generate JSON examples from OpenAPI 3.0 yaml file? OpenApi 是否有通用文件规范? - Is there a general file specification for OpenApi? 根据给定的OpenApi / Swagger规范验证请求有效负载和响应 - Validate request payloads and responses against a given OpenApi/Swagger specification 如何从 OpenAPI/Swagger 模型定义生成 JSON 示例? - How to generate JSON examples from OpenAPI/Swagger model definition? yaml 或 json 格式的 OpenAPI 规范并解析为 c# - OpenAPI spec in yaml or json format and parse in c# 如何将基于 strongloop 的 api 导入 json 或 yaml 规范 - how to import strongloop based api into json or yaml specification 如何告诉 springdoc-openapi-maven-plugin 生成 YAML 而不是 JSON? - How to tell springdoc-openapi-maven-plugin to generate YAML instead of JSON? YAML/OpenAPI:用值中有两个字段(整数和字符串)的值定义键值对 - YAML/OpenAPI: Define key value pair with value having two fields in value (integer and string) 我如何解释用 JSON 或 JavaScript 编写的配置文档示例并知道如何将其转换为 YAML? - How can I interpret config documentation examples written in JSON or JavaScript and know how it would translate to YAML? 如何使用 Swagger 2.0 或 OpenApi 3.x 在 API 对象中指定“JSON 原始消息”? - How do I specify a "JSON Raw Message" in API object using Swagger 2.0 or OpenApi 3.x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM