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

时间:2016-03-30 13:30:29

标签: swagger swagger-2.0 swagger-editor

使用swagger为我的服务编写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

1 个答案:

答案 0 :(得分:2)

Swagger使用json-schema:http://json-schema.org $ref为您提供了一种在新路径上重复现有json-schema的方法。 请注意,您使用的$ref用于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