swagger 摆动误差:冲突的架构ID:检测到类型A和B的重复schemaId

vnjpjtjt  于 2023-03-08  发布在  其他
关注(0)|答案(7)|浏览(360)

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?

56lgkhnf

56lgkhnf1#

swagger JSON中的每个类都必须有唯一的schemaId。
Swashbuckler尝试只使用类名作为一个简单的schemaId,但是如果您在不同的名称空间中有两个类具有相同的名称(如您所做的),这将不起作用。
如错误所示,您可以使用配置设置"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/

xfb7svmp

xfb7svmp2#

我终于在swagger configs中找到了一种方法。转到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 =>
        // ....
    });
czfnxgou

czfnxgou3#

我使用的是Asp.netCore 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);
        });
wwwo4jvm

wwwo4jvm4#

如果注解掉或添加:

c.UseFullTypeNameInSchemaIds();

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

js81xvg6

js81xvg65#

对于Swashbuckle.AspNetCore5.2.1(在. NETCore3.1上),Swashbuckle配置API似乎缺少旧解决方案中描述的选项,相反,Startup.cs中的以下更改对我很有效:

services.AddSwaggerGen(c =>
  {
     // Existing configuration

     // Tweak to the Schema generator
     c.SchemaGeneratorOptions = new SchemaGeneratorOptions {SchemaIdSelector = type => type.FullName};
  }
f87krz0w

f87krz0w6#

如果您的模型包含泛型类型,请考虑使用Type.ToString()而不是Type.FullName,以摆脱为泛型参数类型生成的程序集信息,并使您的schema 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]
jum4pzuy

jum4pzuy7#

在我的例子中,我在2个微服务项目中有2个相同的类(A和B)。
当我打开项目(A)中的dependencies文件夹时,我发现我错误地将另一个项目(B)添加为对此项目的引用。
删除我的两个项目之间的依赖关系之后,冲突就解决了。

相关问题