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 ParameterNamesModule to "well known" jackson modules #27511

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions spring-web/spring-web.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {
testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8")
testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
testImplementation("com.fasterxml.jackson.module:jackson-module-kotlin")
testImplementation("com.fasterxml.jackson.module:jackson-module-parameter-names")
testImplementation("org.apache.tomcat:tomcat-util")
testImplementation("org.apache.tomcat.embed:tomcat-embed-core")
testImplementation("org.eclipse.jetty:jetty-server")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,16 @@ private void registerWellKnownModulesIfAvailable(MultiValueMap<Object, Module> m
// jackson-datatype-jdk8 not available
}

try {
Class<? extends Module> parameterNamesModuleClass = (Class<? extends Module>)
ClassUtils.forName("com.fasterxml.jackson.module.paramnames.ParameterNamesModule", this.moduleClassLoader);
Module parameterNamesModule = BeanUtils.instantiateClass(parameterNamesModuleClass);
modulesToRegister.set(parameterNamesModule.getTypeId(), parameterNamesModule);
}
catch (ClassNotFoundException ex) {
// jackson-module-parameter-names not available
}

try {
Class<? extends Module> javaTimeModuleClass = (Class<? extends Module>)
ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", this.moduleClassLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import org.springframework.util.StringUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

Expand Down Expand Up @@ -257,6 +258,25 @@ void modulesToInstallByInstance() {
assertThat(serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass()).isSameAs(CustomIntegerSerializer.class);
}

static class ParameterModuleDto {

int x;
int y;

ParameterModuleDto(int x, int y) {
this.x = x;
this.y = y;
}

int getX() {
return x;
}

int getY() {
return y;
}
}

@Test
void wellKnownModules() throws JsonProcessingException, UnsupportedEncodingException {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
Expand All @@ -267,6 +287,9 @@ void wellKnownModules() throws JsonProcessingException, UnsupportedEncodingExcep
Optional<String> optional = Optional.of("test");
assertThat(new String(objectMapper.writeValueAsBytes(optional), "UTF-8")).isEqualTo("\"test\"");


assertThatCode(() -> objectMapper.readValue("{\"x\":1,\"y\":2}", ParameterModuleDto.class)).doesNotThrowAnyException();

// Kotlin module
IntRange range = new IntRange(1, 3);
assertThat(new String(objectMapper.writeValueAsBytes(range), "UTF-8")).isEqualTo("{\"start\":1,\"end\":3}");
Expand Down