Using Interfaces Results in _1 on OperationId
| Q | A |
|---|---|
| Bug or feature request? | Bug |
| Which Swagger-Core version? | 1.5.18 |
| Which Java version? | 1.8u151 |
| Which JAX-RS framework & version? | RESTEasy 3.1.4 with JAX-RS 2.0 |
I believe this is related to 2643.
I have some endpoints which are setup with an interface plus an implementation class. The interface has the annotations (eg. JAX-RS, Swagger) while my implementation class contains all of the logic for when an endpoint is called. Swagger Core will parse both the interface and the implementation class resulting in the same methods being scanned twice. This results in Swagger Core thinking the same method has been implemented twice resulting in the OperationId being labelled as an _1 after the second pass. Swagger Core is able to tell that the methods are from the same endpoint but is not able to tell that the method has already been scanned before.
I'm assuming that scanning classes which implement interfaces but contain no new Swagger annotations is intentional so the issue lies in Swagger Core not knowing that a method has already been parsed.
| Q | A |
|---|---|
| Which Java version? | Java(TM) SE Runtime Environment (build 1.8.0_162-b12) |
| Which swagger-codegen used? | swagger-codegen-cli-2.3.1.jar |
I've encountered the same issue as @PeaceInColour with the generated SDK for JavaScript (with swagger-codegen-cli-2.3.1.jar) via the annotated interface along with the implementation class in Java.
Unlike #2643 , all the method names of the given API endpoint has 1 appended to it without any exception.
The result impacts all the generated markdown readme files *.md as well as the underlying source code.
e.g.
In *.md files:
createFileList1
listFileLists1
deleteFileList1
In *.js files:
this.createFileList1 = function(...) {...}
...
this.listFileLists1 = function(...) {...}
...
this.deleteFileList1 = function(...) {...}
I suspect this might be a common issue, any suggestions or workaround would be much appreciated.
Looking through the code base and some of the test cases it looks like the situations being considered assume that anytime a method appears twice then it is a duplicate. This shows in Reader.java's getOperationId method which assumes that anytime a method with the same name appears it must be a duplicate. This makes sense for the test cases and examples where both the interface and implementation class contain JAX-RS annotations (eg. ReaderFixFor1959Test's testClassWithImplementedInterface test). I can only speak for RESTEasy but scenarios such as this can occur:
public interface SomeInterface {
@GET
@Path("/example")
@ApiOperation (value = "Example")
SomeResponse getExample();
}
public class SomeClass implements SomeInterface {
@Override
public SomeResponse getExample() {
...
}
}
"/example": {
"get": {
"summary": "Example",
"operationId": "getExample_1"
}
Here there should not be any duplicate endpoints as RESTEasy interprets there being only one getExample endpoint. However, Swagger Core mistakenly thinks there is a duplicate getExample endpoint. This is because Swagger Core would scan both SomeInterface and SomeClass, see the identically named methods, and mark whichever method gets scanned second as a duplicate.
I'd suggest that Swagger Core should not mark a method as being a duplicate if it's implementing a method from an interface or a base class, and the method does not contain any additional JAX-RS annotations which will change the functionality of the method (eg. no @Path). If there are Swagger Annotations on the method in the derived/implemented method but no overriding JAX-RS annotations then the Swagger Annotations should overwrite the Swagger Annotations in the interface or base class. Thoughts?
After looking through the logic for determining when a duplicate endpoint exists I don't believe this is worth fixing immediately. It's easy to write a fix for this issue but an overhaul to the logic will be required to fix this case without breaking any other scenarios (eg. ReaderFixFor1959Test). This fix doesn't seem worth it right now when the workaround to just have Swagger Core ignore any implementation classes (@Api(hidden = true)) works.