Skip to content

Commit

Permalink
Add ParameterNamesModule to "well known" jackson modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Kruglov authored and sdeleuze committed Aug 28, 2023
1 parent f054c2e commit 2f43f77
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions spring-web/spring-web.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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("com.squareup.okhttp3:mockwebserver")
testImplementation("io.micrometer:micrometer-observation-test")
testImplementation("io.projectreactor:reactor-test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,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,8 @@
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 @@ -255,6 +257,25 @@ void modulesToInstallWithConsumer() {
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 @@ -265,6 +286,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

0 comments on commit 2f43f77

Please sign in to comment.