-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Handle broader range of collection types in model binding #2946
Conversation
/cc @rynowak |
{ | ||
return newCollection?.ToArray(); | ||
return collection?.ToArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will the default case here be that the model is already an array? What are the cases we might get here and have it not be an array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll never be an array since the base CollectionModelBinder
uses a List<TElement>
until it needs to copy everything into the final model. The value should not be null
but this was written defensively originally...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! Can you add that info to a comment here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌
⌚ overall looks good, tests look good. |
- #2793 - add `ICollectionModelBinder`, allowing `GenericModelBinder` to call `CreateEmptyCollection()` - adjust `CollectionModelBinder` and `DictionaryModelBinder` to activate model if default types are incompatible - do not create default (empty) top-level collection in fallback case if Model already non-`null` - change type checks in `GenericModelBinder` to align with `CollectionModelBinder` capabilities - add special case for `IEnumerable<T>` - correct `ModelMetadata` of a few tests that previously did not need that information
- did stuff
2033030
to
4311c87
Compare
Updated |
{ | ||
Debug.Assert(targetType == typeof(TElement[]), "GenericModelBinder only creates this binder for arrays."); | ||
|
||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why just do the thing you put in the assert? You could say with like 20 characters what all of this does, and technically be more correct 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
ICollectionModelBinder
, allowingGenericModelBinder
to callCreateEmptyCollection()
CollectionModelBinder
andDictionaryModelBinder
to activate model if default types are incompatiblenull
GenericModelBinder
to align withCollectionModelBinder
capabilitiesIEnumerable<T>
ModelMetadata
of a few tests that previously did not need that information