Fix/asparameters fromform collection interfaces #64753
Open
+227
−13
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