swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B
Every class in the swagger JSON must have a unique schemaId.
Swashbuckler tries to just use the class name as a simple schemaId, however if you have two classes in different namespaces with the same name (as you do) this will not work.
As the error suggests, you can use the config setting "UseFullTypeNameInSchemaIds*" for a potential workaround (Update: not available in recent versions)
In newer versions you can achieve the same behavior via options.CustomSchemaIds(x => x.FullName).
Here is an example:
services.ConfigureSwaggerGen(options =>
{
//your custom configuration goes here
...
// UseFullTypeNameInSchemaIds replacement for .NET Core
options.CustomSchemaIds(x => x.FullName);
});
for more information http://wegotcode.com/microsoft/swagger-fix-for-dotnetcore/
I finally found a way in swagger configs. Go to App_Start\SwaggerConfig.cs
file and under EnableSwagger
lambda expression add this line:
c.SchemaId(x => x.FullName);
Full code is like this:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
// your configs...
c.SchemaId(x => x.FullName);
// other configs...
})
.EnableSwaggerUi(c =>
// ....
});
I am using Asp.net Core 2.1. This error resulted when I tried to show Swagger UI.
I solved the problem this way:
In Starup.cs
, in ConfigureServices()
add c.CustomSchemaIds(i => i.FullName);
see example below:
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);
});