Skip to content

Add getter method(getMapper()) to Serializer class that uses ObjectMapper #2342

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

Closed
doljae opened this issue Jun 9, 2022 · 2 comments
Closed
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@doljae
Copy link

doljae commented Jun 9, 2022

Description

After creating the XXXSerializer class, I suggest to add a getter method that can access the ObjectMapper to adjust the configurations.

If ObjectMapper is not specified in the current Serializer class constructor, the ObjectMapper that has been set internally by default is used.

In this state, if the user wants to make minor modifications to the ObjectMapper, since it cannot be accessed, it is inconvenient to create an ObjectMapper with new settings and then create a new Serializer.

And I know that DefaultTyping of ObjectMapper of GenericJackson2JsonRedisSerializer has been changed from DefaultTyping.NON_FINAL to EVERYTHING since version 2.7.0 due to the issue & PR below.

If there is a method that can access the ObjectMapper, it has the advantage of being able to flexibly change only the necessary parts of the default setting even after creating the Serializer.

Sample

Without getter()

// always make new ObjectMapper when add Modules or adjust configurations
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);

// duplicated -> same as default ObjectMapper configurations, 
mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(),
                             DefaultTyping.EVERYTHING,
                             As.PROPERTY);

return new GenericJackson2JsonRedisSerializer(mapper);

If a getter() method is provided for the ObjectMapper, it can be used flexibly in the same way as the test code below.

@Test
void genericJackson2JsonRedisSerializerCanCustomizeObjectMapperAfterCreated() {

    GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();

    serializer.getMapper() // add getter method for accessing ObjectMapper in Serializer
              .registerModule(new JavaTimeModule()) // just add module with default ObjectMapper
	      .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) // adjust configurations
	      .configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);
 
    ObjectWithOffsetDateTime source = new ObjectWithOffsetDateTime(1L, "doljae", OffsetDateTime.now());

    assertThat(serializer.deserialize(serializer.serialize(source))).isEqualTo(source);
}

Additional info

Regarding this, after folking the repository, I've already done some work targeting the GenericJackson2JsonRedisSerializer.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 9, 2022
@doljae
Copy link
Author

doljae commented Jun 21, 2022

@mp911de
It's been about two weeks since you posted the issue, so I'm curious about your opinion.

@mp911de
Copy link
Member

mp911de commented Jun 23, 2022

We do not want to expose the ObjectMapper from the serializer because that is a potential source for bugs and it leaks internal implementation details. Instead, please configure multiple serializers or create the serializer with a given ObjectMapper.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2022
@mp911de mp911de added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

3 participants