Skip to content

AutoExpand does not expand beyond non-default MaxExpansionDepth - #841

Merged
gathogojr merged 1 commit into
OData:mainfrom
gathogojr:fix/autoexpand-does-not-expand-beyond-non-default-max-expansion-depth
Feb 28, 2023
Merged

AutoExpand does not expand beyond non-default MaxExpansionDepth#841
gathogojr merged 1 commit into
OData:mainfrom
gathogojr:fix/autoexpand-does-not-expand-beyond-non-default-max-expansion-depth

Conversation

@gathogojr

@gathogojr gathogojr commented Feb 16, 2023

Copy link
Copy Markdown
Contributor

Fixes OData/WebApi#2748

We set the default MaxExpansionDepth when the Validate method of the ODataQueryOptions object is called. The problem currently is that when the MaxExpansionDepth is overridden, for instance by assigning the value from the EnableQuery attribute ([EnableQuery(MaxExpansionDepth = 4)]), we call the Validate method before the AutoExpand navigation properties have been added to the SelectExpandQueryOption object. For that reason, the specified MaxExpansionDepth ends up not being respected for the auto-expanded navigation properties.

This pull request fixes that by calling ODataQueryOptions.AddAutoSelectExpandProperties prior to validating the SelectExpand query option.

xuzhg
xuzhg previously approved these changes Feb 16, 2023
habbes
habbes previously approved these changes Feb 20, 2023
@gathogojr
gathogojr dismissed stale reviews from habbes and xuzhg via 2555b3a February 24, 2023 08:55
@gathogojr
gathogojr force-pushed the fix/autoexpand-does-not-expand-beyond-non-default-max-expansion-depth branch from 250c253 to 2555b3a Compare February 24, 2023 08:55
@gathogojr
gathogojr force-pushed the fix/autoexpand-does-not-expand-beyond-non-default-max-expansion-depth branch from 2555b3a to ff68ccf Compare February 24, 2023 08:57
{
SelectExpand.LevelsMaxLiteralExpansionDepth = originalSelectExpand.LevelsMaxLiteralExpansionDepth;
}
else if (Context.ValidationSettings != null && Context.ValidationSettings.MaxExpansionDepth > 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, 0 is not a valid max expansion depth?

@gathogojr gathogojr Feb 27, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@habbes What my investigation revealed is that if you set a MaxExpansionDepth of 0, that doesn't get applied - it defaults to configured MaxExpansionDepth of 2. The following won't prohibit expansion currently. That's the reason I used Context.ValidationSettings.MaxExpansionDepth > 0 in this PR to keep it consistent with the current behavior even as I follow up confirm if the current behavior is by design.
The following won't currently have the effect of preventing expansion:

[EnableQuery(MaxExpansionDepth = 0)]
public ActionResult<IEnumerable<Customer>> Get()
{
    return customers;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AutoExpand only goes 2 levels deep irrespective of MaxExpansionDepth

3 participants