简体   繁体   English

使用 swagger-codegen maven 插件生成代码时删除默认实现

[英]remove default implementation while generating code using swagger-codegen maven plugin

I have to generate code from yaml file, in my swagger maven plugin I put :我必须从 yaml 文件生成代码,在我的 swagger maven 插件中:

<configOptions>
  <java8>true</java8>
  <sourceFolder>src/main/java</sourceFolder>
  <interfaceOnly>true</interfaceOnly>
  <dateLibrary>java8</dateLibrary>
  <singleContentTypes>true</singleContentTypes>
</configOptions>

even if it says iinterfaceOnly>true however the codegen generate an interface with default implementation as follow:即使它说 iinterfaceOnly>true 但是代码生成器生成一个具有默认实现的接口,如下所示:

@ApiOperation(value = "", nickname = "billetsFichiersHealthGet", notes = "Obtient l'état de santé de l'API. ", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "OK"),
        @ApiResponse(code = 200, message = "Erreur", response = Error.class) })
    @RequestMapping(value = "/bills/health",
        produces = "application/json", 
        consumes = "",
        method = RequestMethod.GET)
    default ResponseEntity<Void> billetsFichiersHealthGet() {
        if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
        } else {
            log.warn("ObjectMapper or HttpServletRequest not configured in default BilletsApi interface so no example is generated");
        }
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }

How can I disable generation of default interface method and just have definition in interface and not default implementation.如何禁用默认接口方法的生成,并且只在接口中定义而不是默认实现。

When I remove the following two tags it works当我删除以下两个标签时,它会起作用

<java8>true</java8> 
<dateLibrary>java8</dateLibrary>

However my models are using localdatetime and so I should be on java8 and cant remove these two tags really但是我的模型使用的是 localdatetime,所以我应该在 java8 上并且不能真正删除这两个标签

Any Idea ?任何的想法 ?

Setting java8 to false but dateLibrary to java8 does the trick in openapi plugin version 4.1.2将 java8 设置为 false 但将 dateLibrary 设置为 java8 在 openapi 插件版本 4.1.2 中起作用

<java8>false</java8> 
<dateLibrary>java8</dateLibrary>

Please try:请尝试:

<configOptions>
    <dateLibrary>java8</dateLibrary>
    <java8>true</java8>
    <defaultInterfaces>false</defaultInterfaces>
</configOptions>

https://github.com/swagger-api/swagger-codegen/issues/8833 https://github.com/swagger-api/swagger-codegen/issues/8833

If you need LocalDateTime support and default method implementation is unwanted, you can use the following trick:如果您需要 LocalDateTime 支持并且不需要默认方法实现,您可以使用以下技巧:

<configuration>
    ...
    <typeMappings>
        <typeMapping>date=LocalDate</typeMapping>
        <typeMapping>date-time=LocalDateTime</typeMapping>
    <typeMappings>
    <importMappings>
        <importMapping>LocalDate=java.time.LocalDate</importMapping>
        <importMapping>LocalDateTime=java.time.LocalDateTime</importMapping>
    </importMappings>
    <configOptions>
        <interfaceOnly>true</interfaceOnly>
        <dateLibrary>legacy</dateLibrary>
    </configOptions>
<configuration>

Using legacy dateLibrary excludes default methods, but it is possibility to manually map date to java8 date format.使用旧的 dateLibrary 排除默认方法,但可以手动将日期映射到 java8 日期格式。 It works on swagger-codegen plugin 3.0.18.它适用于 swagger-codegen 插件 3.0.18。

Please pay attention, specifying请注意,指定

<dateLibrary>java8</dateLibrary>
<defaultInterfaces>false</defaultInterfaces>

will avoid default implementation, but leads to redundant methods in API (like getRequest() , getObjectMapper() etc).将避免默认实现,但会导致 API 中的冗余方法(如getRequest()getObjectMapper()等)。

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

相关问题 如何使用 swagger-codegen-plugin (maven) 生成客户端代码? - How to generate client code using with swagger-codegen-plugin (maven)? 使用 swagger codegen for openapi 3.0 生成代码时出现 NullPointer 异常 - NullPointer exception while generating code using swagger codegen for openapi 3.0 如何使用 springdoc-openapi-maven-plugin 和 swagger-codegen-maven-plugin 生成客户端代码? - How to generate client code using springdoc-openapi-maven-plugin and swagger-codegen-maven-plugin? swagger-codegen不为引用的json文件中定义的模式生成类 - swagger-codegen not generating classes for schemas defined in referenced json files SWAGGER swagger-codegen 配置 - SWAGGER swagger-codegen configuration 删除 swagger-codegen-maven-plugin api class 中的 ResponseEntity - Remove ResponseEntity in swagger-codegen-maven-plugin api class 编译 swagger-codegen java Petstore sdk 时出错 - Error while compiling swagger-codegen java Petstore sdk Swagger-codegen gradle 版本 - Swagger-codegen gradle version 如何使用 swagger-codegen-maven 插件在 boolean 上生成 getter - how to generate getter on boolean using swagger-codegen-maven plugin swagger-codegen-maven-plugin 在生成 API class 时忽略我为 ZonedDateTime 设置的导入映射 - swagger-codegen-maven-plugin ignores the importmapping i have set for ZonedDateTime when generating an API class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM