-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Inferring FromServices optionality #39804
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
Changes from 1 commit
8c701ab
4a66bcc
5f0c160
d1c4680
3e298f3
7231185
3eadb0e
70b369f
14b53d7
69a9fd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
#nullable enable | ||
|
||
using System.Reflection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders; | ||
|
||
|
@@ -11,9 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders; | |
/// </summary> | ||
public class ServicesModelBinderProvider : IModelBinderProvider | ||
{ | ||
// ServicesModelBinder does not have any state. Re-use the same instance for binding. | ||
|
||
private readonly ServicesModelBinder _modelBinder = new ServicesModelBinder(); | ||
brunolins16 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private readonly NullabilityInfoContext _nullabilityContext = new(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 cc @pranavkm There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, let me do a deep look at the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think The only scenario that will cause a big change to the current behavior is when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eg.: #nullable disable
public IActionResult Action([FromServices] IPersonService param1);
#nullable restore There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
/// <inheritdoc /> | ||
public IModelBinder? GetBinder(ModelBinderProviderContext context) | ||
|
@@ -26,9 +25,16 @@ public class ServicesModelBinderProvider : IModelBinderProvider | |
if (context.BindingInfo.BindingSource != null && | ||
context.BindingInfo.BindingSource.CanAcceptDataFrom(BindingSource.Services)) | ||
{ | ||
return _modelBinder; | ||
return new ServicesModelBinder() | ||
{ | ||
IsOptionalParameter = IsOptionalParameter(context.Metadata.Identity.ParameterInfo!) | ||
brunolins16 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
internal bool IsOptionalParameter(ParameterInfo parameterInfo) => | ||
parameterInfo.HasDefaultValue || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use ParameterDefaultValue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't get what you mean with that. |
||
_nullabilityContext.Create(parameterInfo).ReadState == NullabilityState.Nullable; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.