如何在 swagger API 路由文档中指定模型属性的子集

How to specify subset of model properties in swagger API route documentation

正在大摇大摆地为我的服务编写 API 规范。我正在使用模型定义('#/definitions/prototype')作为 POST /prototypesPATCH /prototypes/:id 路由的主体参数。

如何指定 PATCH 路由仅接受 POST 路由所接受的请求正文中的一部分属性?例如,我希望 PATCH /instances/:id 路由只允许修改 mobileDeviceId 原型 属性.

swagger: "2.0"
info:
  title: ""
  description: ""
  version: "1.0.0"
host: foo.example.com
schemes:
  - https
basePath: /v1
produces:
  - application/json
consumes:
  - application/json
paths:
  /prototypes:
    post:
      summary: Create new prototype
      parameters:
        - name: prototype
          in: body
          description: Prototype object
          required: true
          schema:
            $ref: "#/definitions/Prototype"
      responses:
        201:
          description: Success
          schema:
            $ref: "#/definitions/SuccessCreated"
        403:
          description: Prototype limit exceeded error
          schema:
            $ref: "#/definitions/Error"
        default:
          description: Unexpected error
          schema:
            $ref: "#/definitions/Error"
  /prototypes/{id}:
    patch:
      summary: Update an existing prototype's properties
      parameters:
        - name: id
          in: path
          type: number
          description: ID property of a prototype
          required: true
        - name: prototype
          in: body
          description: Prototype object
          required: true
          schema:
            $ref: "#/definitions/Prototype"
      responses:
        200:
          description: Success
definitions:
  Prototype:
    type: object
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      mobileDeviceId:
        type: number
  SuccessCreated:
    type: object
    description: Returned as response to successful resource create request
    properties:
      code:
        type: number
        default: 201
      message:
        type: string
  Error:
    type: object
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
      fields:
        type: string

Swagger 使用 json-schema:http://json-schema.org $refs 为您提供了一种在新路径上重复现有 json-schema 的方法。 请注意,您正在为 patch/parameters/-name:prototype/schema 使用 $ref。 您可以在定义部分为补丁创建一个新定义并引用它

swagger: "2.0"
info:
  title: ""
  description: ""
  version: "1.0.0"
host: foo.example.com
schemes:
  - https
basePath: /v1
produces:
  - application/json
consumes:
  - application/json
paths:
  /prototypes:
    post:
      summary: Create new prototype
      parameters:
        - name: prototype
          in: body
          description: Prototype object
          required: true
          schema:
            $ref: "#/definitions/Prototype"
      responses:
        201:
          description: Success
          schema:
            $ref: "#/definitions/SuccessCreated"
        403:
          description: Prototype limit exceeded error
          schema:
            $ref: "#/definitions/Error"
        default:
          description: Unexpected error
          schema:
            $ref: "#/definitions/Error"
  /prototypes/{id}:
    patch:
      summary: Update an existing prototype's properties
      parameters:
        - name: id
          in: path
          type: number
          description: ID property of a prototype
          required: true
        - name: prototype
          in: body
          description: Prototype object
          required: true
          schema:
            $ref: "#/definitions/PatchPrototype"
      responses:
        200:
          description: Success
definitions:
  Prototype:
    type: object
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      mobileDeviceId:
        type: number
  PatchPrototype:
    type: object
    properties:
      mobileDeviceId:
        type: number
  SuccessCreated:
    type: object
    description: Returned as response to successful resource create request
    properties:
      code:
        type: number
        default: 201
      message:
        type: string
  Error:
    type: object
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
      fields:
        type: string