请在我的合同yaml文件中找到架构:
Foo:
allOf:
- $ref: "#/components/schemas/Bar"
properties:
ancestors:
items:
$ref: "#/components/schemas/Bar"
type: array
description:
type: object
additionalProperties:
type: string
id:
description: id
type: string
type: object
Bar:
properties:
accessAllowed:
items:
type: string
type: array
catalog:
type: boolean
children:
items:
$ref: "#/components/schemas/Bar"
type: array
在使用swagger 2时,生成的类Foo扩展了Bar。但是在使用openAPI 3之后,Foo在使用allOf
时并没有扩展Bar。它所做的只是将Bar的所有属性复制到Foo类。
虽然现在Foo类将包含Bar的所有属性,但当我们查看java代码时,Foo实际上并没有继承。在使用OpenAPI 3时,有没有任何方法可以生成Foo扩展Bar类,因为在许多情况下,需要生成继承父类的类。
您需要添加:
...
Bar:
...
discriminator:
propertyName: barType
mapping:
foo: "#/components/schemas/Foo"
...
...
完整示例:
Foo:
allOf:
- $ref: "#/components/schemas/Bar"
properties:
ancestors:
items:
$ref: "#/components/schemas/Bar"
type: array
description:
type: object
additionalProperties:
type: string
id:
description: id
type: string
type: object
Bar:
discriminator:
propertyName: barType
mapping:
foo: "#/components/schemas/Foo"
properties:
accessAllowed:
items:
type: string
type: array
catalog:
type: boolean
children:
items:
$ref: "#/components/schemas/Bar"
type: array