Ask a Question

Generated Java code (swagger) does not compile

ric1
Regular Visitor

Generated Java code (swagger) does not compile

I'm trying to create e Java client defined with swagger. I have generated the source code automatically in three ways.

online https://editor.swagger.io/
java -jar swagger-codegen-cli-3.0.20.jar generate -i https://www.xxxxxx.xxx/xxxxxx/swagger.json -l java
swagger-codegen generate -i https://www.xxxxxx.xxx/xxxxxx/swagger.json -l java
Unfortunately, the generated source code cannot be compiled in all ways. When I execute

mvn clean install

I get many errors of this type:

A.java cannot inherit from final B

I have analyzed the error and found the following pattern many times.

public class A extends B {
....
}
public enum B {
....
}
I know that the swagger provider works with c# and in this language this construct should be allowed.

I see the following in jason

"xxxx.xxxx.xx.B": {
"enum": [
"A",
"B",
"C",
"D"
],
"type": "string"
}

...

"b": {
"allOf": [
{
"$ref": "#/xxxx/xxxx/xxxx.xxxx.xx.B"
}
],
"description": "abc",
"nullable": true
}
I am not allowed to publish the swagger URL.

Is there anything I can do to build a Java client anyway?
1 REPLY 1
dsinghvi77
Occasional Contributor

Hey @ric1 -- 

 

1) If you post your open api spec happy to help you debug in more detail.(I have heard the Native Generator: https://openapi-generator.tech/docs/generators/java works the best) 

2) My team has been working on a Swagger alternative that really focuses on codegen (as Swagger was initially built for docs) and we are calling it Fern. Repo here. Demo video here.
It is as easy as 

npm install -g fern-api (to install)
fern init # initializes a basic fern api
fern add java # adds java generator
fern generate

 

cancel
Showing results for 
Search instead for 
Did you mean: