Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Fix #1579 - Bind top-level collections as an empty collection #2583

Closed
wants to merge 3 commits into from

Conversation

rynowak
Copy link
Member

@rynowak rynowak commented May 19, 2015

This change treats 'top-level' collection-type models similarly to
top-level POCO model - namely that they will always be instantiated even
if there's no data to put inside.

var isTopLevelObject = bindingContext.ModelMetadata.ContainerType == null;
var hasExplicitAlias = bindingContext.BinderModelName != null;

if (isTopLevelObject && (hasExplicitAlias || bindingContext.ModelName == string.Empty))
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a simplified version of the logic used by MutableObjectModelBinder - simplified because we don't have to handle as many cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

Working on adding a few unit tests for this logic.

Copy link
Member

Choose a reason for hiding this comment

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

You're done "working on" those tests, correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

yep.

@rynowak
Copy link
Member Author

rynowak commented May 19, 2015

@dougbu updated with some unit tests

return null;
}

private static Type GetCollectionBinder(Type modelType)
private static GenericModelBindingInfo GetCollectionBinder(Type modelType)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest renaming this method and similar ones below.
GetCollectionBinder => GetCollectionBinderInfo

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

@dougbu
Copy link
Member

dougbu commented May 20, 2015

@rynowak
Copy link
Member Author

rynowak commented May 21, 2015

@dougbu Addressed most feedback, have one idea we should discuss.

bindingContext.ModelMetadata,
model);

return new ModelBindingResult(model, bindingContext.ModelName, true, validationNode);
Copy link
Member

Choose a reason for hiding this comment

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

pls name the true argument

This change treats 'top-level' collection-type models similarly to
top-level POCO model - namely that they will always be instantiated even
if there's no data to put inside.
@rynowak
Copy link
Member Author

rynowak commented May 22, 2015

Updated. Moved the model-creation down into the CollectionModelBinder classes.

// Caller (GenericModelBinder) was able to resolve a binder type and will create a ModelBindingResult
// that exits current ModelBinding loop.
// If this is the fallback case, and we failed to find data as a top-level model, then generate a
// default 'empty' model and return it.
Copy link
Member

Choose a reason for hiding this comment

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

Odd case. Curious whether Web API 2 or MVC 5 pass default(KeyValuePair<TKey, TValue>) into an action. Of course it's fine to support here.

@dougbu
Copy link
Member

dougbu commented May 22, 2015

:shipit:

@rynowak rynowak closed this May 22, 2015
@rynowak rynowak deleted the collection branch May 22, 2015 05:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants