如何重用招摇定义并删除其中的某些参数? [英] How to reuse swagger definitions and remove some of the parameters in it?

查看:101
本文介绍了如何重用招摇定义并删除其中的某些参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

definitions:
  User:
    type: object
    properties:
      id:
        type: integer
      username:
        type: string
      first_name:
        type: string
      last_name:
        type: string
      password:
        type: string
      created_at:
        type: string
        format: date-time
      updated_at:
        type: string
        format: date-time
    required:
      - username
      - first_name
      - last_name
      - password

/api/users:
  post:
    description: Add a new user
    operationId: store
    parameters:
      - name: user
        description: User object
        in: body
        required: true
        type: string
        schema:
          $ref: '#/definitions/User'
    produces:
      - application/json
    responses:
      "200":
        description: Success
        properties:
          success:
            type: boolean
          data:
            $ref: '#/definitions/User'

如您所见,在/api/users下的post键中,我将User定义用作其模式.

As you can see, in the post key under /api/users I used the User definition as my schema on it.

我想减少代码,所以我将User定义重用为自己的架构.这里的问题是我不需要idcreated_atupdated_at字段.

I want to lessen my code so I reused the User definition as my schema. The problem here is that I do not need the id, created_at and updated_at fields.

除了提到的字段之外,是否有一种方法可以继承某些字段?另外,由于我想学习敏捷,所以我希望提出一些建议以使它更好.谢谢.

Is there a way to just inherit some of the fields except the fields mentioned? Also, I would love some suggestions to make it better since I'm trying to learn swagger. Thank you.

推荐答案

您将不得不分别定义模型.

You would have to define the models separately.

但是,对于排除和区别的情况,您可以选择.

However, you have options for the cases of exclusion and difference.

如果您想排除在外(这很简单),请创建一个模型 具有排除属性的,例如ModelA.然后将ModelB定义为 ModelA加上其他属性:

If you're looking to exclude, which is the easy case, create a model of with the excluded property, say ModelA. Then define ModelB as ModelA plus the additional property:

ModelB:
  allOf:
    - $ref: "#/definitions/ModelA"
    - type: object
      properties:
        id:
          type: string

如果要定义差异,请遵循相同的方法 并从ModelA中排除ID.然后定义ModelBModelC 作为扩展ModelA并向其添加id属性,每个属性都有其自己的 限制.请注意,JSON Schema可以让您遵循 上面的原始示例在某些情况下覆盖"了定义. 但是,由于它并不是真正的压倒一切,因此需要 更好地理解JSON Schema的概念,以免变得简单 错误,我建议暂时走这条路.

If you're looking to define the difference, follow the same method above, and exclude the id from ModelA. Then define ModelB and ModelC as extending ModelA and add the id property to them, each with its own restrictions. Mind you, JSON Schema can allow you to follow the original example above for some cases to "override" a definition. However, since it is not really overriding, and one needs to understand the concepts of JSON Schema better to not make simple mistakes, I'd recommend going this path for now.

这篇关于如何重用招摇定义并删除其中的某些参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆