Swashbuckle.aspnetcore: SwaggerGen 5.0.0-* doesn't use TypeConverters

Created on 26 Apr 2019  路  2Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Steps to reproduce:

  • create asp.net core 2.2 app
  • create
public class EventQuery
{
    public EventQuery(long? eventId, string tag)
    {
        EventId = eventId;
        Tag = tag;
    }

    public long? EventId { get; }

    public string Tag { get; }
}

public class EventQueryConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        return base.ConvertFrom(context, culture, value);
    }
}

// in EventsController.cs
[HttpGet("{query}")]
[Produces("application/protobuf")]
public string Get([FromQuery] EventQuery query)
{
    ...
}

// in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    TypeDescriptor.AddAttributes(typeof(EventQuery), new TypeConverterAttribute(typeof(EventQueryConverter)));
}
  • Navigate to swagger home page. An error occures during swagger generation.

System.Collections.Generic.KeyNotFoundException: The given key 'WebApplication3.EventQuery' was not present in the dictionary.
at System.Collections.Generic.Dictionary2.get_Item(TKey key) at Swashbuckle.AspNetCore.SwaggerGen.PrimitiveSchemaGenerator.GenerateSchemaFor(Type type, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.ChainableSchemaGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateParameter(ApiDescription apiDescription, ApiParameterDescription apiParameter, SchemaRepository schemaRepository) at System.Linq.Enumerable.WhereSelectListIterator2.ToList()
at System.Linq.Enumerable.ToListTSource
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
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.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Affected Versions: 5.0.0-*

The same code works with version 4.0.1.

Most helpful comment

@domaindrivendev I can see the v5-rc2 was released earlier, than the commit you've posted (22 March vs 17 May). Could you release a new rc version with the latest fixes?

All 2 comments

See c1fb708e3fe3f2c62abf4a85b91786f38b66582e. This is what have happened for this scenario in older versions.

@domaindrivendev I can see the v5-rc2 was released earlier, than the commit you've posted (22 March vs 17 May). Could you release a new rc version with the latest fixes?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rgelb picture rgelb  路  3Comments

nezoic picture nezoic  路  3Comments

TimmyGilissen picture TimmyGilissen  路  3Comments

NinoFloris picture NinoFloris  路  3Comments

vanillajonathan picture vanillajonathan  路  3Comments