假设我的 openapi.yml 中有这个定义(这只是一个虚构的示例来描述我遇到的问题):
components:
schemas:
Address:
type: object
properties:
name:
type: string
zip:
type: integer
format: int64
town:
type: string
这将导致生成如下所示的模型代码:
public class Address {
@JsonProperty("name")
private String name = null;
@JsonProperty("zip")
private Long zip = null;
@JsonProperty("town")
private String town = null;
...
我的问题是,我必须将这个 Pojo 保留在数据库中,并且不存在表
Address
(我们假设它被称为 Places
),并且邮政编码的列被称为 zipcode
而不是 zip
。
所以我需要两件东西:
Address=Places
和 zip=zipcode
的方法。@Table(name="Places")
和 @Column(name="zipcode")
。重要提示:我无法更改API,预计会坚持
Address
和zip
。
这可以做到吗?我检查了 specs 以及 swagger-codegen 和 openapi-generator (我更喜欢后者),但没有发现任何迹象表明类似的内容被涵盖。 对于 openapi-generator,我查看了 Mustache templates,据我所知,没有代码引用 yaml 文件中定义的任何“别名信息”。
我是否必须走一条艰难的路,自己定义一个 OpenAPI 扩展,如 this 或 this 加上 自定义模板 ? 我能找到的最接近的是这个现有扩展,它定义了一种类型的别名。但这只满足了我的第一部分需求。
使用 openapi 作为别名很简单,但机器人架构很健壮。
引入AddressDto来处理公众脸(API)和内部模型之间的阻抗不匹配
$ref
components:
schemas:
Address:
type: object
properties:
name:
type: string
zip:
type: integer
format: int64
town:
type: string
Places:
$ref: '#/components/schemas/Address'