Swashbuckle.aspnetcore: System.Type is not supported by SwaggerGen

Created on 4 Jul 2019  路  6Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

This applies for the version 5.0.0-rc2, seems to be fixed in master branch.

I tried to integrate Swashbuckle.AspNetCore accordingly to Getting started with swashbuckle.

Inside my application I have a model that has a property of type System.Type.
Unfortunately, the Swashbuckle threw a System.Collections.Generic.KeyNotFoundException:

   An unhandled exception has occurred while executing the request.
System.Collections.Generic.KeyNotFoundException: The given key 'System.Type' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Swashbuckle.AspNetCore.SwaggerGen.PrimitiveSchemaGenerator.GenerateSchemaFor(Type type, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.ChainableSchemaGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository)
// [...] ommited for simplicity
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

After my investigation, I found out that Json.NET creates a JsonStringContract for a property of type System.Type.

The JsonStringContract is inheriting from the JsonPrimitiveContract and this let's the PrimitiveSchemaGenerator handle System.Type as well and throws because the dictionary doesn't contain this type as key.

Most helpful comment

I believe this is fixed with the latest code (both Type and PathString both default to a string schema). There's no official Nuget yet (still working through some bugs before official release) but if you download the latest preview from my.get.org things should be working.

All 6 comments

The same error . Anyone can help?

I fixed it, I can do a PR for it.

I'm coming across this problem too and can confirm that it is fixed in the master! Can we have another release candidate @domaindrivendev?

I have same issue with Microsoft.AspNetCore.Http.PathString.

I believe this is fixed with the latest code (both Type and PathString both default to a string schema). There's no official Nuget yet (still working through some bugs before official release) but if you download the latest preview from my.get.org things should be working.

I have similar issue.

KeyNotFoundException: The given key 'System.Version' was not present in the dictionary.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alasvant picture alasvant  路  3Comments

jderus picture jderus  路  4Comments

rgelb picture rgelb  路  3Comments

mrmartan picture mrmartan  路  3Comments

TimmyGilissen picture TimmyGilissen  路  3Comments