If you are following Jimmy Bogard’s style of MediatR commands and queries that he did within his Contoso University example you’ll find that Swagger does not play nice by default with that type of class nesting.

Snippet Example:

Thankfully there is a solution!

Lets say I have two commands. One called DeleteAllTheThings.cs and another called DeleteAllTheOtherThings.cs and both have a nested class called Command. By default Swagger will start to complain with an error about duplicate schema ids because it is only looking at the Command class of which there are now two. We need something a little more robust than that in how we generate a name. Thankfully when registering Swagger in the Startup.cs we can add an option to customize how Schema Ids get generated.

This bit of code is simply using the full name of the type instead and gluing the wrapping class type name with the nested Command or Query. The result is you get a nicely named class in your documentation too!


Dave van Herten

Adventures in Code