swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[jaxrs-cxf-client] incorrect List of Lists return type in Api

Open mhaefele opened this issue 9 years ago • 7 comments

Description

I have a swagger.json file with a response schema of string[]. I'm expecting this to produce a method return type of List of String. Instead I'm seeing List of List of String

Swagger-codegen version

2.2.2

Swagger declaration file content or url
"/v1/comments/categories" : {
     "get" : {
       "tags" : [ "comments" ],
       "summary" : "Returns all comment categories.",
       "description" : "",
       "operationId" : "getCategories_1",
       "parameters" : [ ],
       "responses" : {
         "200" : {
           "description" : "successful operation",
           "schema" : {
             "type" : "array",
             "items" : {
               "type" : "string"
             }
           }
         }
       }
     }
   }

Produces

@Api(value = "/", description = "")
public interface CommentsApi  {

    @GET
    @Path("/v1/comments/categories")
    @ApiOperation(value = "Returns all comment categories.", tags={  })
    public List<List<String>> getCategories1();
...

expected:

@Path("/")
@Api(value = "/", description = "")
public interface CommentsApi  {

    @GET
    @Path("/v1/comments/categories")
    @ApiOperation(value = "Returns all comment categories.", tags={  })
    public List<String> getCategories1();
...
Command line used for generation

Maven.

    <plugins>
      <plugin>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>2.2.2</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>swagger.json</inputSpec>

              <language>jaxrs-cxf-client</language>

              <configOptions>
                <dateLibrary>joda</dateLibrary>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
Steps to reproduce

Run maven against the above json.

Related issues

Searched, but nothing jumped out at me. I'm not really familiar with the project though.

Suggest a Fix

Unsure... Will poke a bit at it.

mhaefele avatar Mar 07 '17 15:03 mhaefele

So setting breakpoints, it looks like operation.returnType is already List but JavaJaxRS/cxf/api.mustache is delegating to returnTypes.mustache which also applies List< to the return type based on isListContainer.

So there's a mismatch in expectations there.

changing public {{>returnTypes}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); in api.mustache to public {{{returnType}}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); seems to fix it, but no idea if that breaks other things.

mhaefele avatar Mar 07 '17 16:03 mhaefele

Could you create a pull request, regenerating the samples (run bin/jaxrs-cxf-petstore-server.sh or similar)? This way our CI (and other contributors) server can have a look whether this breaks anything visible.

ePaul avatar Mar 07 '17 16:03 ePaul

Also, maybe compare with the templates in the other Java versions.

ePaul avatar Mar 07 '17 16:03 ePaul

Hmm...AFAIK looks like JavaSpring uses the returnTypes template, but the rest access returnType directly. I'll take a look at creating a pull request. Is there a quick guide on that (creating a pull request in general for OSS?)

mhaefele avatar Mar 07 '17 16:03 mhaefele

Ah... Yeah.. The change above definitely breaks the output from jaxrs-cxf-petstore-server.sh. I copied that file to a new one and changed it to use jaxrs-cxf-client as the language and I can see my issue in the sample output. I can also see it missing "void" for return types.

Looks like the AbstractJavaJAXRSServerCodegen specifically strips off the List from the return type:

                    operation.returnType = "void";
                    // set vendorExtensions.x-java-is-response-void to true as returnType is set to "void"
                    operation.vendorExtensions.put("x-java-is-response-void", true);
                } else if ( operation.returnType.startsWith("List") ) {
                    String rt = operation.returnType;
                    int end = rt.lastIndexOf(">");
                    if ( end > 0 ) {
                        operation.returnType = rt.substring("List<".length(), end).trim();
                        operation.returnContainer = "List";
                    }

Changing JavaCXFClientCodegen to extend AbstractJavaJAXRSServerCodegen fixed the pet store sample and the issues I was seeing in my code (i'm assuming the actual solution is not as straight-forward as that).

mhaefele avatar Mar 07 '17 16:03 mhaefele

Is there a quick guide on that (creating a pull request in general for OSS?)

For this repository (and many others), it is in CONTRIBUTING.md.

In general, you need to fork the repository, create a new branch in your fork (you could use master, but it is better to have a clear name), commit to this branch and then open a pull-request (PR) from this branch to the upstream repo's master branch (or whatever branch it should go – in our case master, unless it is a breaking change, which we reserve for the next minor release (2.3.0), so they go in this branch).

When there is a PR, automatically several CI systems will take it and give you feedback (at least in our case, might be different for other projects), and also other contributors can read and comment. If one of the maintainers agrees it is good, it will be merged. (If you think it is not yet ready, note this clearly in the initial PR comment.)

I also found this a good read: How to fix a bug in an open-source project

ePaul avatar Mar 07 '17 17:03 ePaul

Cool. Thanks so much for your help.
I abandoned using jaxrs-cxf-client in favor of jaxrs-cxf for the time being, but I'll try to come back to do this and do a pull request. Looks like at the least adding jaxrs-cxf-client to the samples directories would show the issue I'm seeing.

mhaefele avatar Mar 07 '17 17:03 mhaefele