Openapi-generator: [JAVA] [SPRING] Missed ApiUtil when generated interface only

Created on 6 Aug 2018  路  8Comments  路  Source: OpenAPITools/openapi-generator

Description
task buildAPI(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
    verbose = true
    generatorName = "spring"

    systemProperties = [apis: ""]

    additionalProperties = [
            "sourceFolder"  : "",
            "interfaceOnly" : "true",
    ]

}

Generates code:

    @ApiOperation(value = "List all pets", nickname = "listPets", notes = "", response = Error.class, tags={ "pets", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "unexpected error", response = Error.class) })
    @RequestMapping(value = "/pets",
        produces = { "application/json" }, 
        method = RequestMethod.GET)
    default ResponseEntity<Error> listPets() {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    ApiUtil.setExampleResponse(request, "application/json", "{  \"code\" : 0,  \"message\" : \"message\"}");
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

But class ApiUtil is not generated.

openapi-generator version

openapiGeneratorVersion = '3.2.0-SNAPSHOT'

OpenAPI declaration file content or url

openapi: "3.0.0"
info:
  version: 1.0.0
  title: IBAN Register
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      responses:
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

Suggest a fix/enhancement

Probably duplicate of #386

Bug Java Spring

Most helpful comment

I prefer solution when generateSupportingFiles is false then default interface implementation does not rely on ApiUtil class.

All 8 comments

@demtnt thanks for reporting the issue. I tried to reproduce with the following:

java -jar "./modules/openapi-generator-cli/target/openapi-generator-cli.jar" generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -l spring -o /tmp/spring-cloud3/ --additional-properties interfaceOnly=true

But the output can be compiled without issue using mvn test

and I can verify the file src/main/java/org/openapitools/api/ApiUtil.javaexisted

same issue
setting the property generateSupportingFiles=true helped

Confirmed. In gradle plugin case You need to add system property e.g.

task buildAPI(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){ [...] systemProperties = [ supportingFiles: 'ApiUtil.java' ] [...] }

I prefer solution when generateSupportingFiles is false then default interface implementation does not rely on ApiUtil class.

I set generateSupportingFiles is false but I still get a default method in the generated Interface using ApiUtil
Any idea howto disable the generation of use of ApiUtil.setExampleResponse(request, "application/json", .... in the default method?
The default method should just return return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

A workaround is to place the generated ApiUtil.java in your src/main/java (under the same package name as defined by apiPackage). This allows you to use generateSupportingFiles = false without compilation issues.

Or if you are on java 8 and don't care about default implementations, use spring generator's property skipDefaultInterface = true

Setting generateSupportingFiles = false and skipDefaultInterface = true did not work out for me, as other compilation errors occured. However, I've analyzed the generation behavior for support files and found another configurable parameter supportingFilesToGenerate = ApiUtil.java that allowed me to generate the missing ApiUtil.java but nothing more :)

change library to spring-mvc

Was this page helpful?
1 / 5 - 1 ratings