【问题标题】:remove default implementation while generating code using swagger-codegen maven plugin使用 swagger-codegen maven 插件生成代码时删除默认实现
【发布时间】:2019-09-28 13:44:48
【问题描述】:

我必须从 yaml 文件生成代码,在我放的 swagger maven 插件中:

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

即使它说 iinterfaceOnly>true 但是 codegen 会生成一个具有默认实现的接口,如下所示:

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

如何禁用默认接口方法的生成,只在接口中定义而不是默认实现。

当我删除以下两个标签时,它可以工作

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

但是我的模型使用的是 localdatetime,所以我应该在 java8 上并且不能真正删除这两个标签

有什么想法吗?

【问题讨论】:

    标签: java maven-plugin swagger-codegen


    【解决方案1】:

    将 java8 设置为 false 但将 dateLibrary 设置为 java8 在 openapi 插件版本 4.1.2 中可以解决问题

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

    【讨论】:

      【解决方案2】:

      请尝试:

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

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

      【讨论】:

        【解决方案3】:

        如果您需要 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>
        

        使用旧版 dateLibrary 会排除默认方法,但可以手动将日期映射到 java8 日期格式。 它适用于 swagger-codegen 插件 3.0.18。

        请注意,指定

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

        将避免默认实现,但会导致 API 中的冗余方法(如 getRequest()getObjectMapper() 等)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-10-20
          • 2017-04-15
          • 2018-08-29
          • 1970-01-01
          • 2019-04-29
          • 1970-01-01
          • 2023-03-04
          • 1970-01-01
          相关资源
          最近更新 更多