OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
855 stars 473 forks source link

Make sure 'top' doesn't apply on a null collection #2842

Closed ificator closed 4 months ago

ificator commented 5 months ago

Issues

$top is attempted on null collections, resulting in failures like this:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --> 
System.ArgumentNullException: Value cannot be null. 
Parameter name: source
    at System.Linq.OrderedEnumerable`2..ctor(Enumerable`1 source, Func` keySelector, IComparer`1comparer, Boolean decending)
    at lambda_method(Closure_Site) 
    --End of inner exception stack trace--
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 
    at System.Reflection.RuntimeMethodInfo. UnsafeInvokeInternal(Object obj, Object [] parameters, Object arguments) 
    at System.Delegatre.DynamicInvokeImpl
    at Microsot.AspNet.OData.Query.Expressions.SelectExpandBinder.Bind
    at Microsot.AspNet.OData.Query.ODataQueryOptions.ApplySelectExpand

Description

ProjectCollection does have logic for handling null propagation, but it's currently using the modified source (e.g. the one that wraps the original in OrderBy, Take, Skip). As a result, when the source is null the check doesn't apply prior to the execution of those operations, which all require a non-null collection.

The fix is to simply perform the null check on the original source.

NOTE: The same issue exists in https://github.com/OData/AspNetCoreOData but I'll leave it to maintainers to port the fix.

Checklist (Uncheck if it is not completed)

habbes commented 5 months ago

Comment to trigger pipeline.