Fix/asparameters fromform collection interfaces#64753
Open
wahab-cide wants to merge 2 commits intodotnet:mainfrom
Open
Fix/asparameters fromform collection interfaces#64753wahab-cide wants to merge 2 commits intodotnet:mainfrom
wahab-cide wants to merge 2 commits intodotnet:mainfrom
Conversation
Fixes dotnet#62329 This change extends simple binding to support collection interface types (IEnumerable<T>, IList<T>, ICollection<T>, List<T>) when used as properties in [AsParameters] classes with [FromForm]. Previously, these types caused InvalidOperationException because the framework attempted to use complex form binding (FormDataMapper) which cannot instantiate interface types. The fix: - Adds IsBindableCollectionInterface() helper to detect collection interfaces - Routes these types through simple binding (like arrays) when used as AsParameters properties - Creates arrays internally and converts them to the target collection type - Preserves complex binding for direct [FromForm] parameters to maintain backward compatibility
Adds comprehensive tests for IEnumerable<T>, IList<T>, ICollection<T>, List<T>, and int[] to verify the fix for dotnet#62329 and ensure no regressions.
Contributor
|
Thanks for your PR, @@wahab-cide. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Open
1 task
Contributor
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix collection interface binding with AsParameters and FromForm
Summary
Fix collection interface binding with AsParameters and FromForm
Description
This change extends simple binding to support collection interface types (
IEnumerable<T>,IList<T>,ICollection<T>,List<T>) when used as properties in[AsParameters]classes with[FromForm].Problem
Previously, these types caused
InvalidOperationExceptionbecause the framework attempted to use complex form binding (FormDataMapper) which cannot instantiate interface types. This issue only affected properties in[AsParameters]classes, not direct parameters.Example that failed before this fix:
This PR
This PR follows the same pattern as PR #63072 (which fixed
string[]arrays) by extending simple binding to handle collection interfaces:Key Changes:
IsBindableCollectionInterface()helper - Detects collection interface types that can be treated as arraysAsParameterspropertiesBindParameterFromValue()- Creates arrays internally and converts them to the target collection type (cast for interfaces, constructor forList<T>)GetExpressionType()- Made it parameter-aware to treat collection interfaces asstring[]only forAsParameterspropertiesPropertyAsParameterInfo- Preserves complex binding for direct[FromForm]parameters to maintain backward compatibilityWhat Now Works
IEnumerable<T>with[AsParameters]and[FromForm]IList<T>with[AsParameters]and[FromForm]ICollection<T>with[AsParameters]and[FromForm]List<T>with[AsParameters]and[FromForm]Where
Tisstringor any type with a validTryParsemethod.Backward Compatibility
No breaking changes - All existing tests pass
Scoped fix - Only applies to
[AsParameters]propertiesDirect parameters unchanged -
([FromForm] List<int> items)still uses complex binding for nested key support like["[0]"],["[1]"]Testing
Added comprehensive unit tests:
RequestDelegateHandlesAsParametersWithFromFormIEnumerable- Tests IEnumerableRequestDelegateHandlesAsParametersWithFromFormIList- Tests IListRequestDelegateHandlesAsParametersWithFromFormList- Tests ListRequestDelegateHandlesAsParametersWithFromFormIEnumerableString- Tests IEnumerableRequestDelegateHandlesAsParametersWithFromFormIntArray- Tests int[] to verify no regressionAll 2,111 tests (2,106 existing + 5 new) pass successfully.
Fixes #62329