api - 在 swagger 2.0 (openapi) 中,如何在 POST 和 PUT 请求之间有不同的资源定义?

标签 api inheritance swagger openapi

如果我们看一个消息 API(例如)
我希望它能够

  • 创建消息
  • 留言
  • 更新留言

  • 消息包含外部引用(来自消费者的唯一 ID)
  • 这个 external_id 应该在创建时使用 POST 请求设置
  • 这个 external_id,不能用 PATCH 改变

  • 实现它的解决方案是什么?

    API 示例:
    swagger: '2.0'
    
    host: api.com
    basePath: /v2
    schemes:
      - https
    
    info:
      title: dummy
      version: 1.0.0
    
    consumes:
      - application/json
    
    produces:
      - application/json
    
    
    paths:
      /messages:
        post:
          summary: Create a message
          parameters:
            - name: message
              in: body
              required: true
              schema:
                $ref: '#/definitions/Message'
    
          responses:
    
            201:
              description: Ok
              schema:
                $ref: '#/definitions/Message'
    
    
      /messages/{id}:
        get:
    
          summary: "Get a message by ID"
          parameters:
            - name: id
              in: path
              description: The message ID
              required: true
              type: string
              format: uuid
    
          responses:
    
            200:
              description: OK - the message
              schema:
                $ref: '#/definitions/Message'
    
        patch:
          summary: Modify a message
          parameters:
            - name: id
              in: path
              description: The message ID
              required: true
              type: string
              format: uuid
    
            - name: message
              in: body
              required: true
              schema:
                $ref: '#/definitions/Message'
    
          responses:
    
            201:
              description: Ok
              schema:
                $ref: '#/definitions/Message'
    
    definitions:
    
      Message:
        type: object
        required:
          - id
          - external_id
          - title
          - content
          - created_at
    
        properties:
          id:
            type: string
            readOnly: true
    
          external_id:
            type: string
            description: "Your own reference ID"
    
          title:
            type: string
    
          content:
            type: string
    
          created_at:
            type: string
            format: date-time
            readOnly: true
    

    我看到的唯一解决方案:
  • 使用 allOf
  • 定义 2 个定义(Message 和 UpdatedMessage)
  • 不使用 PATCH 方法或 GET/POST 方法中的定义

  • 有没有更好的解决方案来实现这一目标?理想的解决方案是只有一个 Message 定义,并在 PATCH 方法中覆盖 Message 定义(删除该字段)。

    我不知道这是否可能。

    谢谢

    最佳答案

    要处理此用例,您必须定义 2 条消息:

  • UpdateMessage包含除 external_id 之外的所有属性
  • 一个 Message包含所有 UpdateMessage属性加external_id使用 allOf是使用 OpenAPI (fka. Swagger) 2.0 版本实现这一目标的唯一方法。

  • 这是相应的 YAML:
    swagger: '2.0'
    
    host: api.com
    basePath: /v2
    schemes:
      - https
    
    info:
      title: dummy
      version: 1.0.0
    
    consumes:
      - application/json
    
    produces:
      - application/json
    
    
    paths:
      /messages:
        post:
          summary: Create a message
          parameters:
            - name: message
              in: body
              required: true
              schema:
                $ref: '#/definitions/Message'
    
          responses:
    
            201:
              description: Ok
              schema:
                $ref: '#/definitions/Message'
    
    
      /messages/{id}:
        get:
    
          summary: "Get a message by ID"
          parameters:
            - name: id
              in: path
              description: The message ID
              required: true
              type: string
              format: uuid
    
          responses:
    
            200:
              description: OK - the message
              schema:
                $ref: '#/definitions/Message'
    
        patch:
          summary: Modify a message
          parameters:
            - name: id
              in: path
              description: The message ID
              required: true
              type: string
              format: uuid
    
            - name: message
              in: body
              required: true
              schema:
                $ref: '#/definitions/UpdateMessage'
    
          responses:
    
            201:
              description: Ok
              schema:
                $ref: '#/definitions/Message'
    
    definitions:
    
      UpdateMessage:
        type: object
        required:
          - id
          - title
          - content
          - created_at
    
        properties:
          id:
            type: string
            readOnly: true
    
          title:
            type: string
    
          content:
            type: string
    
          created_at:
            type: string
            format: date-time
            readOnly: true
    
      Message:
        allOf:
          - $ref: '#/definitions/UpdateMessage'
          - required:
              - external_id
            properties:
              external_id:
                type: string
                description: "Your own reference ID"
    

    关于api - 在 swagger 2.0 (openapi) 中,如何在 POST 和 PUT 请求之间有不同的资源定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38244375/

    相关文章:

    json - 在 Coldfusion 中的 api Web 服务中以 json 格式发送纯文本

    c++ - 为什么 const/non-const 函数重载的继承不明确?

    java - 在 RequestBody 的 POST 上隐藏 ID,但在创建时返回 ID

    swagger - 生成 REST API 文档的步骤

    swagger - NestJS 映射类型 - 嵌套对象

    python - 使用亚马逊的 API 查找产品的 UPC (Python)

    facebook - 如何获取知道线程 ID 的消息线程 URL?

    php - HTTP 请求失败 所需长度

    另一个类的android xml按钮调用方法

    c# - Swashbuckle 多态性支持问题