-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Serialized result of java Class & Record is different with RedisTemplate #3512
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
Comments
Can you give a test case that does not use spring? But my guess is that DefaultTyping.NON_FINAL is the issue – records are final. |
I write a test case without using Spring, same ObjectMapper configuration. class MapperTest {
@Test
void test() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper()
.registerModule(new ParameterNamesModule(JsonCreator.Mode.DEFAULT))
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(),
DefaultTyping.NON_FINAL,
As.PROPERTY);
final RequestDto dto = new RequestDto(1L, "doljae", OffsetDateTime.now());
final RequestRecord record = new RequestRecord(1L, "doljae", OffsetDateTime.now());
final byte[] dtoBytes = mapper.writeValueAsBytes(dto);
final byte[] recordBytes = mapper.writeValueAsBytes(record);
// how to print the result?...
System.out.println(Arrays.toString(dtoBytes));
System.out.println(Arrays.toString(recordBytes));
}
} |
Could you please explain in more detail about The As you said, these settings seem to be the clues to the problem, and I googled and looked at the documentation, but I didn't quite understand. Could you please explain in more detail? Or if you have a reference link that explains it in detail, please share. |
i can reproduce your issue with the new test case, and yes it is because of A better alternative is to use |
Spring Data Redis uses
Could you please explain in more detail about ParameterNamesModule and activateDefaultTyping? |
@doljae getting the ObjectMapper configuration is something that Spring Data Redis users or maintainers can help with. I think your problem can be divided in two parts:
But one other thing I would STRONGLY recommend: instead of attempting to serialize any given "Root value" directly, you really should instead use a wrapper if possible: something like:
This will avoid many pitfalls, including problem of Java Type Erasure for the root value. In your case, for example, one problem is that |
I improved my understanding by looking for settings of I still don't quite understand the principle. In other words, I did not fully understand exactly what the role of the However, as @yawkat said, I understood that root class information was not saved when saving a mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(),
DefaultTyping.NON_FINAL,
As.PROPERTY); // <- this is the point Here's what I can do in this situation: 1. Using
|
By the way, although I'm not a good English speaker, I couldn't find any really good material about detailed settings related to ObjectMapper and Jackson. (But there is a possibility that I did not understand it well) If you have a reference that you can recommend, please share it. The more detailed and the more examples, the better. |
Sorry, I do not think there is much good documentation regarding this particular configuration (default typing). But one quick comment. This:
is something I strongly suggest you change, you should usually not use This may not be the main issue you are facing but I would avoid this setting anyway. However, I do have one question: why do you not try the approach I suggested as the best solution:
|
@cowtowncoder To sum it upAs, the more general setting you recommended.It is better to use
|
Not quite sure what would be needed, and since Record-type handling has been improved a lot since 2.13, closing as there's no action planned for now. May be re-filed with more specific request for change. |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
Serialized result of java
Class
&Record
is different withRedisTemplate
.Version information
2.13.3
To Reproduce
Expected behavior
Both class and record objects should be stored and deserialized normally in RedisTemplate.
Additional context
The issue occurred while using
Spring Data Redis
class, but sinceObjectMapper
does serialization and deserialization, I reported it to Jackson issue borad.You can reproduce this issue with this repository's test class (use redis docker image in
repo/docker/docker-compose.yml
)I found that if i adjust ObjectMapper Configuration, both Class & Record serialize & deserialize normally.
The text was updated successfully, but these errors were encountered: