Skip to content

Fixes dictionary binding error for numeric key/value#59911

Closed
ebalders wants to merge 2 commits intodotnet:mainfrom
ebalders:main
Closed

Fixes dictionary binding error for numeric key/value#59911
ebalders wants to merge 2 commits intodotnet:mainfrom
ebalders:main

Conversation

@ebalders
Copy link
Copy Markdown

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
@ghost ghost 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
@dotnet-policy-service
Copy link
Copy Markdown
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
Copy Markdown
Member

Could you add a test for this scenario?

Copy link
Copy Markdown
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.

var prefix = bindingContext.ModelName;
var keys = enumerableValueProvider.GetKeysFromPrefix(prefix);
if (keys.Count == 0)
if (string.IsNullOrEmpty(prefix) || keys.Count == 0)
Copy link
Copy Markdown
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.

{
{ string.Empty, "[{0}]", dictionaryWithOne },
{ string.Empty, "[{0}]", dictionaryWithThree },
{ string.Empty, "[{0}]", dictionaryWithNone },
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Author

ebalders commented Jan 22, 2025

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

1 similar comment
@ebalders
Copy link
Copy Markdown
Author

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

{ string.Empty, "[{0}]", dictionaryWithOne },
{ string.Empty, "[{0}]", dictionaryWithThree },
{ string.Empty, "[{0}]", dictionaryWithNone },
{ string.Empty, "[{0}]", dictionaryWithNone },
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
{ string.Empty, "[{0}]", dictionaryWithNone },
{ string.Empty, "[{0}]", dictionaryWithOne },

I think this a typo? We want to retain the test for dictionaryWithOne.

Copy link
Copy Markdown
Member

@halter73 halter73 Jan 27, 2025

Choose a reason for hiding this comment

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

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?

@captainsafia captainsafia requested a review from Copilot January 27, 2025 19:51
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 4, 2025
@danmoseley danmoseley requested review from Copilot and removed request for Copilot February 14, 2025 03:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

@captainsafia captainsafia added pr: pending author input For automation. Specifically separate from Needs: Author Feedback and removed pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun labels Mar 10, 2025
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Mar 18, 2025
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Hi @ebalders.
It seems you haven't touched this PR for the last two weeks. To avoid accumulating old PRs, we're marking it as stale. As a result, it will be closed if no further activity occurs within 4 days of this comment. You can learn more about our Issue Management Policies here.

@dotnet-policy-service dotnet-policy-service Bot added the stale Indicates a stale issue. These issues will be closed automatically soon. label Mar 28, 2025
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 pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun pr: pending author input For automation. Specifically separate from Needs: Author Feedback stale Indicates a stale issue. These issues will be closed automatically soon.

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

6 participants