openapi-processor / openapi-processor/openapi-processor-spring
Inheritance support
还没有人认领这个 Issue。
- 主要语言
- Kotlin
- 星标
- 56
- 派生
- 11
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 8
描述
Quite impressed with the toolset you created here.
I was curious about inheritance support so I tried it out with a minimal example:
openapi: 3.0.2
info:
title: edit me
version: 1.0.0
paths:
/nothing:
description: dummy
get:
responses:
'200':
description: empty
content:
'application/json':
schema:
$ref: '#/components/schemas/ChildSchema'
components:
schemas:
ParentSchema:
type: object
properties:
parentAttr:
type: string
ChildSchema:
allOf:
- $ref: '#/components/schemas/ParentSchema'
- type: object
properties:
childAttr:
type: string
This yields a ChildSchema.java consisting of everything from the parent and child model:
public class ChildSchema {
@JsonProperty("parentAttr")
private String parentAttr;
@JsonProperty("childAttr")
private String childAttr;
public String getParentAttr() {
return parentAttr;
}
public void setParentAttr(String parentAttr) {
this.parentAttr = parentAttr;
}
public String getChildAttr() {
return childAttr;
}
public void setChildAttr(String childAttr) {
this.childAttr = childAttr;
}
}
Now specifying a discriminator results in inheritance in the generated code (at least for the openapi-generator project):
openapi: 3.0.2
info:
title: edit me
version: 1.0.0
paths:
/nothing:
description: dummy
get:
responses:
'200':
description: empty
content:
'application/json':
schema:
$ref: '#/components/schemas/ChildSchema'
components:
schemas:
ParentSchema:
type: object
discriminator:
propertyName: className
properties:
parentAttr:
type: string
ChildSchema:
allOf:
- $ref: '#/components/schemas/ParentSchema'
- type: object
properties:
childAttr:
type: string
For this a special propertyName of className is used. If I run that in your playground with spring 2021.5 it complains about the property name which is not wrong strictly speaking:
The discriminator 'className' is not a property of this schema (code: 134)
If I run this locally with 2023.1.2 then this does not error but does not generate two classes with one inheriting from each other:
public class ChildSchema {
@JsonProperty("parentAttr")
private String parentAttr;
@JsonProperty("childAttr")
private String childAttr;
public String getParentAttr() {
return parentAttr;
}
public void setParentAttr(String parentAttr) {
this.parentAttr = parentAttr;
}
public String getChildAttr() {
return childAttr;
}
public void setChildAttr(String childAttr) {
this.childAttr = childAttr;
}
}
Expected result would have been:
public class ParentSchema {
@JsonProperty("parentAttr")
private String parentAttr;
public String getParentAttr() {
return parentAttr;
}
public void setParentAttr(String parentAttr) {
this.parentAttr = parentAttr;
}
}
public class ChildSchema extends ParentSchema {
@JsonProperty("childAttr")
private String childAttr;
public String getChildAttr() {
return childAttr;
}
public void setChildAttr(String childAttr) {
this.childAttr = childAttr;
}
}
Now this expectation could of course be wrong. My question now would be if this inheritance use case is supported and/or if I would have to do something differently to get the desired result. Thanks.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
在 playground 中使用 spring 2021.5,并在本地使用 2023.1.2,复现 issue 中的最小 OpenAPI 示例。将生成的 ParentSchema 和 ChildSchema 输出与预期的独立类层次结构进行比较,然后确定是否有意支持继承,以及应记录或更改哪种行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- java, kotlin, spring-boot
- 领域
- api, backend, tooling
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 20/100