Search code examples
c#asp.net-coreswaggerswashbuckle.aspnetcore

How to add hyphen in class title of swagger.json


I am using Asp .net core 3.1. I want to update the controller name in swagger.json of Swashbuckler.AspNetCore which is a auto generated file. for Example class title as School-Admin

I am using RestFul APIs, In Restful APIs, we use hyphen separated entities name For example

Controller School-Admin
[GET] api/school-admins/1

As c# doest not allow hyphen in class name so I can't change my controller name..I want to achieve this usinh swagger..json

To achieve this I have added a hyphen in [Route("api/school-admins")] Right now, It works for Endpoints only and doest not add hyphen in class title. it shows the API and class name on swagger UI as:

SchoolAdmin => Class Title not hyphen separated on swagger UI yet
GET api/school-admins/{id} => API

My goal is to display class name hyphen separated on swagger UI since swash buckle creates swagger.json automatically so it adds the class name same as mentioned in .cs file which is not hyphen separated due to language restrictions. is it possible to change my class title in swagger.json without affecting auto generated functionality? I am using Swashbuckle.AspNetCore in which swagger.json generates automatically and also that file is non editable. Can I change the class name in this auto-generated swagger.json and push it into source control but it should not affect my auto-generated functionality of swagger.json.For example, if I add a new API or controller in the future swagger.json should be auto-updated with new changes only Dev need to add new class name hyphen separated if required.


Solution

  • Posting to help other I solved it using APIExplorerSettings I was unaware of that before. In startup.cs I use

    SwaggerGen(){
    options.DocInclusionPredicate((_, api) => !string.IsNullOrWhiteSpace(api.GroupName));
    options.TagActionsBy(api => new[] { api.GroupName });}
    

    in Classes

    [ApiExplorerSettings(GroupName = "school-admin")]
    

    and see the magic