3

I am trying to generate a java API using swagger-codegen (3.0.0) I am using the swagger-codegen-cli-3.0.24.jar version of codegen jar. I ran the following command to generate the SDK for the sample petstore project:

java -jar swagger-codegen-cli.jar generate -i code/swagger-codegen/modules/swagger-codegen/src/test/resources/3_0_0/petstore.json -l java -o out/pet-java

When I am compiling the generated sdk project:

cd out/pet-java
mvn clean package

I am getting the below error:

Compilation failure
[ERROR] out/pet-java/src/main/java/io/swagger/client/model/Pet.java:[87,34] cannot find symbol
[ERROR]   symbol:   method nextObject()
[ERROR]   location: variable jsonReader of type com.google.gson.stream.JsonReader
it is using <gson-version>2.8.1</gson-version> as dependency.
AmanSinghal
  • 2,404
  • 21
  • 22
  • 1
    The codegen seems to be broken. There is not `nextObject` in [JsonReader](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonReader.html). – terrorrussia-keeps-killing Jan 22 '21 at 13:24
  • did you also face the same problem? – AmanSinghal Jan 23 '21 at 03:21
  • 1
    I don't use Swagger 3, still on Swagger 2 (+codegen), I'm only telling that the code generated by the codegen cannot be compiled because the codegen generates illegal code. – terrorrussia-keeps-killing Jan 23 '21 at 06:55
  • I'm getting the same error generating java code from swagger based on the Strava API: https://developers.strava.com/swagger/swagger.json. For their API, they use swagger 2.x. – Rich von Lehe Jan 24 '21 at 18:36
  • Fixing my comment above, I get the same error when compiling the generated java: there is no method nextObject() in JsonReader. – Rich von Lehe Jan 24 '21 at 19:16
  • Thanks for sharing your inputs, did you find any workaround or solution to fix this? I also configured my machine to use JDK 8, but the problem persists. – AmanSinghal Jan 25 '21 at 00:21
  • I haven't found a solution yet. I added similar comments to a couple of issues, including yours, on the swagger-codegen github page. There were 2500 issues already so I'm not sure how likely a quick fix will be. – Rich von Lehe Jan 25 '21 at 01:01
  • All of the offending code is in these nested static Adapter classes derived from TypeAdapter. I'm trying to figure out what the code is trying to do and if there's a way to do it with existing calls to the JsonReader. This is a hobby project for me so I'm not spending a lot of time on it. – Rich von Lehe Jan 25 '21 at 01:05
  • It seems to have been fixed in 3.0.25 https://github.com/swagger-api/swagger-codegen-generators/commit/8f23a89ce4c75597b8deb82e010fa1e8037168d8 – Alexander Ishmuradov Mar 26 '21 at 16:28

1 Answers1

3

Ultimately the real answer is for swagger-codegen to fix their regression. They've admitted that it's a problem. In the mean time I've worked around the issue for my particular situation. To demonstrate the types of changes that are necessary, see below:

enter image description here

In some files instead of nextObject(), there may be a call to next(). The solution is the same, however.

Apologies for any confusion in the diff. Apparently I left the original lines in the file commented out below the working replacement line.

Rich von Lehe
  • 1,362
  • 2
  • 16
  • 38