-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Support for associating XML Comments with custom tags defined using TagsAttribute #2565
Support for associating XML Comments with custom tags defined using TagsAttribute #2565
Conversation
Great ! |
@domaindrivendev Any ETA on this? |
We could really do with this change. |
This would be a lifesaver 👍 |
This fix does not cover all cases. For example it won't work
|
Thanks for contributing - if you'd like to continue with this pull request, please rebase against the default branch to pick up our new CI. |
…ng TagsAttribute. Also includes all tags in root tags object, not just ones with XML Comments.
9843dd8
to
51e095f
Compare
Thanks @martincostello, I've rebased against your latest master branch. Edit: apologies, I've realised that the build is failing, I'll look into it when I have another chance. |
/// If customizing the default tag for operations via TagsAttribute, only the first Tag per controller will be | ||
/// associated with the description. | ||
/// However, don't set this flag if you're customizing the default tag for operations via TagActionsBy. |
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.
Let's make TagsAttribute
and TagActionsBy
<see .... />
references in these two comments.
public static class KeyValuePairExtensions | ||
{ | ||
// Explicit deconstruct required for older .NET frameworks | ||
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> kvp, out TKey key, out TValue value) | ||
{ | ||
key = kvp.Key; | ||
value = kvp.Value; | ||
} | ||
} |
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.
It would be better to only add this polyfill for the TFMs that need it. Also, can it be internal
?
swaggerDoc.Tags = swaggerDoc.Tags.OrderBy(x => x.Name).ToList(); | ||
} | ||
|
||
private class ControllerInfo |
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.
private class ControllerInfo | |
private sealed class ControllerInfo |
|
||
#if NET6_0_OR_GREATER | ||
controllerInfo.CustomTagName = | ||
group.First().MethodInfo.DeclaringType?.GetCustomAttribute<TagsAttribute>()?.Tags[0]; |
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.
This looks like it'll throw an exception if Tags
is empty or null
.
This pull request is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further changes are made. |
This pull request was closed because it has been inactive for 14 days since being marked as stale. |
Description & reference issues
As of .NET 6.0, Swagger/OpenAPI supports using TagsAttribute to rename each group of endpoints, overriding the default value of the controller name. (See also e.g. the top voted answer by Raul on this SO question)
The existing implementation of Swashbuckle's XmlCommentsDocumentFilter associates the controller's
<summary>
XML comment with the controller name. It currently has two limitations:It does not support using TagsAttribute - it only links the summary node with the controller name. See the last four comments on TagActionsBy custom tag or controller name #467, these last comments refer to the new TagsAttribute instead of TagActionsBy.
It only adds tags for controllers with a summary node. If the user has summary nodes for some controllers but not others, then the root tags object will only include a partial list of tags. This results in the side-effect that tags are not displayed in alphabetical order. Instead, tags with descriptions are displayed first, followed by tags without descriptons. Related issues:
OrderActionsBy
when only some group have description #1757Another linked issue #2530 is a result of both these limitations.
This PR fixes both these issues. If IncludeXmlComments is true:
Tests
Tests are included for the target framework .NET 6.0, which supports TagsAttribute. The SwaggerGen.Tests project currently only targets .NET 6.0, so I did not add unit tests for earlier frameworks. I built the project in .NET 5.0 and did a manual test of this case to check it works.
Potential improvements
This PR still has some limitations:
[Tags("first tag", "second tag")]
however I don't see this as a primary use case in generating the swagger file, and there is no way of providing separate XML summaries for the two tags, so this is not supported in this PR - only the first tag is supported.If it's actually useful, I would be happy to add support for these cases.