Mapping an array which is null becomes an empty array #63

Closed
jbogard opened this Issue Sep 2, 2011 · 6 comments

Projects

None yet

3 participants

@jbogard
Member
jbogard commented Sep 2, 2011

boswald[CodePlex]
Hey Guys,

I am mapping an array in a class. In my source class it is null but when it gets mapped it becomes an array of 0 size.

I have tried the :

Mapper.Initialize(c =>
{
c.AllowNullDestinationValues = true;
});

option but this does not keep the array as a null value and instead creates the array of empty size.

Is there a way I can get the array to stay null? or is this a bug?

@jbogard
Member
jbogard commented Sep 2, 2011

DJHightower[CodePlex]
I can approve this. This is also true for Lists.
Should not behave like that imho.

@jbogard
Member
jbogard commented Sep 2, 2011

jbogard[CodePlex]
From Framework Design Guidelines and other C# references, arrays, lists, collections, dictionaries and IEnumerables should NEVER be null, ever. You always only check for empty values. Any method or property that returns one of these items should guarantee that an empty collection is returned, rather than a null.

Since this is the behavior of the entire BCL, what's the use case of wanting a null collection?

@jbogard
Member
jbogard commented Sep 2, 2011

boswald[CodePlex]
The area that I am using this tool. The starting object has null lists. I am then serializing to xml. in the case of a null value the node gets the following attribute:

<mynode xsi:nil="true" />

In the case of an empty list the node comes out as

<mynode />

which causes a problem on the xsd validation.

@jbogard
Member
jbogard commented Sep 2, 2011

ebersys[CodePlex]
agree, some properties (of type collection) may not have been assigned to anything depending on logic; this changes the behavior, violates the principle of least astonishment in my opinion, if the original object had a null collection, I expect the new object to have a null collection as well

@jbogard jbogard closed this in f3c28fe Nov 7, 2011
@tpatel11
tpatel11 commented Sep 12, 2016 edited

Replying pretty late but these two works fine.

                        configuration.AllowNullCollections = true;
                        configuration.AllowNullDestinationValues = true;
@smiles2424
smiles2424 commented Sep 29, 2016 edited

@tpatel11 your comment helped me so its never too late!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment