Fixes dictionary binding error for numeric key/value#59911
Fixes dictionary binding error for numeric key/value#59911ebalders wants to merge 2 commits intodotnet:mainfrom
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? |
lewing
left a comment
There was a problem hiding this comment.
Looking at the test results this appears to fail on cases that expected to work.
| 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.
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.
| { | ||
| { string.Empty, "[{0}]", dictionaryWithOne }, | ||
| { string.Empty, "[{0}]", dictionaryWithThree }, | ||
| { string.Empty, "[{0}]", dictionaryWithNone }, |
There was a problem hiding this comment.
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" |
| { string.Empty, "[{0}]", dictionaryWithOne }, | ||
| { string.Empty, "[{0}]", dictionaryWithThree }, | ||
| { string.Empty, "[{0}]", dictionaryWithNone }, | ||
| { string.Empty, "[{0}]", dictionaryWithNone }, |
There was a problem hiding this comment.
| { string.Empty, "[{0}]", dictionaryWithNone }, | |
| { string.Empty, "[{0}]", dictionaryWithOne }, |
I think this a typo? We want to retain the test for dictionaryWithOne.
There was a problem hiding this comment.
And dictionaryWithThree! I like keeping the additional test cases for empty modelName though.
Also, considering the issue focused on numeric keys, shouldn't we have at least one regression test with a numeric key?
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
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