-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for @JsonValue custom ID types #224
Comments
Not at this time, at least not without fakery inside the class (like declaring the _id field as String and then converting to/from it...) |
Thanks, that's what I was afraid of. Is this something that could be added? |
I think so. It involves determining if the jackson-converted value will be something mongo will use as an id, though, so the solution will have to be thought through a bit. I played with some options this morning but I don't know if I like them. |
I managed to make it work by adding
at the bottom of |
I did something a bit similar: try {
final JsonNode tree = objectMapper.valueToTree(value);
if (tree.isTextual()) {
return new BsonString(tree.asText());
}
} catch (RuntimeException e) {
throw new IllegalArgumentException(String.format("Unsupported ID type: %s", value.getClass()), e);
} And I wrote some tests. But I wanted to support the other types and I ran into test problems that seem related to newer JDKs, and then I had to move onto assigned work. :) I'll see if I can get to it more seriously soon. |
I'm afraid to report that I've found further code in my codebase (yay legacy codebases) that uses an id field with an object, which apparently mongojack 2.x and Mongo happily allow (totally didn't know that).
If we want MongoJack to support this... that would be really nice (and backwards compatible with 2.x!). Though I could understand if you didn't want to. (BTW, I found that any version of Jackson below 2.13.3 does not work with Java 17, in case that's what you're running into) |
I found that this works
|
I have published a |
Thanks so much! Trying this out now. |
This does appear to work for me! Thanks! |
Fix deployed 4.5.1 and 4.7.0 |
Thank you! |
I'm finally trying to migrate from 2.x. I have a lot of code of the form:
where
CustomId
is a class with@JsonValue
and@JsonCreator
annotations that allow Jackson to serialize it to a string. With 2.x, this was supported and used the String serialization ofCustomId
as the id.With 3.x and 4.x, the generic key type has been removed from JacksonMongoCollection, and I get the following error when trying to insert:
Is there any way to get the equivalent old functionality?
The text was updated successfully, but these errors were encountered: