question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TagActionsBy custom tag or controller name

See original GitHub issue

Hi,

I was looking for a solution where I could group the actions of one or more controllers under a custom tag. I did a bit of searching round and found this section in the documentation. I dug into it and managed to produce a solution where I assign a custom attribute to controller(s) that I want to be tagged together. I then created an extension method for the ApiDescription class that will either extract the name in my custom attribute or use the name of the controller if the attribute is not present. This has worked, however, it was a little painful getting the name of the controller when my attribute was not present. I ended up having to duplicate some of the code in the ApiDescriptionExtensions class. Ideally, I would have just used the ControllerName extension method but this has been marked as internal.

I have two questions:

  1. Is there a reason why the ControllerName extension method is marked as internal? If not, would it be possible to make it public?
  2. Is there an alternative approach to what I have done which achieves the same result?

Here is the code that I have produced to achieve this.

public class SwaggerGroupAttribute : Attribute
{
    public string GroupName { get; }

    public SwaggerGroupAttribute(string groupName)
    {
        GroupName = groupName;
    }
}

public static class ApiDescriptionExtensions
{
    public static string GroupBySwaggerGroupAttribute(this ApiDescription api)
    {
        var groupNameAttribute = (SwaggerGroupAttribute)api.ControllerAttributes().SingleOrDefault(attribute => attribute is SwaggerGroupAttribute);

        // ------
        // Lifted from ApiDescriptionExtensions
        var actionDescriptor = api.GetProperty<ControllerActionDescriptor>();
            
        if (actionDescriptor == null)
        {
            actionDescriptor = api.ActionDescriptor as ControllerActionDescriptor;
            api.SetProperty(actionDescriptor);
        }
        // ------

        return groupNameAttribute != null ? groupNameAttribute.GroupName : actionDescriptor?.ControllerName;
    }
}

// In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddSwaggerGen(c => 
    {
        // ...
        c.TagActionsBy(api => api.GroupBySwaggerGroupAttribute());
    }
}

// ******
// Example usage
[SwaggerGroup(“Grouped”)]
public class GroupedAController : Controller { }

[SwaggerGroup(“Grouped”)]
public class GroupedBController : Controller { }

public class NonGroupedController : Controller { }

public class AnotherController : Controller { }

// This results in three groups on the swagger page
//
// - Grouped
// - NonGrouped
// - Another

  

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
ckarczcommented, Jan 16, 2020

@domaindrivendev custom tags and tag descriptions (from controller xml docs) do not play well together. do you have any nice workaround in mind where I can link a custom tag to a controller’s description?

5reactions
enriquecorpcommented, Mar 9, 2022

Actually, in .net6 I’m using TagsAttribute image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does Swagger (Asp.Net Core) have a controller description?
Customize Operation Tags (e.g. for UI Grouping)​​ The Swagger spec allows one or more "tags" to be assigned to an operation. The Swagger ......
Read more >
Swagger Grouping With Controller Name Fallback Using ...
As we're moving toward ASP.NET Core we encountered some difficulty in achieving the same grouping behavior with ASP.NET Core using Swashbuckle.
Read more >
Developers - TagActionsBy custom tag or controller name -
Hi,. I was looking for a solution where I could group the actions of one or more controllers under a custom tag.
Read more >
Swashbuckle.AspNetCore
Customize Operation Tags (e.g. for UI Grouping). The Swagger spec allows one or more "tags" to be assigned to an operation. The Swagger...
Read more >
Is there a way change the Controller's name in the swagger-ui ...
i was looking to change the name of my controller to something more friendly or in another language. the best i could find...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found