I'm building an OData v.4 web service that must expose data retrieved from another 3rd party web source, so the data doesn't resemble anything from LINQ world, i.e.: no IQueryable, no Context, no whatever..
The way to go seems to be manually processing parameters from ODataQueryOptions and returning simple sequence of items. So, controller method should look something like this:
class MyMasterEntity {
[Contained]
public IEnumerable<MyDetailEntity> Details { get; set; }
}
// [EnableQuery]
public IEnumerable<MyMasterEntity> Get(ODataQueryOptions<MyMasterEntity> options)
{
// process .FilterQueryOption
// process .SelectExpandQueryOption
// process .SkipQueryOption
// process .TopQueryOption
return myMasterEntityList;
}
This works well except for $expand=Details
, in which case properties are not expanded in resulting response, although my logic adds them just fine.
If adding [EnableQuery]
attribute (which makes no sense to start with, as it's mutually exclusive with the whole idea of ODataQueryOptions), then expansion starts working. Or it rather pretends to be working, because what is really happening is that query is processed twice: first time by my code, then by OData machinery after i returned the data. This could be tolerable (anyway, i do expensive calls manually, so no big deal of OData retrying on already prepared data), if not for the fact that that second pass screws non-deterministic operations like $skip. (I.e.: it's okay to apply $top as many times as you like receiving the same result, but it's not okay to do this with $skip).
As i understood from reverse engineering relevant assemblies, standard expansion code wraps entities into something, which tells JSON formatter to emit corresponding properties, regardless if they're actually expanded inside entities.
Also tried:
- changing return type (IQueryable, IHttpActionResult)
- force calling
SelectExpandQueryOption.ApplyTo(myMasterEntityList, new ODataQuerySettings())
after manual expansion, but before returning
How do I properly expand navigation properties?