[JAVA-2587] Null property during serialization breaks deserialization with @BsonCreator Created: 24/Aug/17 Updated: 29/Oct/23 Resolved: 08/Sep/17 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | POJO |
| Affects Version/s: | 3.5.0 |
| Fix Version/s: | 3.6.0 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Paulo Melo | Assignee: | Ross Lawley |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Description |
|
If a POJO is saved with a field with a null value, it ends up not being serialized in the resulting Json, causing the deserialization to fail with a missing field exception: Could not construct new instance of: Candidate. Missing the following properties: [mothersName] In this case, my POJO has the property `mothersName` as `null`. |
| Comments |
| Comment by Ross Lawley [ 06/Sep/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Scheduled to be backported to 3.5.1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Paulo Melo [ 06/Sep/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Awesome, thanks! | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ross Lawley [ 06/Sep/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks paulo_melo for the ticket, This is now fixed and scheduled for the 3.6.0 release. We now try and default any missing parameters to null. This lets the creator handle if nulls are appropriate or not. If the creator throws the original missing parameters error will be thrown. All the best, Ross | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Githook User [ 05/Sep/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Author: {'username': 'rozza', 'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com'}Message: Allow for missing parameters when creating new instances Make any missing @BsonProperty values null, then try to construct a new instance.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Paulo Melo [ 04/Sep/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks! In Jackson (where I suppose you guys got the inspiration) you can define if a given JsonProperty is required or not, as a second parameter to the annotation, being required = false the default value. I believe that BsonProperty can have the same behavior, but I think that adding this parameter would break backwards compatibility.. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ross Lawley [ 04/Sep/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi paulo_melo, Thanks for that, I can see the issue, as no value is stored in the database, there is no value that the @BsonCreator can use. If the null values were stored in the database, I suspect this would work as expected. I'll investigate if the BsonCreator convention can determine if nulls are acceptable for missing values based on the underlying ClassModel. Ross | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Paulo Melo [ 04/Sep/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hello and sorry for the delay, somehow I didn't get notified of this. The POJO is this one:
This is how I've instantiated the class:
See that I've nulled out three parameters: address, phone and motherName. This is the stacktrace I get when trying to fetch my Candidate from the database:
The behavior I expected was to deserialize these parameters as null, instead of throwing an exception. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ross Lawley [ 04/Sep/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi paulo_melo, Can you provide anymore information? So we can replicate this issue and look to provide a solution. Ross | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Jeffrey Yemin [ 24/Aug/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks for the report. To help ensure that we understand the exact issue you're running in to, please provide the source code for your Pojo as well as the stack trace of the exception that's being thrown. |