AutoMapper / AutoMapper.Extensions.OData

Creates LINQ expressions from ODataQueryOptions and executes the query.
MIT License
140 stars 38 forks source link

Expanded navigation properties MUST be returned, even if they are not specified in $select. #100

Closed AbdullahUlber closed 3 years ago

AbdullahUlber commented 3 years ago

The current implementation requires expansions to be included in the selects, unless selects are missing. The OData specification however says that "Expanded navigation properties MUST be returned, even if they are not specified in $select." (see http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#_Toc31361040).

The method below is in the LinqExtensions class.

        private static List<List<ODataExpansionOptions>> GetExpansions(this IEnumerable<SelectItem> selectedItems, HashSet<string> selects, Type parentType)
        {
            if (selectedItems == null)
                return new List<List<ODataExpansionOptions>>();

            return selectedItems.OfType<ExpandedNavigationSelectItem>().Aggregate(new List<List<ODataExpansionOptions>>(), (listOfExpansionLists, next) =>
            {
                string path = next.PathToNavigationProperty.FirstSegment.Identifier;//Only first segment is necessary because of the new syntax $expand=Builder($expand=City) vs $expand=Builder/City

                if (!selects.ExpansionIsValid(path))/*If selects are defined then check to make sure the expansion is one of them.*/
                    return listOfExpansionLists;
BlaiseD commented 3 years ago

I think this works if you configure AutoMapper to NOT require explicit expansions. If not a PR is welcome.