简体   繁体   中英

Is it possible to add methods to OpenAPI?

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;  
    }

}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM