【发布时间】:2021-11-25 07:04:51
【问题描述】:
将 Swashbuckle.AspnetCore 从版本 5.3.0 升级到 6.1.4 后,我遇到了与swagger.json 文件相关的问题,如下图所示。每个 example 属性中都有特殊字符 (BOM - 0xFEFF)。它使 JSON 文件格式不正确。
我尝试在其他项目中重现,但没有发生。
配置 Swagger 生成
private static void RegisterSwagger(IServiceCollection services, IEnumerable<OpenApiServer> openApiServers)
{
services.AddSwaggerGen(options =>
{
foreach (var api in ApiInfo.ApiVersions)
{
options.SwaggerDoc(api.Version, new OpenApiInfo
{
Title = api.Title,
Version = api.Version,
Description = api.Description,
Contact = api.Contact
});
}
options.ExampleFilters();
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
options.DocumentFilter<DocumentFilter>();
options.CustomSchemaIds(x => x.FullName);
// OAS Rule: oas3-api-servers
foreach (OpenApiServer server in openApiServers)
{
options.AddServer(server);
}
// OAS Rule: operation-operationId
// Use method name and HTTP method as operationId
options.CustomOperationIds(apiDesc =>
{
return apiDesc.TryGetMethodInfo(out MethodInfo methodInfo) ? $"{methodInfo.Name}_{apiDesc.HttpMethod}" : null;
});
// OAS Rule: query-strings-camel-case
options.DescribeAllParametersInCamelCase();
});
services.AddSwaggerExamplesFromAssemblyOf<DataRequestSwaggerExample>();
}
【问题讨论】:
标签: asp.net-core asp.net-web-api swashbuckle swashbuckle.aspnetcore