【问题标题】:Failing to add examples to YAML file无法将示例添加到 YAML 文件
【发布时间】:2020-05-21 08:24:43
【问题描述】:

目的:将示例添加到 yaml 文件中,以便在 Postman 集合中公开。

我正在尝试添加 2 个简单示例,但不断出现错误。当前错误是:流集合条目之间缺少逗号。

你能帮忙吗?

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

    
    
    
  

【问题讨论】:

    标签: yaml openapi swagger-2.0


    【解决方案1】:

    您混淆了 OpenAPI 2.0 和 3.0 语法。

    仅 OpenAPI 3.0 支持多个 examples。正确的语法是:

    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
    


    如果使用 OpenAPI 2.0 (swagger: '2.0),则只能定义单个示例,并且此示例必须内联指定,不能为 $referenced。

    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
    

    【讨论】:

    • 非常感谢您的澄清。所以基本上如果我想为我的 2.0 语法添加几个响应(例如 204 和 200) - 我可以这样做吗?还是必须更改为 3.0?
    • 200 和 204 是不同的状态码,因此可以在 responses 部分单独定义。有关示例,请参阅 swagger.io/docs/specification/2-0/describing-responses
    猜你喜欢
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2021-06-22
    • 2021-10-15
    • 2015-02-28
    • 1970-01-01
    • 2022-12-13
    • 2020-08-09
    相关资源
    最近更新 更多