google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Reading empty lists from MongoDB results in null #278

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Using Morphia 0.97

Trying to read from Mongo:

{
    "_id" : BinData(3,"iEv5u1K/AQgJnPSHpsuYsQ=="),
    "participants" : [ ]
}

Into a class like this:

class Conversation {
   @Id private UUID id;
   @Embedded List<UUID> participants;
}

results in the participants list being set to null.  This seems incorrect; 
there is a list on the Mongo side, and it is not null, so I'd intuitively 
expect Morphia to set the list to a non-null reference.

From looking at the code, it appears that just removing the if(values.size() > 
0) check in EmbeddedMapper.readCollection() would fix this case.

This is similar to issue #263 which was closed as a won't fix; this is the 
opposite side of the same issue.  I don't understand the logic in closing that 
one, but mentioning it because maybe the same logic applies here too 
(interested to know what the logic is).

Original issue reported on code.google.com by m...@lewisworld.org on 18 May 2011 at 7:50

GoogleCodeExporter commented 9 years ago
By default morphia doesn't save empty lists and won't replace the field if the 
data being loaded is missing or empty.

It is null because you are not setting it on construction, or after.

That fix is not wanted; it works as it should. If you want it non-null, then 
declare it as such:

class Conversation {
   @Id private UUID id;
   @Embedded List<UUID> participants = new ArrayList();
}

Original comment by scotthernandez on 18 May 2011 at 8:25