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

Inferring FromServices optionality #39804

Merged
merged 10 commits into from
Feb 2, 2022

Conversation

brunolins16
Copy link
Member

Fixes #39757

@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jan 27, 2022
// ServicesModelBinder does not have any state. Re-use the same instance for binding.

private readonly ServicesModelBinder _modelBinder = new ServicesModelBinder();
private readonly NullabilityInfoContext _nullabilityContext = new();
Copy link
Member

Choose a reason for hiding this comment

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

We have some issues with thread safety and unit tests when we implemented this in minimal APIs. This isn't thread safe so I wonder if it should be part of the ModelMetadata instead.

cc @pranavkm

Copy link
Member Author

Choose a reason for hiding this comment

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

@davidfowl good to know. My initial design was move to modelmetada, since the same will be need around and that is the reason I did not published the PR.

The problem is, today there is a IsRequired property there already that does not take exactly the nullabiliy context in account. So, I am not sure what to do about that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, let me do a deep look at the IsRequired because I feel it does what we need already.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think IsRequired works here and also take in account the [Required] attribute.

The only scenario that will cause a big change to the current behavior is when Reference type parameters without a default value in an oblivious nullability context, because today they will throw an exception and after the change they will, based on the IsRequired logic, very similar to the RequestDelegateFactory, bind to null. I was trying to avoid this behavioral change, but any thoughts about that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Eg.:

#nullable disable
        public IActionResult Action([FromServices] IPersonService param1);
#nullable restore

Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't change the semantics of IsRequired if the context is oblivious. Is that something we can infer from the context?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean infer for the Service as required to keep the same behavior for the scenario? If so, let me double check but maybe is possible to infer based on something like this:

!ModelType.IsValueType && nullabilityInfo.ReadState == NullabilityState.Unknown

Copy link
Member Author

Choose a reason for hiding this comment

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

@pranavkm I have updated the PR with something I don't like much but that covers the scenario without change the IsRequired.

Copy link
Contributor

@pranavkm pranavkm left a comment

Choose a reason for hiding this comment

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

This would need a few integration tests

}

return null;
}

internal bool IsOptionalParameter(ParameterInfo parameterInfo) =>
parameterInfo.HasDefaultValue ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use ParameterDefaultValue?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't get what you mean with that.

@brunolins16 brunolins16 marked this pull request as ready for review January 28, 2022 22:24
@brunolins16 brunolins16 requested a review from javiercn as a code owner January 28, 2022 22:24
brunolins16 and others added 6 commits January 28, 2022 14:48
Co-authored-by: Pranav K <prkrishn@hotmail.com>
@brunolins16 brunolins16 merged commit b89eba6 into dotnet:main Feb 2, 2022
@ghost ghost added this to the 7.0-preview2 milestone Feb 2, 2022
@brunolins16 brunolins16 deleted the brunolins16/issues/39757 branch February 2, 2022 00:11
@brunolins16 brunolins16 linked an issue Apr 19, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inferring FromServices optionality based on nullability information FromServicesAttribute not optional
3 participants