简体   繁体   English

是否可以向 OpenAPI 添加方法?

[英]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.我有一个问题:是否可以将具有某些逻辑的方法添加到由 open-api 生成的 DTO。

For example I have an openapi DTO:例如我有一个 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:我使用 maven 插件(openapi-generator-maven-plugin),它为我生成了一个 java 类:

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?是否可以在openapi中添加一个方法,所以它将在我的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.您必须覆盖与您正在使用的 OpenAPI 生成器相关的pojo.mustache文件。

I usually do that, and here is the official OpenAPI customization guide .我通常会这样做,这里是官方的 OpenAPI 定制指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:是否可以自动向方法添加日志语句? - Java: Is it possible to automatically add log statements to methods? Swagger openapi:openapi.yaml/swagger-ui 中的过滤方法 - Swagger openapi: Filter methods in openapi.yaml/ swagger-ui OpenApi 3.0 显示所有 HTTP 方法 - OpenApi 3.0 shows all af the HTTP methods 如何将 OpenAPI 客户端添加为子项目? - How to add OpenAPI client as a subproject? 是否可以检查 returnType 是否是 OpenApi-Generator 中的文件? - Is it possible to check if returnType is a file in the OpenApi-Generator? 是否可以使用 openapi 生成 Spring hateoas 实体 - Is it possible to generate Spring hateoas entities with openapi 我是否需要添加许多方法,或者可以调用一个方法 - Do I need to add many methods, or is it possible to call one method 是否可以在不破坏 Java 的情况下向 Comparable 添加默认方法? - Would it be possible to add default methods to Comparable without breaking Java? IntelliJ是否可以将@Overrrides添加到特定接口的所有方法? - IntelliJ is it possible to add @Overrrides to all methods of a particular interface? 如果添加带有 WebMvcConfigurationSupport 的转换器,Swagger OpenAPI 3 不会显示在 springboot 2 上 - Swagger OpenAPI 3 not display on springboot 2 if add Converter with a WebMvcConfigurationSupport
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM