Mah*_*ahi 43 c# asp.net-web-api swagger-ui asp.net-web-api2 swashbuckle
使用Web API并使用swashbuckle生成swagger文档,我在两个不同的命名空间中定义了两个具有相同名称的不同类.当我在浏览器中打开swagger页面时说
冲突的schemaIds:为类型A和B检测到重复的schemaId.请参阅配置设置 - "UseFullTypeNameInSchemaIds"以获取潜在的解决方法
完整消息:
500:{"消息":"发生了错误.","ExceptionMessage":"冲突的schemaIds:为类型A和B检测到重复的schemaIds.请参阅配置设置 - \"UseFullTypeNameInSchemaIds \"以获取潜在的解决方法","ExceptionType ":"System.InvalidOperationException","StackTrace":"在Swashbuckle.Swagger.SchemaRegistry.CreateRefSchema(类型类型)\ r \n,在Swashbuckle.Swagger.Swagger.SchemaRegistry.CreateInlineSchema(类型类型)\ r \n在Swashbuckle.Swagger. SchemaRegistry.b__1f(JsonProperty prop)\ r \n在System.Linq.Enumerable.ToDictionary [TSource,TKey,TElement](IEnumerable
1 source, Func2 keySelector,Func2 elementSelector, IEqualityComparer1 comparer )\ r \n在Swashbuckle.Swagger.SchemaRegistry.CreateObjectSchema(JsonObjectContract jsonContract) )\ r \n at Swashbuckle.Swagger.SchemaRegistry.CreateDefinitionSchema(Type type)\ r \n at Swashbuckle.Swagger.SchemaRegistry.GetOrRegister(Type type)\ r \n at Swashbuckle.Swagger.SwaggerGenerator.CreateOperation(ApiDescription apiDesc,SchemaRegistry) schemaRegistry)\ r \n在Swashbuckle.Swagger.SwaggerGenerator.CreateP athItem(IEnumerable的1 apiDescriptions, SchemaRegistry schemaRegistry)\r\n at Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.<GetSwagger>b__4(IGrouping2组)\ r \n在System.Linq.Enumerable.ToDictionary [TSource,TKEY的,TElement](IEnumerable的1 source, Func2的KeySelector,Func键2 elementSelector, IEqualityComparer1比较器)\ r \n在Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(字符串使用rootUrl,字符串apiVersion)\ r \n在Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)\ r \n在System.Net.Http.MttageInvoker.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)\ r \n在System. Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)\ r \n在System.Web.Http.HttpServer上的System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)\ r \n. d__0.MoveNext()"} http:// localhost:24215/swagger/docs/v1
我不想改变班级的名字.我该如何解决?
Ghe*_*wet 66
swagger JSON中的每个类都必须具有唯一的schemaId.
Swashbuckler尝试将类名称用作简单的schemaId,但是如果在不同的名称空间中有两个具有相同名称的类(就像你一样),这将无法工作.
如错误所示,您可以使用配置设置" UseFullTypeNameInSchemaIds "进行潜在的解决方法.
如果您关心的是如何在.NetCore中间件中使用"UseFullTypeNameInSchemaIds"?您可以通过options.CustomSchemaIds(x => x.FullName)实现相同的行为.
这是一个例子:
services.ConfigureSwaggerGen(options =>
{
//your custom configuration goes here
...
// UseFullTypeNameInSchemaIds replacement for .NET Core
options.CustomSchemaIds(x => x.FullName);
});
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请访问http://wegotcode.com/microsoft/swagger-fix-for-dotnetcore/
Mah*_*ahi 32
我终于找到了摆弄招架的方法.转到App_Start\SwaggerConfig.cs文件并在EnableSwaggerlambda表达式下添加以下行:
c.SchemaId(x => x.FullName);
Run Code Online (Sandbox Code Playgroud)
完整代码是这样的:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
// your configs...
c.SchemaId(x => x.FullName);
// other configs...
})
.EnableSwaggerUi(c =>
// ....
});
Run Code Online (Sandbox Code Playgroud)
小智 14
我使用的是Asp.net Core 2.1.当我尝试显示Swagger UI时出现此错误.
我这样解决了问题:
在Starup.cs中,ConfigureServices()加c.CustomSchemaIds(i => i.FullName);
见下面的例子:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Title = "ASP.NET Core 2.1+ ConsumerApp API",
Version = "v1"
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
c.CustomSchemaIds(i => i.FullName);
});
Run Code Online (Sandbox Code Playgroud)
如果您注释掉或添加:
c.UseFullTypeNameInSchemaIds();
Run Code Online (Sandbox Code Playgroud)
在那一节中,似乎做同样的事情.
对于 Swashbuckle.AspNetCore 5.2.1(在 .NET Core 3.1 上),Swashbuckle 配置 API 似乎缺少旧解决方案中描述的选项。相反,以下更改Startup.cs对我有用:
services.AddSwaggerGen(c =>
{
// Existing configuration
// Tweak to the Schema generator
c.SchemaGeneratorOptions = new SchemaGeneratorOptions {SchemaIdSelector = type => type.FullName};
}
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您的模型包含泛型类型,请考虑使用Type.ToString()而不是Type.FullName删除为泛型参数类型生成的程序集信息,并使您的架构 ID 更加一致。
services.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.ToString());
});
Run Code Online (Sandbox Code Playgroud)
显示差异的示例List<string>:
Console.WriteLine(typeof(List<string>).FullName);
Console.WriteLine(typeof(List<string>).ToString());
// Output:
// System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
// System.Collections.Generic.List`1[System.String]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19957 次 |
| 最近记录: |