Mike
Mike

Reputation: 807

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

Upvotes: 1

Views: 2076

Answers (2)

n.prokhorov
n.prokhorov

Reputation: 930

What library are you using to build you swagger shema? Take a look at Swashbuckle . This allows to build swagger shema automatically for your app (by using xml-documentation) and is flexible for customization. See "Modifying Generated Schemas" in documentation.

Upvotes: 0

Benjamin Soulier
Benjamin Soulier

Reputation: 2263

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.

Upvotes: 2

Related Questions