Should I define different POST and PATCH models in OpenAPI?

匆匆过客 提交于 2019-12-11 17:58:29

问题


I am defining a REST API in OpenAPI3 (Swagger).

I have an API that has a POST which uses a Model I have defined in teh components section as follows:

post:
  summary: "Used to add some data"
  operationId: postMyData
  requestBody:
    content:
      application/json:
        schema:
           $ref: '#/components/schemas/MyModel'
    required: true

components:
  schemas:
    MyModel:
      type: object
      properties:
        SomeProperty1:
          type: string
        SomeProperty2:
          type: string
        SomeProperty3:
          $ref: '#/components/schemas/SomeOtherModel'
        SomeProperty4:
          type: string

Now I have a PATCH API call that I want to use to update only some data of MyModel, e.g. SomeProperty1 and SomeProperty4.

Should I define a new Model for this PATCH operation? like so:

MyPATCHModel:
  type: object
  properties:
    SomeProperty1:
     type: string
    SomeProperty4:
     type: string

And then use this new MyPATCHModel in the requestBody of the PATCH operation? What is the standard practice here as I will have several API that are similar to this.


回答1:


Check the docs on combining JSON schemas.

In your case you could for example define a shared MyModel schema with the two properties used in the PATCH method, and then another NewMyModel schema that uses allOf to combine MyModel with the POST-only properties.

Check this question for a concrete example.



来源:https://stackoverflow.com/questions/57459085/should-i-define-different-post-and-patch-models-in-openapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!