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

[JAVA] Client code does not compile for Iterable<ConcreteClass> API return type

Open digital-abyss opened this issue 10 years ago • 1 comments

Description

I have a resource class with an Iterable as a return type:

    @GET
    @Timed
    @Path("/all")
    public Iterable<Saying> sayHellos() {
        LinkedList<Saying> sayings = new LinkedList<Saying>();

        sayings.add(new Saying(counter.incrementAndGet(), "Austin Powers"));
        sayings.add(new Saying(counter.incrementAndGet(), "Dr. Evil"));
        return sayings;
    }

Saying.java:

public class Saying {
    private long id;

    @Length(max = 3)
    private String content;

    public Saying() {
        // Jackson deserialization
    }

    public Saying(long id, String content) {
        this.id = id;
        this.content = content;
    }

    @JsonProperty
    public long getId() {
        return id;
    }

    @JsonProperty
    public String getContent() {
        return content;
    }
}

This generates the following specification:

https://github.com/digital-abyss/swagger-codegen-issue/blob/master/swagger.json

When trying to compile this using swagger-codegen, I encounter errors:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[42,31] cannot find symbol
  symbol:   class IterableSaying
  location: package io.swagger.client.model
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[220,12] cannot find symbol
  symbol:   class IterableSaying
  location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[231,24] cannot find symbol
  symbol:   class IterableSaying
  location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[244,70] cannot find symbol
  symbol:   class IterableSaying
  location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[221,21] cannot find symbol
  symbol:   class IterableSaying
  location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[233,49] cannot find symbol
  symbol:   class IterableSaying
  location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[266,49] cannot find symbol
  symbol:   class IterableSaying
  location: class io.swagger.client.api.DefaultApi
[INFO] 7 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.479 s
[INFO] Finished at: 2016-10-18T15:17:26-04:00
[INFO] Final Memory: 16M/366M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project swagger-java-client: Compilation failure: Compilation failure:
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[42,31] cannot find symbol
[ERROR] symbol:   class IterableSaying
[ERROR] location: package io.swagger.client.model
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[220,12] cannot find symbol
[ERROR] symbol:   class IterableSaying
[ERROR] location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[231,24] cannot find symbol
[ERROR] symbol:   class IterableSaying
[ERROR] location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[244,70] cannot find symbol
[ERROR] symbol:   class IterableSaying
[ERROR] location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[221,21] cannot find symbol
[ERROR] symbol:   class IterableSaying
[ERROR] location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[233,49] cannot find symbol
[ERROR] symbol:   class IterableSaying
[ERROR] location: class io.swagger.client.api.DefaultApi
[ERROR] //sanitized/path/swagger-codegen/test-client/src/main/java/io/swagger/client/api/DefaultApi.java:[266,49] cannot find symbol
[ERROR] symbol:   class IterableSaying
[ERROR] location: class io.swagger.client.api.DefaultApi
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

This is the return value of the endpoint:

[{"id":1,"content":"Austin Powers"},{"id":2,"content":"Dr. Evil"}]

If I replace Iterable interface with List, the generated source compiles.

Swagger-codegen version
commit c3a12cb3f99d643de1c2e29c5e8ddfe543b9e178
Merge: f345600 c26833f
Author: wing328 <[email protected]>
Date:   Tue Oct 18 23:22:09 2016 +0800

    Merge pull request #4027 from diega/patch-1

    [java] Allow setting test folder
Swagger declaration file content or url

https://github.com/digital-abyss/swagger-codegen-issue/blob/master/swagger.json

Command line used for generation
java/jdk1.8.0_101/bin/java -jar swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
    -i swagger.json \
    -l java \
    -o test-client
cd test-client
mvn clean package
Steps to reproduce
  • Clone https://github.com/digital-abyss/swagger-codegen-issue
  • Boot server (I did this by importing mvn project in to eclipse and adding the following arguments to the debug configuration):
server "${workspace_loc:app1}/conf.yml
  • generate JSON swagger file:
curl http://localhost:8080/swagger.json
  • Try to build client:
java/jdk1.8.0_101/bin/java -jar swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
    -i swagger.json \
    -l java \
    -o test-client
cd test-client
mvn clean package
Related issues

Could not find a related issue.

Suggest a Fix

Would be happy to assist trying to fix.

digital-abyss avatar Oct 18 '16 19:10 digital-abyss

I was able to also get this to compile using this annotation:

    @ApiOperation(value = "Return new Sayings.",
        response = Saying.class,
        responseContainer = "List")

Maybe this is expected behaviour for Iterable as a return type?

digital-abyss avatar Oct 18 '16 19:10 digital-abyss