Webapi: Selection on an expanded property not working

Created on 12 Jan 2015  路  5Comments  路  Source: OData/WebApi

I am trying to select a child object from the expanded property, but I am
getting an exception Microsoft.OData.Core.ODataException.

For ex, I am using the below url to get list of all postal codes in the
address object.

http://localhost/MyService/customer?$select=Address/PostalCode&$expand=Addre
ss($select=PostalCode)

See the below exception details.
{
message: "The query specified in the URI is not valid. Found a path with
multiple navigation properties or a bad complex property path in a select
clause. Please reword your query such that each level of select or expand only
contains either TypeSegments or Properties."
exceptionMessage: "Found a path with multiple navigation properties or a bad
complex property path in a select clause. Please reword your query such that
each level of select or expand only contains either TypeSegments or
Properties."
exceptionType: "Microsoft.OData.Core.ODataException"
stackTrace: " at Microsoft.OData.Core.UriParser.Visitors.SelectPropertyVisitor
.ProcessTokenAsPath(NonSystemToken tokenIn) at Microsoft.OData.Core.UriParser.
Visitors.SelectPropertyVisitor.Visit(NonSystemToken tokenIn) at Microsoft.ODat
a.Core.UriParser.Syntactic.NonSystemToken.Accept(IPathSegmentTokenVisitor
visitor) at
Microsoft.OData.Core.UriParser.Parsers.SelectBinder.Bind(SelectToken tokenIn)
at Microsoft.OData.Core.UriParser.Parsers.SelectExpandBinder.Bind(ExpandToken
tokenIn) at Microsoft.OData.Core.UriParser.Parsers.SelectExpandSemanticBinder.
Bind(IEdmStructuredType elementType, IEdmNavigationSource navigationSource,
ExpandToken expandToken, SelectToken selectToken, ODataUriParserConfiguration
configuration) at Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseS
electAndExpandImplementation(String select, String expand,
ODataUriParserConfiguration configuration, IEdmStructuredType elementType,
IEdmNavigationSource navigationSource) at
Microsoft.OData.Core.UriParser.ODataQueryOptionParser.ParseSelectAndExpand()
at System.Web.OData.Query.Validators.SelectExpandQueryValidator.Validate(Selec
tExpandQueryOption selectExpandQueryOption, ODataValidationSettings
validationSettings) at System.Web.OData.Query.SelectExpandQueryOption.Validate
(ODataValidationSettings validationSettings) at System.Web.OData.Query.Validat
ors.ODataQueryValidator.Validate(ODataQueryOptions options,
ODataValidationSettings validationSettings) at
System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings
validationSettings) at
System.Web.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage
request, ODataQueryOptions queryOptions) at
System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response,
HttpRequestMessage request, HttpActionDescriptor actionDescriptor) at System.W
eb.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext
actionExecutedContext)"
}

Work Item Details

Original CodePlex Issue: Issue 2161
Status: Proposed
Reason Closed: Unassigned
Assigned to: Unassigned
Reported on: Oct 20, 2014 at 10:36 PM
Reported by: suhman
Updated on: Oct 22, 2014 at 10:38 PM
Updated by: danroth27

Most helpful comment

Hello, the issue is that you have to use the $select inside the expand, try:

GET http://localhost/odata/Categories?$expand=Products($select=Name)&$select=Name,Products

All 5 comments

Follow up to investigate

The request url contain some issues:

http://localhost/MyService/customer?$select=Address/PostalCode&$expand=Address($select=PostalCode)

If the "Address" is a complex property. It cannot be used in $expand as above.
If the "Address" is a navigation property. It cannot be used in $select as above

Attached the OData Protocol

Is this a change since Web API 2 OData? To quote an article on Web API 2 OData on www.asp.net:

You can also select the properties within an expanded property. The following request expands Products and selects category name plus product name.

GET http://localhost/odata/Categories?$expand=Products&$select=Name,Products/Name

Here is the response body:

{ "odata.metadata":"http://localhost/odata/$metadata#Categories&$select=Name,Products/Name", "value":[ { "Products":[ {"Name":"Hat"},{"Name":"Scarf"},{"Name":"Socks"} ], "Name":"Apparel" }, { "Products":[ {"Name":"Yo-yo"},{"Name":"Puzzle"} ], "Name":"Toys" } ] }

Is there a reason this functionality has been removed? Like the OP, I'd very much like to have it.

If not possible, how else would you go about filtering properties from an $expand-ed query? Are you simply forced to include the whole entity if you use $expand?

Hello, the issue is that you have to use the $select inside the expand, try:

GET http://localhost/odata/Categories?$expand=Products($select=Name)&$select=Name,Products

@alexschrod like jlsfernandez said, use select in expand if the "Products" is a navigation property is working.

Was this page helpful?
0 / 5 - 0 ratings