openapi - OpenApi Spring 生成器 - 缺少模型文件
问题描述
我可以使用 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
- 2 个子类按预期ChildTwo.java
扩展类。Parent
但是,当我分成api.yml
2 个文件(见下文)ChildOne.java
并且ChildTwo.java
完全丢失(仅Parent.java
生成文件)时:
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'
api-sub.yml 文件:
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
生成没有错误,但只生成 1 个类 -Parent.java
生成:
// [...]
// 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....
// [...]
为什么还有 2 个其他类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
文件:
test:
post:
requestBody:
content:
application/json:
schema:
$ref: 'api.yml#/components/schemas/Parent'
responses:
200:
description: New user created
然后,生成 4 个文件:Parent.java
、ChildOne.java
和ChildTwo.java
(surprise) Parent2.java
。
Parent2.java
在语义上与 相同Parent.java
,只是名称不同。
知道为什么会这样吗?OpenApi 生成器中是否存在错误?
编辑:
还有一个观察:
当我在任何端点的某个地方使用一个孩子时,例如(仅用于测试)作为响应,然后生成这个特定的孩子(但不是另一个):
test:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Parent'
responses:
200:
description: New user created
content:
application/json:
schema:
$ref: '#/components/schemas/ChildOne'
### [...]
### rest of api-sub.yml with schemas
### [...]
是否需要使用子实体才能生成?我在继承案例之外的其他地方注意到了这种行为。
解决方案
推荐阅读
- javascript - VueJS:将对象作为道具传递给组件-作为null
- php - SQLite 查找范围(3000-4000)之间的缺失数字
- r - 我是否需要遍历 R 中的向量元素来计算相关性?
- csv - F# 合并具有不同列的 CSV 文件
- react-native - 反应导航中的`goBack`不会返回
- c# - C# SQL 数据填充到 Listview 失败
- solr - Cassandra - 如何使用地图在 UDT 上执行 Solr 查询
- node.js - Redis 在节点中是“有前途的”吗?
- cassandra - 跨中心 2 个不同 cassandra 集群的复制策略应该是什么?
- yii2 - Button in every row in dynamic form yii2