How to define alias in OpenApi, eventually using it in Pojo
Asked Answered
B

1

7

Let's assume I have this definition in my openapi.yml (this is just a madeup example to depict the issue I have):

components:
  schemas:
    Address:
      type: object
      properties:
        name:
          type: string
        zip:
          type: integer
          format: int64
        town:
          type: string

This will lead to the generation of code for the model that looks like this:

public class Address   {
  @JsonProperty("name")
  private String name = null;

  @JsonProperty("zip")
  private Long zip = null;

  @JsonProperty("town")
  private String town = null;
...

My issue is that I have to persist this Pojo in a database and there exists no table Address (let's assume it is called Places instead) and the column for the zipcode is called zipcode instead of zip.

So I would need two things:

  • a way to tell OpenAPI about the aliases Address=Places and zip=zipcode.
  • a way to make this information change the code created. For Hibernate for example this would mean to add @Table(name="Places") and @Column(name="zipcode") in the right places.

Important remark: I can not change the API, it is expected to stick to Address and zip.

Can this be done ? I checked the specs and both swagger-codegen and openapi-generator (I'ld prefer the latter) but found no sign that something like this is covered. For openapi-generator I had a look at the Mustache templates and as far as I can tell there is no code that refers any "alias information" that was defined in the yaml file.

Do I have to go the hard way and define an OpenAPI extension like this or this plus customize the templates all by myself ? The closest I could find is this existing extension, that defines one type of alias. But this satisfies only the first part of my demand.

Bharal answered 4/10, 2021 at 19:41 Comment(1)
the only way to define alias in OpenAPI is through $refMacrobiotics
B
0

The use of openapi for the alias is simple, but bot architecture robust.

Introduce an AddressDto to handle the impedance mistmatch beteen public face (API) and internal model

Bathometer answered 22/1, 2024 at 21:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.