-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Fixes dictionary binding error for numeric key/value #59911
base: main
Are you sure you want to change the base?
Conversation
Thanks for your PR, @ebalders. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Could you add a test for this scenario? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the test results this appears to fail on cases that expected to work.
@@ -142,7 +142,7 @@ public override async Task BindModelAsync(ModelBindingContext bindingContext) | |||
// Attempt to bind dictionary from a set of prefix[key]=value entries. Get the short and long keys first. | |||
var prefix = bindingContext.ModelName; | |||
var keys = enumerableValueProvider.GetKeysFromPrefix(prefix); | |||
if (keys.Count == 0) | |||
if (string.IsNullOrEmpty(prefix) || keys.Count == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the lines below, this is a last ditch effort to find prefix[key] and return a validation error only if BindingRequired. As such, if prefix is null, it should follow the same AddErrorIfBindingRequired logic.
@@ -111,8 +112,8 @@ public static TheoryData<string, string, IDictionary<string, string>> StringToSt | |||
|
|||
return new TheoryData<string, string, IDictionary<string, string>> | |||
{ | |||
{ string.Empty, "[{0}]", dictionaryWithOne }, | |||
{ string.Empty, "[{0}]", dictionaryWithThree }, | |||
{ string.Empty, "[{0}]", dictionaryWithNone }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If prefix is empty, we should not be trying to bind all values into the dictionary.
Apologies for not including tests, it has taken me a while to get everything running. I see that since .NET9, a try/catch has been implemented. This alleviates the FormatException, but will still cause a validation error.
Given the scenario above, I expect no validation error when the form does not include "testValueThatWillNeverExist". I also expect it not to return a validation error message when the form contains non-numeric keys. I have updated the existing tests to reflect these outcomes. |
@dotnet-policy-service agree company="Data Research Group" |
1 similar comment
@dotnet-policy-service agree company="Data Research Group" |
Fix Dictionary with numeric binding errors
Binding a dictionary with a numeric key type on a request that doesn't include the values causes a FormatException. When the request doesn't contain the parameter name, it attempts to find prefixes based on the parameter name. Since value providers return all keys when the prefix(ModelName) is empty, it will attempt to convert every form key into the numeric type.
This avoids that when the prefix is null or empty.
Fixes #35594