我可以使用Gradle OpenApi生成器插件("org.openapi.generator" version "4.2.1"
成功地生成Spring API和模型文件。
我只有一个api.yml
文件:
openapi: 3.0.2
info:
title: API Documentation
version: 1.0.0
servers:
- url: http://localhost:8080/
tags:
- name: Users
paths:
/users:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Parent'
responses:
200:
description: New user created
components:
schemas:
Parent:
required:
- status
properties:
status:
type: string
discriminator:
propertyName: status
mapping:
CHILD_ONE: '#/components/schemas/ChildOne'
CHILD_TWO: '#/components/schemas/ChildTwo'
ChildOne:
allOf:
- $ref: '#/components/schemas/Parent'
- type: object
ChildTwo:
allOf:
- $ref: '#/components/schemas/Parent'
- type: object
然后代码生成产生Java Spring接口文件和3个模型文件:Parent.java
,ChildOne.java
和ChildTwo.java
-2个子类按预期扩展了Parent
类。
但是,当我将api.yml
分成2个文件(见下文)时,ChildOne.java
和ChildTwo.java
完全丢失了(只有{{1} }文件生成):
Parent.java
文件:
api.yml
api-sub.yml文件:
openapi: 3.0.2
info:
title: API Documentation
version: 1.0.0
servers:
- url: http://localhost:8080/
paths:
/users:
$ref: './api-sub.yml#/test'
生成传递没有错误,但仅生成了一个类-test:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Parent'
responses:
200:
description: New user created
components:
schemas:
Parent:
required:
- status
properties:
status:
type: string
discriminator:
propertyName: status
mapping:
CHILD_ONE: '#/components/schemas/ChildOne'
CHILD_TWO: '#/components/schemas/ChildTwo'
ChildOne:
allOf:
- $ref: '#/components/schemas/Parent'
- type: object
ChildTwo:
allOf:
- $ref: '#/components/schemas/Parent'
- type: object
:
Parent.java
为什么没有生成另外2个类// [...]
// Imports etc....
// [...]
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "status", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = ChildOne.class, name = "CHILD_ONE"),
@JsonSubTypes.Type(value = ChildTwo.class, name = "CHILD_TWO"),
})
public class Parent {
@JsonProperty("status")
private String status;
// [...]
// Getters, setters etc....
// [...]
和ChildOne.java
?
请注意,生成器在ChildTwo.java
中使用它们的名称和映射时会看到它们。
我做了一些调查,发现当我将模式定义从Parent.java
移到api-sub.yml
时,
api.yml
文件:
api.yml
openapi: 3.0.2
info:
title: API Documentation
version: 1.0.0
servers:
- url: http://localhost:8080/
paths:
/users:
$ref: './api-sub.yml#/test'
components:
schemas:
Parent:
required:
- status
properties:
status:
type: string
discriminator:
propertyName: status
mapping:
CHILD_ONE: '#/components/schemas/ChildOne'
CHILD_TWO: '#/components/schemas/ChildTwo'
ChildOne:
allOf:
- $ref: '#/components/schemas/Parent'
- type: object
ChildTwo:
allOf:
- $ref: '#/components/schemas/Parent'
- type: object
文件:
api-sub.yml
然后,生成4个文件:test:
post:
requestBody:
content:
application/json:
schema:
$ref: 'api.yml#/components/schemas/Parent'
responses:
200:
description: New user created
,Parent.java
,ChildOne.java
和(惊奇)ChildTwo.java
。
Parent2.java
在语义上与Parent2.java
相同,只是名称不同。
有人知道为什么会这样吗? OpenApi生成器中有错误吗?
编辑:
又一次观察:
当我在任何端点的某个地方使用孩子时,例如(仅用于测试)作为响应,然后生成这个特定的孩子(而不是另一个孩子):
Parent.java
是否需要使用子实体来生成?除了继承案例,我在其他地方也注意到了这种行为。