如何在OpenApi中定义别名,最终在Pojo中使用它

问题描述 投票:0回答:2

假设我的 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

所以我需要两件东西:

  • 告诉 OpenAPI 关于别名
    Address=Places
    zip=zipcode
    的方法。
  • 一种使此信息更改创建的代码的方法。例如,对于 Hibernate,这意味着在正确的位置添加
    @Table(name="Places")
    @Column(name="zipcode")

重要提示:我无法更改API,预计会坚持

Address
zip

这可以做到吗?我检查了 specs 以及 swagger-codegen 和 openapi-generator (我更喜欢后者),但没有发现任何迹象表明类似的内容被涵盖。 对于 openapi-generator,我查看了 Mustache templates,据我所知,没有代码引用 yaml 文件中定义的任何“别名信息”。

我是否必须走一条艰难的路,自己定义一个 OpenAPI 扩展,如 thisthis 加上 自定义模板 ? 我能找到的最接近的是这个现有扩展,它定义了一种类型的别名。但这只满足了我的第一部分需求。

swagger openapi swagger-codegen openapi-generator openapi-generator-cli
2个回答
0
投票

使用 openapi 作为别名很简单,但机器人架构很健壮。

引入AddressDto来处理公众脸(API)和内部模型之间的阻抗不匹配


-1
投票

使用

$ref

components:
  schemas:
    Address:
      type: object
      properties:
        name:
          type: string
        zip:
          type: integer
          format: int64
        town:
          type: string
    Places: 
      $ref: '#/components/schemas/Address'
© www.soinside.com 2019 - 2024. All rights reserved.