简体   繁体   中英

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 :

<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:

@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

Any Idea ?

Setting java8 to false but dateLibrary to java8 does the trick in openapi plugin version 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

If you need LocalDateTime support and default method implementation is unwanted, you can use the following trick:

<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. It works on swagger-codegen plugin 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).

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