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

Fixes dictionary binding error for numeric key/value #59911

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ebalders
Copy link

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

@ebalders ebalders requested a review from a team as a code owner January 17, 2025 02:52
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jan 17, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 17, 2025
Copy link
Contributor

Thanks for your PR, @ebalders. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@martincostello
Copy link
Member

Could you add a test for this scenario?

Copy link
Member

@lewing lewing left a 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)
Copy link
Author

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 },
Copy link
Author

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.

@ebalders
Copy link
Author

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.

public IActionResult Index(Dictionary<long, long> testValueThatWillNeverExist)

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.

@ebalders
Copy link
Author

ebalders commented Jan 22, 2025

@dotnet-policy-service agree company="Data Research Group"

1 similar comment
@ebalders
Copy link
Author

@dotnet-policy-service agree company="Data Research Group"

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 community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Model binding fails with FormatException if dictionary key is numeric type and value is missing from POST
3 participants