Skip to content

Issue with OData Singleton entity controller actions #610

Closed
@Graph2133

Description

@Graph2133

Hello, could you please help me. I'm trying to configure endpoint in OData controller which is configured as Singleton in my edm model. I'm reusing this sample https://github.com/microsoft/aspnet-api-versioning/tree/master/samples/aspnetcore/SwaggerODataSample (release 4.0 because it's crucial for real project which I'm working on).

    public class PizzaConfiguration : IModelConfiguration
    {
        public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
        {
            builder.Singleton<Pizza>("Pizza");
            builder.EntityType<Pizza>().Select().Expand();
        }
    }

My controller has nothing extraordinary but just one simple action

    [ApiVersionNeutral]
    public class PizzaController : ODataController
    {
        [HttpGet]
        [Produces("application/json")]
        [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Select | 
             AllowedQueryOptions.Expand)]
        [ODataRoute("Pizza")]
        public SingleResult<Pizza> GetPizza(ODataQueryOptions<Pizza> options) 
            => SingleResult.Create(new[] { new Pizza { Name = "Test" } }.AsQueryable());
    }

But swashbuckle can not load api definitions and I'm getting this error:

System.Collections.Generic.KeyNotFoundException: The given key 'System.Reflection.TypeInfo' 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)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.ArraySchemaGenerator.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.WhereSelectListIterator`2.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
   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.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

If I remove ODataQueryOptions all works but I need them for proper generation of expansion properties because I want load only needed data to my singleton result using query options (checking whether needed expansion property was requested).
This works with route "api/pizza" and swashbuckle generate proper schema:

    [ApiVersionNeutral]
    public class PizzaController : ODataController
    {
        [HttpGet]
        [Produces("application/json")]
        [EnableQuery(AllowedQueryOptions = AspNet.OData.Query.AllowedQueryOptions.Select | 
             AllowedQueryOptions.Expand)]
        [ODataRoute("Pizza")]
        public SingleResult<Pizza> GetPizza() 
            => SingleResult.Create(new[] { new Pizza { Name = "Test" } }.AsQueryable());
    }

And I've also question regarding generation of routes. Let's say iIve the same configuration I mentioned above but my controller will look like this (I will omit setting of ODataRoute and will use default routing convention)

    [ApiVersionNeutral]
    public class PizzaController : ODataController
    {
        [HttpGet]
        [Produces("application/json")]
        [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Select | 
            AllowedQueryOptions.Expand)]
        public SingleResult<Pizza> Get() 
            => SingleResult.Create(new[] { new Pizza { Name = "Test" } }.AsQueryable());
    }

Is it expected behavior that swagger action looks like this:

image

Shouldn't it be also api/pizza ?

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions