Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

Support for associating XML Comments with custom tags defined using TagsAttribute

Open yingxinj opened this issue 3 years ago • 7 comments

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:

  1. It does not support using TagsAttribute - it only links the summary node with the controller name. See the last four comments on #467, these last comments refer to the new TagsAttribute instead of TagActionsBy.

  2. 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:

    • #2162
    • #1757

Another linked issue #2530 is a result of both these limitations.

This PR fixes both these issues. If IncludeXmlComments is true:

  • then the resulting tag names & descriptions will support the new TagsAttribute, falling back to the default (controller name) if TagsAttribute is not used.
  • then the resulting root tags object will contain all controller-level tags, regardless of whether they have an XML summary node or not. Those without a summary node will not have any "description" field.

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:

  • TagsAttribute theoretically allows you to specify more than one custom tag e.g. by [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.
  • TagsAttribute can be specified at the method level, if so, such tags would still be omitted from the root tags object.

If it's actually useful, I would be happy to add support for these cases.

yingxinj avatar Dec 05 '22 20:12 yingxinj

Great ! When will this fix added on NuGet?

NilkOne avatar Jan 05 '23 09:01 NilkOne

@domaindrivendev Any ETA on this?

silkfire avatar Apr 14 '23 13:04 silkfire

We could really do with this change.

Skintkingle avatar Aug 11 '23 13:08 Skintkingle

This would be a lifesaver 👍

sirmeepington avatar Oct 27 '23 20:10 sirmeepington

This fix does not cover all cases. For example it won't work

 public abstract class ConfigurationControllerBase : ControllerBase
{
}

[Tags("ConfigurationController ")]
public class ConfigurationController : ConfigurationControllerBase 
{
}

DenKn avatar Nov 28 '23 22:11 DenKn

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.

martincostello avatar Apr 13 '24 08:04 martincostello

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.

yingxinj avatar Apr 15 '24 19:04 yingxinj

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.

github-actions[bot] avatar Jun 24 '24 01:06 github-actions[bot]

This pull request was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jul 13 '24 01:07 github-actions[bot]