OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.35k stars 6.46k forks source link

[Java] Codegen fails with multiple response types #896

Open BBKolton opened 6 years ago

BBKolton commented 6 years ago
Description

With two different defined response types for two different response codes, the generated file has a missing comma which crashes the build.

openapi-generator version

3.0.2

OpenAPI declaration file content or url
  /health/deep:
    get:
      operationId: getHealthDeep
      summary: provides a deep endpoint healthcheck
      tags:
        - sometag
      responses: 
        200:
          description: Health OK
          content:
            text/plain:
              schema:
                type: string

        500:
          description: Failed Health Check Error Report
          content:
            application/json:
              schema:
                $ref: ../common/500.yaml

Deep in the target folder hierarchy, the HealthApi.java file has this section which throws a syntax error

    @ApiOperation(value = "provides a deep endpoint healthcheck", nickname = "getHealthDeep", notes = "", response = String.class, tags={ "sometag", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Health OK", response = String.class) })
    @RequestMapping(value = "/health/deep",
        produces = { "text/plain""application/json" },  // The issue is here, where a comma is ommitted
        method = RequestMethod.GET)
    default ResponseEntity<String> getHealthDeep() {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }
Command line used for generation

mvn clean install

Steps to reproduce

Create an API document with a route. Attempt to build the file using the openapi-generator-maven-plugin

Suggest a fix/enhancement

Ensure that multiple options for return types create commas inbetween them

(This is my first real issue I've reported, please let me know if you need more info!)

jmini commented 6 years ago

(This is my first real issue I've reported, please let me know if you need more info!)

This is a great report for an issue.

If I remember well, something similar have already be reported and fixed. Can you try with the last released version: 3.2.2 instead of 3.0.2?

BBKolton commented 6 years ago

Looks like the issue in the HealthApi file is resolved, but now servicenameApi.java, a generated file under src/main/java/com/company/team/service/client/v1/serviceApi.java fails. I assume a fix to the health api file was not propogated.

public com.squareup.okhttp.Call getHealthDeepCall(final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
        Object localVarPostBody = new Object();

        // create path and map variables
        String localVarPath = "/health/deep";

        List<Pair> localVarQueryParams = new ArrayList<Pair>();
        List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();

        Map<String, String> localVarHeaderParams = new HashMap<String, String>();

        Map<String, Object> localVarFormParams = new HashMap<String, Object>();

        final String[] localVarAccepts = {
            "text/plain""application/json" // same problem, comma omitted
        };
        final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept);
jmini commented 6 years ago

I can not reproduce your error... I checked the code and the templates, and for me it should work. I also try to did to generate a java client to reproduce the error you are describing in the generated code. No luck.

To investigate further, please tell the exact spec (JSON, YAML) you are using and the exact configuration parameters in your pom.xml.

For references I found the issue I was mentioning earlier: #443