Home > front end >  Is it possible to add methods to OpenAPI?
Is it possible to add methods to OpenAPI?

Time:07-08

I have a question: is it possible to add methods with some logic to DTO, generated by open-api.

For example I have an openapi DTO:

CarDTO:
   type: object
   properties:
     id:
       type: string
       format: uuid
     isEngineWorks:
       type: boolean
       default: false
     isFuelFull:
       type: boolean
       default: false

I use maven plugin (openapi-generator-maven-plugin) and it generates me a java class:

public class CarDTO {
    @JsonProperty("id")
    @Valid
    private UUID id;

    @JsonProperty("isEngineWorks")
    private Boolean isEngineWorks = false;

    @JsonProperty("isFuelFull")
    private Boolean isFuelFull = false;
}

Is it possible to add a method in openapi, so it will be generated in my DTO? as a result, I want to have:

public class CarDTO {
    @JsonProperty("id")
    @Valid
    private UUID id;

    @JsonProperty("isEngineWorks")
    private Boolean isEngineWorks = false;

    @JsonProperty("isFuelFull")
    private Boolean isFuelFull = false;
    
    public boolean isCarReadyToDrive {
        return isEngineWorks && isFuelFull;  
    }

}

CodePudding user response:

Sure, it is possibile.

You have to override the pojo.mustache file related to the OpenAPI generator you are using.

I usually do that, and here is the official OpenAPI customization guide.

  • Related