Customize generated model names - Swagger UI

前端 未结 2 1693
心在旅途
心在旅途 2021-02-19 20:50

I\'m trying to adjust the \"displayName\" of the model being used in an automatically generated Swagger definition.

This will only affect the Swagger names, meaning the

相关标签:
2条回答
  • 2021-02-19 21:27

    The answer is: use an IDocumentFilter:

    private class ApplyDocumentFilter_ChangeCompany : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            if (swaggerDoc.definitions != null)
            {
                swaggerDoc.definitions.Add("Company123", swaggerDoc.definitions["Company"]);
            }
            if (swaggerDoc.paths != null)
            {
                swaggerDoc.paths["/api/Company"].get.responses["200"].schema.@ref = "#/definitions/Company123";
            }
        }
    }
    

    Here is the code: https://github.com/heldersepu/SwashbuckleTest/commit/671ce648a7cc52290b4ad29ca540b476e65240e6

    And here is the final result: http://swashbuckletest.azurewebsites.net/swagger/ui/index#!/Company/Company_Get

    0 讨论(0)
  • (Accounts for .net core:)
    I would combine the display attribute of the class with a custom swagger schema:

    [DisplayName("NewCompany")]
    public class Company
    {
    }
    

    and then in Startup:

    services.AddSwaggerGen(c =>
    {                     
        c.CustomSchemaIds(x => x.GetCustomAttributes<DisplayNameAttribute>().SingleOrDefault().DisplayName);
    });
    

    see also this answer

    0 讨论(0)
提交回复
热议问题