请在我的合同 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 后,使用
allOf
时 Foo 不会扩展 Bar。它所做的就是将 Bar 的所有属性复制到 Foo 类。
虽然现在 Foo 类将包含 Bar 的所有属性,但当我们查看 java 代码方面时,Foo 实际上并没有继承。使用 OpenAPI 3 时,有没有办法生成扩展 Bar 的 Foo 类,因为在很多情况下需要生成继承父类的类。
您需要添加:
...
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
使用maven插件,需要添加配置
<openapiNormalizer>REF_AS_PARENT_IN_ALLOF=true</openapiNormalizer>
它将继承第一个没有鉴别器的 alloff 对象