Using Web API and using swashbuckle to generate swagger documentation, I defined two different classes with the same name in two different namespaces. when I open swagger page in my browser it says
Conflicting schemaIds: Duplicate schemaIds detected for types A and B. See the config setting - "UseFullTypeNameInSchemaIds" for a potential workaround
full message:
500 : {"Message":"An error has occurred.","ExceptionMessage":"Conflicting schemaIds: Duplicate schemaIds detected for types A and B. See the config setting - "UseFullTypeNameInSchemaIds" for a potential workaround","ExceptionType":"System.InvalidOperationException","StackTrace":" at Swashbuckle.Swagger.SchemaRegistry.CreateRefSchema(Type type)\r\n at Swashbuckle.Swagger.SchemaRegistry.CreateInlineSchema(Type type)\r\n at Swashbuckle.Swagger.SchemaRegistry.b__1f(JsonProperty prop)\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable 1 source, Func
2 keySelector, Func 2 elementSelector, IEqualityComparer
1 comparer)\r\n at 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 at Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable 1 apiDescriptions, SchemaRegistry schemaRegistry)\r\n at Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.<GetSwagger>b__4(IGrouping
2 group)\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable 1 source, Func
2 keySelector, Func 2 elementSelector, IEqualityComparer
1 comparer)\r\n at Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion)\r\n at Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.HttpServer.d__0.MoveNext()"} http://localhost:24215/swagger/docs/v1
I don't want to change my classes' names. How can I fix it?
7条答案
按热度按时间56lgkhnf1#
swagger JSON中的每个类都必须有唯一的schemaId。
Swashbuckler尝试只使用类名作为一个简单的schemaId,但是如果您在不同的名称空间中有两个类具有相同的名称(如您所做的),这将不起作用。
如错误所示,您可以使用配置设置"UseFullTypeNameInSchemaIds *"作为潜在的解决方法(更新:在最新版本中不可用)
在较新的版本中,您可以通过选项.CustomSchemaIds(x =〉x. FullName)实现相同的行为。
下面是一个例子:
有关详细信息http://wegotcode.com/microsoft/swagger-fix-for-dotnetcore/
xfb7svmp2#
我终于在swagger configs中找到了一种方法。转到
App_Start\SwaggerConfig.cs
文件,在EnableSwagger
lambda表达式下添加以下行:完整代码如下所示:
czfnxgou3#
我使用的是Asp.netCore 2.1。当我尝试显示Swagger UI时出现此错误。
我是这样解决这个问题的:
在
Starup.cs
中,在ConfigureServices()
中加上c.CustomSchemaIds(i => i.FullName);
参见以下示例:
wwwo4jvm4#
如果注解掉或添加:
在那个部分,它似乎做同样的事情。
js81xvg65#
对于Swashbuckle.AspNetCore5.2.1(在. NETCore3.1上),Swashbuckle配置API似乎缺少旧解决方案中描述的选项,相反,
Startup.cs
中的以下更改对我很有效:f87krz0w6#
如果您的模型包含泛型类型,请考虑使用
Type.ToString()
而不是Type.FullName
,以摆脱为泛型参数类型生成的程序集信息,并使您的schema id更加一致。显示
List<string>
上差异的示例:jum4pzuy7#
在我的例子中,我在2个微服务项目中有2个相同的类(A和B)。
当我打开项目(A)中的dependencies文件夹时,我发现我错误地将另一个项目(B)添加为对此项目的引用。
在删除我的两个项目之间的依赖关系之后,冲突就解决了。