2

大摇大摆地为我的服务编写 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
4

1 回答 1

2

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

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
于 2016-03-30T19:29:05.530 回答