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

Jackson2ObjectMapperBuilder breaks when modules customizer follows modulesToInstall #30751

Closed
Kamii0909 opened this issue Jun 25, 2023 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@Kamii0909
Copy link

In Spring 5.3, to address this issue, Spring added a new API here. It is supposed to be used like this:

@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
    return builder -> builder.modules(list -> list.add(...));
}

Sadly, if you add exactly that configuration along side JacksonAutoConfiguration, Spring Boot would break with an unhinged java.lang.UnsupportedOperationException: null. This is due to the following lines in JacksonAutoConfiguration:

private void configureModules(Jackson2ObjectMapperBuilder builder) {
    builder.modulesToInstall(this.modules.toArray(new Module[0]));
}

The cause is the implementation of Jackson2ObjectMapperBuilder.modulesToInstall(Module...modules), particularly this line:

this.modules = Arrays.asList(modules);

Spring will reuse this.modules and use it when you call builder.modules(Consumer<List<Module>>) causing the exception.

A solution should be easy, we can either patch JacksonAutoConfiguration or on Spring side.

Currently, for a workaround, it is reasonable to make do with:

@Bean
Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    return builder -> builder.modules(list -> {
            List<Module> modules = new ArrayList<>(list);
            // extra modules here
            builder.modules(modules);
    });
}
@Kamii0909 Kamii0909 changed the title JacksonAutoConfiguration in conjunction with new module API JacksonAutoConfiguration in conjunction with new API Jun 25, 2023
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 25, 2023
@wilkinsona
Copy link
Member

Thanks for the report. The javadoc of Jackson2ObjectMapperBuilder advises against mixing modules and modulesToInstall, however your customizer would still cause a failure if it used modulesToInstall(Consumer<List<Module>>). I've asked the Framework team for their opinion.

@bclozel bclozel transferred this issue from spring-projects/spring-boot Jun 26, 2023
@jhoeller jhoeller changed the title JacksonAutoConfiguration in conjunction with new API Jackson2ObjectMapperBuilder breaks when modules customizer follows modulesToInstall Jun 26, 2023
@jhoeller jhoeller added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 26, 2023
@jhoeller jhoeller self-assigned this Jun 26, 2023
@jhoeller jhoeller added this to the 6.0.11 milestone Jun 26, 2023
@jhoeller jhoeller added the for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x label Jun 26, 2023
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x labels Jun 26, 2023
jhoeller added a commit that referenced this issue Jun 26, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants