[英]How to resolve Swashbuckle/Swagger -UI model naming issue for generics?
我想為我的 webapi 清理 swagger ui 中的模型名稱。
問題是,當我在端點上定義響應類型並且該類型是泛型時,它的命名方式是,如果使用像 NSwag 這樣的 swagger-gen 工具,模型會得到最糟糕的名稱,(因為它們基於 swagger,所以我想定義 model 的命名方式)。
這是響應屬性:
[ProducesResponseType(typeof(BulkUpsertResponse<AccountingCode>), StatusCodes.Status200OK)]
This is the resultant name: Company.IntegrationApi.Api.Models.Responses.BulkUpsertResponse1[[Company.IntegrationApi.Api.Models.AccountingParameters.AccountingCode, Company.IntegrationApi.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
什么 NSwag 使它成為: BulkUpsertResponse_1OfOfAccountingCodeAndApiAnd_0AndCulture_neutralAndPublicKeyToken_null
我想要的名字是: BulkUpsertResponse<AccountingCode>
我試圖找到一個屬性或標簽來定義名稱,並且將 generics 變成“硬類型”是不可行的,因為它有數百種用途。
如何准確指定名稱,或者我可以使用哪些變通方法來解決我的問題?
添加 Swashbuckle.Swagger NuGet package。 然后在你的 init 方法中使用
CustomSchemaIds(i => i.FriendlyId(true));
見下例:D
using Swashbuckle.Swagger;
///then in configure services..
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Your API",
Description = "Interfaces available for API",
});
c.CustomSchemaIds(x => x.FullName);
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
c.CustomSchemaIds(i => i.FriendlyId(true));
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.