重用对模型的引用但重新定义所需的属性

Reusing references to models but redefining required properties

There's a year old question with a similar title here, but the only answer does not answer the question fully so hoping that things have changed since I'm trying again.

我有一个 Car 的模型,我想将其与 GET 请求一起使用,以创建我需要 name.[=22= 的新实例]

此外,我想将此与 PUT 请求一起使用以更新现有实例。更新请求不应包含所有属性,Car 标识符已在路径中指定,只有当前属性会 'merged' 到现有对象中。

我尝试使用以下规范:

CarProperties:
  type: object
  properties:
    name:
      type: string
      title: Car Name
      description: A friendly name for this car.
      minLength: 1
      md:
        type: object
        hidden: 'true'
    description:
      type: string
      title: Car Description
      description: A detailed description of the car.
      md:
        type: object
        hidden: 'true'

UpdateCar:
  allOf:
    -  { $ref: '#/definitions/CarProperties' }
  type: object  

NewCar:
  allOf:
    -  { $ref: '#/definitions/CarProperties' }
  type: object  
  required: [name]

我预计 name 只在 NewCar 中需要 属性,但是在 NewCarUpdateCar 中 属性 name 是可选的。

如何从引用模型中指定所需属性的子集?

摆弄了一段时间后,我似乎对 required 字段进行了错误的缩进。以下语法似乎产生了我想要的结果:

CarProperties:
  type: object
  properties:
    name:
      type: string
      title: Car Name
      description: A friendly name for this car.
      minLength: 1
      md:
        type: object
        hidden: 'true'
    description:
      type: string
      title: Car Description
      description: A detailed description of the car.
      md:
        type: object
        hidden: 'true'

UpdateCar:
  allOf:
    - { $ref: '#/definitions/CarProperties' }
    - type: object  

NewCar:
  allOf:
    - { $ref: '#/definitions/CarProperties' }
    - type: object
      required: [name]

现在我有:

  1. NewCar 具有必填 name 字段的实体。
  2. UpdateCar 具有可选 name 字段的实体。
  3. 如果我想在 Car 中添加另一个 属性,我只需要在一个地方添加。