Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assignment of CollectionExpandedProperty.TotalCount when using EF #1693

Merged

Conversation

gunars-skangals
Copy link
Contributor

Issues

This pull request fixes issue #1565

Description

As the expression contained in NamedPropertyExpression.TotalCount can be either Int64 or Nullable depending on ODataQuerySettings.HandleNullPropagation, cast result of expression to the nullable type before assigning, otherwise the property is set to some random number.

Checklist (Uncheck if it is not completed)

  • Test cases added
  • Build and test with one-click build and test script passed

@msftclas
Copy link

msftclas commented Nov 28, 2018

CLA assistant check
All CLA requirements met. #Closed

@KanishManuja-MS KanishManuja-MS added the Ready for review Use this label if a pull request is ready to be reviewed label Nov 30, 2018
Copy link
Member

@xuzhg xuzhg left a comment

Choose a reason for hiding this comment

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

:shipit:

@xuzhg xuzhg requested a review from KanishManuja-MS March 6, 2019 21:53
@KanishManuja-MS KanishManuja-MS added this to the 7.2 milestone Apr 3, 2019
Assert.Contains("\"[email protected]\":9", payload); // Categories
}

private Task<HttpResponseMessage> GetResponse(string uri, string acceptHeader, bool handleNullPropagation = true)
Copy link
Member

Choose a reason for hiding this comment

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

bool handleNullPropagation = true [](start = 87, length = 33)

Please add variations to the test for handle null propagation equal to both true and false (i.e., turn this method into [Theory] with [InlineData] for handleNullPropagation = true and handleNullPropagation=false). Better yet, make all of the tests that assume handleNullPropagation=false into [Theory] that run with handleNullPropagation equal to both true and false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the redundant argument - HandleNullPropagationOption is set on controller action level anyway

do not quite understand the point: SelectExpand_WithOneLevelNestedDollarCount_HandleNullPropagationFalse_Works is already a variation on test SelectExpand_WithOneLevelNestedDollarCount_Works which tests the same thing (count field for a nested collection) but with the default HandleNullPropagationOption value of True, which depends on the namespace of query provider (class HandleNullPropagationOptionHelper, method GetDefaultHandleNullPropagationOption)

Copy link
Member

@mikepizzo mikepizzo Jul 8, 2019

Choose a reason for hiding this comment

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

Right. My preference would be to have unit test that explicitly tests both the null propagation true case as well as the null propagation false case. I realize that the existing test on ln 38 doesn't specify null propagation, which picks up the default, which for the current query provider happens to be null propagation true, but that's not explicit in the test so a future change to that test, such as using a different query provider, could inadvertently invalidate the intention that we have coverage for both null propagation false and null propagation true.

It's not a matter of making sure that this fix works and doesn't break existing tests (which the PR currently validates), but making sure we continue to have coverage and don't inadvertantly invalidate assumptions as the tests and code evolve.

One way to address this would be to specifically set HandleNullPropagation to OData.Query.HandleNullPropagationOption.True in the MsCustomersController, so that we are explicit that we are testing a handle null propagation value of true, rather than relying on a query provider-specific default value.


In reply to: 300698007 [](ancestors = 300698007)

@@ -118,7 +118,7 @@ private static Expression CreateNamedPropertyCreationExpression(NamedPropertyExp

if (property.CountOption != null && property.CountOption.Value)
{
memberBindings.Add(Expression.Bind(namedPropertyType.GetProperty("TotalCount"), property.TotalCount));
memberBindings.Add(Expression.Bind(namedPropertyType.GetProperty("TotalCount"), ExpressionHelpers.ToNullable(property.TotalCount)));
Copy link
Member

@mikepizzo mikepizzo Jun 28, 2019

Choose a reason for hiding this comment

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

ExpressionHelpers.ToNullable [](start = 100, length = 28)

Should this be dependent upon whether or not null propagation handling is true or false? i.e., should we only call ExpressionHelpers.ToNullable if null propagation handling is true? #Closed

Copy link
Contributor Author

@gunars-skangals gunars-skangals Jul 5, 2019

Choose a reason for hiding this comment

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

it has been a while and had almost forgotten about this

from what i can recall, the property that is being set is of type long? (class CollectionExpandedProperty? ), no matter what the settings are, and for assignment to work correctly, the assigned expression should be converted to nullable anyway

i don't remember if the expression type was correct in HandleNullPropagationOption.True case, but ExpressionHelpers.ToNullable does not do anything if the expression is of nullable type already
i don't see a straightforward way of getting that setting value inside of the PropertyContainer class anyway so imho this should stay as is #Resolved

@mikepizzo
Copy link
Member

mikepizzo commented Jun 28, 2019

@gunars-skangals - thanks for the pull request to fix this issue!

See my comments inline. Should we only call ExpressionHelpers.ToNullable if null propagation handling is true?

If you can follow up, I'd love to get this fix integrated into our next WebAPI OData build.

Thanks! #Closed

Copy link
Member

@mikepizzo mikepizzo left a comment

Choose a reason for hiding this comment

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

:shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready for review Use this label if a pull request is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants