Search code examples
asp.net-mvcswaggerswashbuckle

Modify Swagger Definition Names for MVC API


I would like to modify the Swagger schema object names that are auto generated for my MVC application.

For example, I have number of entities like EmployeeModel or CompanyModel objects that are accessible via our REST endpoints

However, it's not necessary or desired that the Swagger definition contains the word "Model" after each entity name. They should be known only as Employee or Company -- see example in screen shot.

enter image description here


Solution

  • You have to use the SchemaId function available in your SwaggerConfig.cs file in the EnableSwagger method as follows:

    c.SchemaId(t =>
    {
        c.SchemaId(t => t.FullName.EndsWith("Model") ? t.FullName.Replace("Model", String.Empty) : t.FullName);
    });
    

    This methods allows you to override Swashbuckle default behavior and set complex type names properly.

    My sample addresses directly the removela of the "Model" part of your boject names, but could be using more complex setup.