-
Notifications
You must be signed in to change notification settings - Fork 679
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
Bug 300: When an operationId has already been used; append a postfix... #756
Conversation
Hi, From swagger 2.0 spec:
I have not looked into the PR but I guess the operationId for a spesific method may change if methods are re-ordered or new ones are placed in between? |
Well, the order is actually not guaranteed but yes will usually depend on the order of declaration. So changing that order may change the designated id given. What makes a method unique in C# is of course the signature as a whole; so perhaps a better approach would be to concatenate method name and the names of parameter types? This would be pretty ugly so you would only want to do this for overloaded methods. |
Had a quick look - it would be pretty trivial to change the FriendlyId to include the parameter types for all methods; slightly less trivial to do this for overloaded methods only - as you'd need to collect duplicates first ahead of creating friendly names. I'm happy to look into either but will probably wait for feedback from the maintainer first. 😄 |
+1 for using argument names. I think the meta used to generate the path should be enough to create a predictable operationId because Swashbuckle already throws if a non-distinct method name is found within a single path :) // operationId: Foo_Get
string Get() {}
// operationId: Foo_Get_id
string Get(string id) { } Adding
|
Great addition... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would love to see this merged...
Can you please fix the conflicts:
Conflicting files
Swashbuckle.Core/Swagger/SwaggerGenerator.cs
Swashbuckle.Dummy.Core/Swashbuckle.Dummy.Core.csproj
059e68e
to
a804426
Compare
No problem! I've force pushed a rebase for the original pr; appending an incrementing integer to the end of the name. There is an unrelated failing test |
Awesome!, I hope this makes it soon... |
... and I just tested your changes, all tests are passing |
SetUpAttributeRoutesFrom(typeof(OverloadedAttributeRoutesController).Assembly); | ||
|
||
var swagger = GetContent<JObject>("http://tempuri.org/swagger/docs/v1"); | ||
var firstGetOperation = swagger["paths"]["/subscriptions"]["get"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this a bit more generic ?!?!
instead of the (var firstGetOperation, secondGetOperation & thirdGetOperation) we should create a list of all operationId, and then make sure they are unique...
See code below:
var opIds = new List<string>();
foreach (var path in swagger["paths"])
foreach (var action in path)
foreach (var op in action)
opIds.Add(op.First()["operationId"].ToString());
Assert.AreEqual(opIds.Count(), opIds.GroupBy(x => x).Count());
…o get a unique one
a804426
to
57f7079
Compare
Thought about it and I agree; a less specific test is more useful here. |
@sjmelia - I know it's been a while and I apologize for not providing feedback sooner. The operationId is most commonly used as a method/function name in client libraries that are generated from the Swagger description. Although your solution does result in a valid spec. (a good thing for sure) it would result in some confusing method names in those client libraries. In the ASP.NET Core version of Swashbuckle, I'm generating operationId from a combination of the URI (including path and query params) and HTTP verb instead of the C# method name. This prevents the conflict and hides implementation detail. The code is relatively straightforward: If we're going to solve the conflicting operationId in this version of Swashbuckle, then I'd like to follow a consistent approach. In addition, I'd like to make it optional and defaulted to the old approach, to maintain backwards compatibility in auto-generated clients unless the service provider explicitly applies this option. This should be highlighted in the docs. Any chance you'd be interested in submitting a new PR with this approach. I'll try my best to be more responsive this time around :) |
…to get a unique one. This provides a resolution for issue #300 and at least ensures that a valid Swagger spec is produced.
Many thanks for your fantastic work on Swashbuckle; i've used it many times and would love to make a contribution - let me know if any further work is required!