AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

The given model does not contain the type 'System.Object' when using $select with untyped properties

Open jusbuc2k opened this issue 8 months ago • 4 comments

Assemblies affected Microsoft.AspNetCore.OData 8.25 and previous versions

Describe the bug An exception is thrown when using $select on an untyped property after following this guide to setup untyped properties:

https://devblogs.microsoft.com/odata/enable-un-typed-within-asp-net-core-odata/#:~:text=In%20ASP.NET%20Core%20OData%2C%20it%E2%80%99s%20easy%20to%20build,a%20single%20value%20untyped%20property%2C%20for%20example%20Data.

Reproduce steps

  1. Clone the sample repo referenced here https://github.com/xuzhg/MyAspNetCore/tree/master/src/UntypedApp
  2. Open the UntypedApp.sln solution project
  3. Add [EnableQuery] to line 18 on the HandlePeopleController
  4. Run the project
  5. Perform a GET request using to the path /odata/people?$select=Id,Name,Data

Data Model See the example repo at https://github.com/xuzhg/MyAspNetCore/tree/master/src/UntypedApp

EDM (CSDL) Model See the example repo at https://github.com/xuzhg/MyAspNetCore/tree/master/src/UntypedApp

Request/Response

GET http://localhost:5299/odata/people?$select=Id,Name,Data

{
  "error": {
    "code": "",
    "message": "The query specified in the URI is not valid. The given model does not contain the type 'System.Object'.",
    "details": [],
    "innererror": {
      "message": "The given model does not contain the type 'System.Object'.",
      "type": "Microsoft.OData.ODataException",
      "stacktrace": "   at Microsoft.AspNetCore.OData.Query.Expressions.QueryBinderContext..ctor(QueryBinderContext context, ODataQuerySettings querySettings, Type clrType)\r\n   at Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.ProjectAsWrapper(QueryBinderContext context, Expression source, SelectExpandClause selectExpandClause, IEdmStructuredType structuredType, IEdmNavigationSource navigationSource, OrderByClause orderByClause, ComputeClause computeClause, Nullable`1 topOption, Nullable`1 skipOption, Nullable`1 modelBoundPageSize)\r\n   at Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.BuildSelectedProperty(QueryBinderContext context, Expression source, IEdmStructuredType structuredType, IEdmStructuralProperty structuralProperty, PathSelectItem pathSelectItem, IList`1 includedProperties)\r\n   at Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.BuildPropertyContainer(QueryBinderContext context, Expression source, IEdmStructuredType structuredType, IDictionary`2 propertiesToExpand, IDictionary`2 propertiesToInclude, IList`1 computedProperties, ISet`1 autoSelectedProperties, Boolean isSelectingOpenTypeSegments, Boolean isSelectedAll)\r\n   at Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.ProjectElement(QueryBinderContext context, Expression source, SelectExpandClause selectExpandClause, IEdmStructuredType structuredType, IEdmNavigationSource navigationSource)\r\n   at Microsoft.AspNetCore.OData.Query.Expressions.SelectExpandBinder.BindSelectExpand(SelectExpandClause selectExpandClause, QueryBinderContext context)\r\n   at Microsoft.AspNetCore.OData.Query.Expressions.BinderExtensions.ApplyBind(ISelectExpandBinder binder, IQueryable source, SelectExpandClause selectExpandClause, QueryBinderContext context)\r\n   at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.ApplySelectExpand[T](T entity, ODataQuerySettings querySettings)\r\n   at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request)\r\n   at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuted(ActionExecutedContext actionExecutedContext, Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request)"
    }
  }
}

Expected behavior I expect to get the Id, Name, and Data properties with their values just as if I had omitted the $select parmeter.

Screenshots

Working image

Not Working image

Additional context

jusbuc2k avatar May 30 '24 14:05 jusbuc2k