swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B

swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B

使用 Web API 并使用 swashbuckle 生成 swagger 文档, 我在两个不同的命名空间中定义了两个具有相同名称的不同 类。当我在浏览器中打开 swagger 页面时,它显示

Conflicting schemaIds: Duplicate schemaIds detected for types A and B. See the config setting - "UseFullTypeNameInSchemaIds" for a potential workaround

完整消息:

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](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 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(IEnumerable1 apiDescriptions, SchemaRegistry schemaRegistry)\r\n at Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.<GetSwagger>b__4(IGrouping2 group)\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 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

我不想更改 类' 的名字。我该如何解决?

我终于找到了 swagger 配置的方法。转到 App_Start\SwaggerConfig.cs 文件并在 EnableSwagger lambda 表达式下添加此行:

c.SchemaId(x => x.FullName);

完整代码如下:

GlobalConfiguration.Configuration 
    .EnableSwagger(c =>
    {
        // your configs...

        c.SchemaId(x => x.FullName);

        // other configs...
    })
    .EnableSwaggerUi(c =>
        // ....
    });

如果您注释掉或添加:

c.UseFullTypeNameInSchemaIds();

在那个部分,它似乎做同样的事情。

swagger JSON 中的每个 class 都必须具有唯一的 schemaId。

Swashbuckler 尝试仅使用 class 名称作为简单的 schemaId,但是如果您在不同的名称空间中有两个具有相同名称的 classes(就像您所做的那样),这将不起作用。

如错误所示,您可以使用配置设置“UseFullTypeNameInSchemaIds*”作为潜在的解决方法(更新:在最新版本中不可用)

在较新的版本中,您可以通过选项实现相同的行为。CustomSchemaIds(x => x.FullName).

这是一个例子:

   services.ConfigureSwaggerGen(options =>
   {
       //your custom configuration goes here

...

  // UseFullTypeNameInSchemaIds replacement for .NET Core
       options.CustomSchemaIds(x => x.FullName);
   });

了解更多信息http://wegotcode.com/microsoft/swagger-fix-for-dotnetcore/

我正在使用 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);
        });

如果您的模型包含通用类型,请考虑使用 Type.ToString() 而不是 Type.FullName 来去除为通用参数类型生成的程序集信息并使您的架构 ID 更加一致。

services.AddSwaggerGen(options =>
{
    options.CustomSchemaIds(type => type.ToString());
});

显示 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]

对于 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};
  }