[JAVA-3915] Unable to map Kotlin data class with an annotated field Created: 18/Dec/20 Updated: 08/Jan/21 Resolved: 08/Jan/21 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | POJO |
| Affects Version/s: | 4.1.1 |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Lammert Westerhoff | Assignee: | Ross Lawley |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||
| Description |
|
I'm using the PojoCodecProvider to map to Kotlin data classes. When the fields of those data classes contain annotations (unrelated to MongoDB) then an exception is thrown. Stacktrace:
It seems that it sees the annotation itself as a property to map. |
| Comments |
| Comment by Ross Lawley [ 08/Jan/21 ] | ||||||||||||||||||||||||||||||
|
I've fixed the NPE for now and have added | ||||||||||||||||||||||||||||||
| Comment by Githook User [ 08/Jan/21 ] | ||||||||||||||||||||||||||||||
|
Author: {'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}Message: Prevent NPE error in Pojo's with synthetic properties
| ||||||||||||||||||||||||||||||
| Comment by Jeffrey Yemin [ 18/Dec/20 ] | ||||||||||||||||||||||||||||||
|
It looks like kmongo has vendored the driver's POJO mapper and modified it to work with Kotlin data classes. | ||||||||||||||||||||||||||||||
| Comment by Lammert Westerhoff [ 18/Dec/20 ] | ||||||||||||||||||||||||||||||
|
I've just tried kmongo and it works fine. I first used the default Jackson engine to map to POJOs. But even when I use the POJO Codes engine as documented on https://litote.org/kmongo/quick-start/ it keeps working, which I was not expecting since I assumed it would run the same code path. | ||||||||||||||||||||||||||||||
| Comment by Jeffrey Yemin [ 18/Dec/20 ] | ||||||||||||||||||||||||||||||
|
Simplified data class that demonstrates the issue:
The problem seems to be that the POJO mapper is trying to map a synthetic property based on the generated method void getName$annotations(). | ||||||||||||||||||||||||||||||
| Comment by Jeffrey Yemin [ 18/Dec/20 ] | ||||||||||||||||||||||||||||||
|
OK, you might want to try using kmongo. I'm curious if it will work there. One issue is that we don't test the POJO mapper with Kotlin data classes, so I'm not surprised that there are issues. I tried first with a similar POJO and it works as expected:
| ||||||||||||||||||||||||||||||
| Comment by Lammert Westerhoff [ 18/Dec/20 ] | ||||||||||||||||||||||||||||||
|
I'm actually not familiar with kmongo. Though it seems interesting and I'll have a look. But in my case I'm using the Java driver directly:
And then fetching the records:
| ||||||||||||||||||||||||||||||
| Comment by Jeffrey Yemin [ 18/Dec/20 ] | ||||||||||||||||||||||||||||||
|
Just one more question, I think: are you using the kmongo wrapping driver, or using the Java driver directly? | ||||||||||||||||||||||||||||||
| Comment by Lammert Westerhoff [ 18/Dec/20 ] | ||||||||||||||||||||||||||||||
|
Sure.
As you can see it's in Kotlin. And we're using Kotlin serialization to later serialize the POJO to JSON. So it's unrelated to MongoDB as I mentioned. But still the @Transient annotation is causing the exceptions. (Same happens when I use kotlinx.serialization.SerialName). Without it it works fine. The @Serializable at class level does NOT cause any problems. | ||||||||||||||||||||||||||||||
| Comment by Jeffrey Yemin [ 18/Dec/20 ] | ||||||||||||||||||||||||||||||
|
Can you provide a sample POJO that demonstrates the problem? Thanks, |