- 
                Notifications
    You must be signed in to change notification settings 
- Fork 10.5k
Fix CollectionModelBinder BindSimpleCollection null value handling #42278
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
Fix CollectionModelBinder BindSimpleCollection null value handling #42278
Conversation
| Thanks for your PR, @campersau. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. | 
        
          
                src/Mvc/Mvc.Core/test/ModelBinding/Binders/CollectionModelBinderTest.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/Mvc/Mvc.Core/src/ModelBinding/Binders/CollectionModelBinder.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/Mvc/Mvc.Core/test/ModelBinding/Binders/CollectionModelBinderTest.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
      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.
I'm fine w/ this version but defer to @brunolins16 for final approval.
One slight reservation: It feels odd to favour functional tests over unit tests here. Were we previously missing coverage of custom IValueProviderFactory / IValueProvider implementations❔
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.
The change looks good. Thanks for that. I have a small ask for an additional test and we will be good to go.
        
          
                src/Mvc/test/WebSites/BasicWebSite/Controllers/CustomValueProviderController.cs
          
            Show resolved
            Hide resolved
        
      c84fd39    to
    03a9a81      
    Compare
  
    | @campersau Thanks for your contribution. | 
Fix CollectionModelBinder BindSimpleCollection null value handling
nullis now ignored instead of receiving the previous value.Because
StringValuesofnullequalsValueProviderResult.Noneand theCompositeValueProvidercontained theElementalValueProviderfrom the previous iteration, it got used instead.There was also an interesting case when
nullis the first value, because then the originalIValueProvidermight be called again, which returns the original array (or something else if it is computed) and because the array can not be bound to an element value it received actuallynull.I don't think the
CompositeValueProvideris needed at all, also thebindingContext.ValueProvidershould be set inside ofEnterNestedScopeso the originalbindingContext.ValueProvideris properly restored afterwards.[42,100,null,200][42,100,100,200][42,100,200][null,42,100,200][null,42,100,200][42,100,200]Fixes #40929
⚠Dangerous hack to get `null` values ⚠
Since
ValueProviderResult.Noneis currently notreadonly(I guess that's a bug?) you can set it to something else and then you would getnullback instead of it being ignored: