Skip to content

Commit

Permalink
Add auto-configuration for Jackson's parameter names module
Browse files Browse the repository at this point in the history
The parameter names module allows users of Java 8 that have compiled
their code with the -parameters option to avoid the name for
annotations to map the json onto constructor and method parameters
with the names of the parameters being used instead.

This commit adds auto-configuration for the module that will only
be enabled when running on Java 8.

Closes spring-projectsgh-3804
  • Loading branch information
wilkinsona committed Sep 21, 2015
1 parent 6c59e75 commit 7f2be6c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
Expand All @@ -44,12 +46,14 @@
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
import com.fasterxml.jackson.datatype.joda.ser.DateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

/**
* Auto configuration for Jackson. The following auto-configuration will get applied:
Expand Down Expand Up @@ -129,6 +133,19 @@ private JacksonJodaDateFormat getJacksonJodaDateFormat() {

}

@Configuration
@ConditionalOnJava(JavaVersion.EIGHT)
@ConditionalOnClass(ParameterNamesModule.class)
static class ParameterNamesModuleConfiguration {

@Bean
@ConditionalOnMissingBean(ParameterNamesModule.class)
public ParameterNamesModule parametersNameModule() {
return new ParameterNamesModule(JsonCreator.Mode.PROPERTIES);
}

}

@Configuration
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
@EnableConfigurationProperties(JacksonProperties.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
Expand All @@ -50,8 +52,10 @@
import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
Expand Down Expand Up @@ -412,6 +416,28 @@ public void customLocale() throws JsonProcessingException {
objectMapper.writeValueAsString(dateTime));
}

@Test
public void parameterNamesModuleIsAutoConfigured() {
assertParameterNamesModuleCreatorBinding(Mode.PROPERTIES,
JacksonAutoConfiguration.class);
}

@Test
public void customParameterNamesModuleCanBeConfigured() {
assertParameterNamesModuleCreatorBinding(Mode.DELEGATING,
ParameterNamesModuleConfig.class, JacksonAutoConfiguration.class);
}

private void assertParameterNamesModuleCreatorBinding(Mode expectedMode,
Class<?>... configClasses) {
this.context.register(configClasses);
this.context.refresh();
Annotated annotated = mock(Annotated.class);
Mode mode = this.context.getBean(ObjectMapper.class).getDeserializationConfig()
.getAnnotationIntrospector().findCreatorBinding(annotated);
assertThat(mode, is(equalTo(expectedMode)));
}

public static class MyDateFormat extends SimpleDateFormat {

public MyDateFormat() {
Expand Down Expand Up @@ -469,6 +495,16 @@ public ObjectMapper objectMapper() {

}

@Configuration
protected static class ParameterNamesModuleConfig {

@Bean
public ParameterNamesModule parameterNamesModule() {
return new ParameterNamesModule(JsonCreator.Mode.DELEGATING);
}

}

protected static final class Foo {

private String name;
Expand Down

0 comments on commit 7f2be6c

Please sign in to comment.