Skip to content
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

Add a constructor with Module to GenericJackson2JsonRedisSerializer #2601

Closed
broaden-horizon opened this issue Jun 13, 2023 · 1 comment
Closed
Assignees
Labels
in: core Issues in core support type: enhancement A general enhancement

Comments

@broaden-horizon
Copy link

Adding Add a constructor with Module to GenericJackson2JsonRedisSerializer would be convenient.

When I tried to register just JavaTimeModule to an ObjectMapper of GenericJackson2JsonRedisSerializer, I had to configure it from scratch like below.

  @Bean
  @Primary
  public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    GenericJackson2JsonRedisSerializer.registerNullValueSerializer(mapper, null);
    StdTypeResolverBuilder builder = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.EVERYTHING,
        mapper.getPolymorphicTypeValidator());
    builder = builder.init(JsonTypeInfo.Id.CLASS, null);
    builder = builder.inclusion(JsonTypeInfo.As.PROPERTY);
    mapper.setDefaultTyping(builder);
    mapper.registerModule(new JavaTimeModule());
    return mapper;
  }

If the configuration already defined in a default constructor of GenericJackson2JsonRedisSerializer can be reused for adding Modules, I think it could make codes simpler and easier.

So it would look like below.

public GenericJackson2JsonRedisSerializer(List<Module> modules) {
    this((String) null);
    for (Module module : modules) {
        this.mapper.registerModule(module);
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 13, 2023
@broaden-horizon broaden-horizon changed the title Add a constructor with Module to GenericJackson2JsonRedisSerializer Add a constructor with Module to GenericJackson2JsonRedisSerializer Jun 13, 2023
@jxblum jxblum self-assigned this Jun 14, 2023
@jxblum jxblum added in: core Issues in core support type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 14, 2023
@jxblum
Copy link
Contributor

jxblum commented Jun 14, 2023

Exposing types from other libraries, such as Jackson, is not in the best interest of Spring Data Redis. A few reasons include, but are not limited to:

  1. The contracts of library types (for example, enabling additional capabilities of Jackson through the "registration" of additional Jackson Modules on a Jackson ObjectMapper) are subject to change in the future, and more importantly...
  2. There are more ways to configure/customize a Jackson ObjectMapper than simply registering additional Modules, such as by enabling or disabling mapping or de/serialization features, and so on, which leads to an increased and undesired footprint on the SD Redis GenericJackson2JsonRedisSerializer API.

A better approach, particularly in the case of configuring and customizing the "internal" Jackson ObjectMapper initialized by the GenericJackson2JsonRedisSerializer constructors that do not accept a user-provided Jackson ObjectMapper, is to expose an API that allows users to provide a callback (after construction) to further configure/customize the "internal" Jackson ObjectMapper.

For example:

class GenericJackson2JsonRedisSerializer {

    public GenericJackson2JsonRedisSerializer configure(Consumer<ObjectMapper> configurer) {
        // ...
    }

    // ...
}

@jxblum jxblum closed this as completed in f3de2d5 Jun 14, 2023
jxblum added a commit to jxblum/spring-data-redis that referenced this issue Jun 14, 2023
…Jackson2JsonRedisSerializer.

We now allow the internally created Jackson ObjectMapper to be customized and further configured after construction of the GenericJackson2JsonRedisSerializer when a user does not explicitly provide a custom ObjectMapper during construction. Even when providing a custom ObjectMapper, not all configuration applied by the GenericJackson2JsonRedisSerialzier (such as (standard) type resolution) to the internal ObjectMapper would get applied to the user-provided ObjectMapper as well.

Closes spring-projects#2601
@jxblum jxblum added this to the 3.1.1 (2023.0.1) milestone Jun 14, 2023
mp911de added a commit that referenced this issue Oct 13, 2023
Add since tag. Reformat code.

See #2601
mp911de added a commit that referenced this issue Oct 13, 2023
Add since tag. Reformat code.

See #2601
mp911de added a commit that referenced this issue Oct 19, 2023
Add since tag. Reformat code.

See #2601
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core support type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants