-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Map.of() cannot be deserialized #4036
Comments
Also, the I think it would be better if |
Thank you for raising this issue about Jackson. The main branch (v5) is already on Jackson 2.13. So the fix will be in Spring Batch 5.0-M1. v4.3.x uses Jackson 2.11. We typically only upgrade dependencies to their latest patch versions in a patch release of Spring Batch, so upgrading Jackson to 2.13 in v4.3.x is risky. But you can always override the dependency and use any compatible version of Jackson.
I think the default object mapper is not accessible by design. Making it accessible could open the door to security issues, that's why the default instance is closed for modification. |
Closing this as 5.0.0-M1 has been released and which depends on Jackson 2.13.1. |
Hi, wanted to point out a few other things:
My current solution: @Bean
@SneakyThrows
public ExecutionContextSerializer customSerializer() {
val serializer = new Jackson2ExecutionContextStringSerializer(
"java.util.ImmutableCollections$ListN",
"java.util.ImmutableCollections$List12",
"java.util.ImmutableCollections$SubList",
"java.util.ImmutableCollections$Set12",
"java.util.ImmutableCollections$SetN",
"java.util.ImmutableCollections$Map1",
"java.util.ImmutableCollections$MapN",
"my.Clazz"
);
((ObjectMapper) FieldUtils.readField(serializer, "objectMapper", true))
.enable(MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL)
.findAndRegisterModules();
return serializer;
} |
Spring Batch provides a setter of the object mapper in order to use your custom one. I'm not sure I understand why you need reflection to set it on the serializer. The following should cover the 3 points you mentioned: @Bean
@SneakyThrows
public ExecutionContextSerializer customSerializer() {
Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer(
"java.util.ImmutableCollections$ListN",
"java.util.ImmutableCollections$List12",
"java.util.ImmutableCollections$SubList",
"java.util.ImmutableCollections$Set12",
"java.util.ImmutableCollections$SetN",
"java.util.ImmutableCollections$Map1",
"java.util.ImmutableCollections$MapN",
"my.Clazz"
);
ObjectMapper objectMapper = new ObjectMapper()
.enable(MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL)
.findAndRegisterModules();
serializer.setObjectMapper(objectMapper);
return serializer;
} |
I want to use
Map.of()
in my execution context (primarily for tests). But this gives an error:Output:
The problem is that the typing inside
Jackson2ExecutionContextStringSerializer
is configured toObjectMapper.DefaultTyping.NON_FINAL
which skips the addition of@class
property to the root object for some reason, even thoughImmutableCollections$Map1
isfinal
. So, as you can see, only the inner map has it. But if we try to serializenew HashMap<>(Map.of("f", Map.of("f1", "v1")))
then it does include it for the root.One solution to it is to use
ObjectMapper.DefaultTyping.EVERYTHING
or enableMapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL
so that theObjectMapper
doesn't need to check against@class
property if it's given the type at compile time.It knows that it's supposed to be a
HashMap
fromJackson2ExecutionContextStringSerializer#deserialize
:So, with enabling the feature, another error comes out:
The output:
I think all
java.util.ImmutableCollections$*
should be enabled by default.With adding those, another error comes out:
The output:
That's a Jackson issue FasterXML/jackson-databind#2900 that's already been fixed in
2.13.0
. So, after updating the Jackson library to2.13.0
the last code snippet gives the correct output:Environment
Spring Batch 4.3.3
Spring Boot 2.5.0
JDK Amazon Corretto 11.0.13
The text was updated successfully, but these errors were encountered: