-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Turn off unique operationId number suffixes, Add RequestMapping path to operationId #1224
Comments
I ended up adding nicknames to every method prefixed with the request path (making them unique across the whole application, not only for the current controller request path). However, currently the Springfox libraries still add the "_1" suffix to most methods (some methods are not suffixed for unknown reason). |
I reverted back to 2.2.0, where the nicknames are picked up correctly without any suffixing of numbers and are only suffixed with the http verb. |
@jfiala/ @zsavic The generated method names are be predictable and unique. The functional tests verify this. Meaning it should NOT be changing when you restart services etc. If it does its a problem. Now the reason its adding For issues like this it would be great if you could create a simple reproducing example so that its easier for me to diagnose. A good place to start is to use the springfox-demos and get it to fail like you're seeing happen. Also 2.2.0 is probably one of the more buggy versions. I'd recommend sticking to 2.4.0 |
Regarding the "_1": this in fact appears with one Docket as soon as I have more than one controller on the path with nicknames attached to the method names. So it has nothing to do with multiple Dockets. It has only to do with multiple controllers, as soon as they have the same signature (here: sayHello(String name)! If I change the method name, the nickname-suffix disappears (e.g. sayHello2(String name) or sayHello(String name2)! With one controller, there is no "_1" suffix. As soon as there are two controllers with a method with the same signature, there is a suffix at the end of the operationId: my-controller: operationId: mypath_sayHello @RestController
@RequestMapping("/test/test")
@Api(value="test")
public class TestController {
@RequestMapping(method=RequestMethod.GET, value="/sayHello")
@ApiOperation(value="", nickname = "test_sayHello")
public String sayHello(@RequestParam String name) {
return "hello : " + name;
}
}
@RestController
@RequestMapping("/test/mypath")
@Api(value="mypath")
public class MyController {
@RequestMapping(method=RequestMethod.GET, value="/sayHello")
@ApiOperation(value="", nickname = "mypath_sayHello")
public String sayHello(@RequestParam String name) {
return "hello : " + name;
}
} Since usually methods have have identical signatures in different controllers (e.g. public MyObject getbyId(Long id)), this results in "_1" suffixed to unique nicknames, and its resulted in "_7" suffixed in one of my controllers (as I have 7 controllers with identical method signatures...). |
I've been able to track this down comparing with the boot example - this was causing it in my Spring XML configuration: <mvc:annotation-driven /> This had no other effect than somehow causing cycles in the nickname calculation. However, I have still to add nickname to each method, otherwise they are suffixed, although they are unique in their scope! |
One additional thing I noted when switching between 2.4.0 and 2.2.0 (pls let me know if I should post a separate question): |
To sum things up (my test results with my configuration): 2.) If no nicknames are used, the method names are also checked for duplicates across the whole application. 3.) There is no way to get rid of the "UsingVERB" suffix without adding nicknames currently. I think it would be nice to be able to configure this in the Docket or so? Please let me know if I should add an example PR in the demos? |
@jfiala that is by design. That is what the spec says
So Regarding #3 you should be able to implement your own |
I found this interpretation regarding the spec: In fact, the term API refers to per API documentation/Swagger description provided, i.e. per Docket. I added a question there to clarify if the scope interpretation can be narrowed down further. However, until these things are clarified:
What do you think, does Option A make sense to you? |
From what I remember earlier Option A is hard to implement such that the operation names are consistent and predictable and not dependent on spring bean loading order etc. |
I understand this, so let the operationId unique for the sake of the current interpretation of the OpenAPI spec. I'll adapt Swagger-Codegen to ignore the operationId, so everything is fine at the client code. |
In case anyone else is running into this. I experienced this same behavior due to having two dockets. I have a customer docket with a filtered view of the API and an admin docket with all endpoints listed. Unfortunately the docs generated with the _1 are on the customer api-docs group. This leads to silly looking SDK methods with a 1 at the end. I read all the suggestions, but i ended up with something a little different. I decided to use controller advice and attach it to the Swagger2Controller to alter the document before its written to the response. This gives me full control of the output. I felt is was pretty elegant until something better is added. This can obviously be extended to do more, but its working for me at the moment.
|
If anyone else is having this problem or similar the simplest solution for me was to override operationName Generator which was causing the issue. the solution was to provide custom counter
You can also implement custom caching: I don't know why this was not done, since duplicate operationId breaks the swagger, should it not be responsibility of the developer to fix it? |
@sarief It has been implemented, You need to subclass |
@dilipkrish oh, good. I must have old version then, thanks for pointer 👍 |
@dilipkrish What do you think about allowing to configure a The proposed work around avoids suffixes like |
I wound up going with the following workaround which makes the generated names unique within a docket:
|
@dilipkrish Could this be reopened? In my opinion it is a desirable feature to be able to have the |
This operation will slow down the startup of the environment if the code is generated automatically
You can see that it took more than ten seconds in the middle These methods are generated by code generator and have similar crud names in different controllers. Do you consider that this scan has performance problems How can I solve it? |
I have a very simple use case with unique nicknames:
Two controllers:
The operations can be called:
http://localhost:8080/test/sayHello?name=test2
http://localhost:8080/test2/sayHello?name=test3
Swagger Springfox currently generates this operationId's for this:
"operationId": "test_sayHello_1",
"operationId": "test2_sayHello_1",
1.) Why is Springfox adding the suffix "_1" here, as the nicknames are unique already?
IMHO it would be only necessary to add the suffixes, if the nicknames provided are not unique.
2.) Is it possible to have the RequestMapping path always as part of the operation name.
e.g. "operationId": "test_sayHello", for @RequestMapping("/test").sayHello()
Then the uniqueness of the operationId would be provided by default, as the compiler would guarantee no duplicate method names in the class.
If this is not possible now, is there a way to override the naming behaviour for operationIds to implement this on my own?
The text was updated successfully, but these errors were encountered: