Swagger fix for dotnetcore

I’m currently working on a Point of Sale type API utilizing dotnetcore.  We use Swagger / Swashbuckle to handle our documentation.  It’s a great project and I really appreciate the work that’s been put into it.  As we all know, developers hate to write documentation and Swagger goes a long way towards the holy grail of maintained documentation.

In our system, there are various endpoints involved and some of them use similar contracts but they are different enough to where Automapping, etc. is not really viable.  So, I started breaking the models into separate namespaces, cleaned things up, hit F5 and boom…the swagger page (and associated schema details) broke with a message like:

Conflicting schemaIds: Identical schemaIds detected for types ABC.IContract and DEF.IContract. See config settings –  “UseFullTypeNameInSchemaIds”

So essentially the workaround is to fully qualify the classes / interfaces that you are using.  The problem is that UseFullTypeNameInSchemaIds isn’t available in the dotnetcore version that I snagged from nuget.  However, I noticed another method, CustomSchemaIds, which is far more useful.  You can specify a differentiation of your own with that method.  In my case, I’m just using the fully qualified name for the interface.

Example:

var docs = Configuration["Swagger:Path"];
services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "An awesome API",
Description = "Dotnet Core Rocks"
});
options.IncludeXmlComments(docs);
options.DescribeAllEnumsAsStrings();
options.CustomSchemaIds(x => x.FullName);
});
I hope this helps someone.  Thanks for stopping by!

Comments 4

  1. Greate Post… thanks a lot.
    In my case, just “options.SchemaIds(x => x.FullName);” did the trick…

    Many Thanks…

Leave a Reply

Your email address will not be published. Required fields are marked *