Merge pull request #11591 from izeye:gson

* pr/11591:
  Polish "Make GsonAutoConfiguration align with JacksonAutoConfiguration"
  Make GsonAutoConfiguration align with JacksonAutoConfiguration
pull/11497/merge
Stephane Nicoll 7 years ago
commit 644925e9f0

@ -43,6 +43,7 @@ import org.springframework.core.Ordered;
public class GsonAutoConfiguration { public class GsonAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(GsonBuilder.class)
public GsonBuilder gsonBuilder(List<GsonBuilderCustomizer> customizers) { public GsonBuilder gsonBuilder(List<GsonBuilderCustomizer> customizers) {
GsonBuilder builder = new GsonBuilder(); GsonBuilder builder = new GsonBuilder();
customizers.forEach(c -> c.customize(builder)); customizers.forEach(c -> c.customize(builder));

@ -25,6 +25,7 @@ import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes; import com.google.gson.FieldAttributes;
import com.google.gson.FieldNamingPolicy; import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy; import com.google.gson.LongSerializationPolicy;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.Test; import org.junit.Test;
@ -32,6 +33,7 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -40,6 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author David Liu * @author David Liu
* @author Ivan Golovko * @author Ivan Golovko
* @author Stephane Nicoll
*/ */
public class GsonAutoConfigurationTests { public class GsonAutoConfigurationTests {
@ -146,13 +149,23 @@ public class GsonAutoConfigurationTests {
@Test @Test
public void additionalGsonBuilderCustomization() { public void additionalGsonBuilderCustomization() {
this.contextRunner.withUserConfiguration(GsonBuilderCustomConfig.class) this.contextRunner.withUserConfiguration(GsonBuilderCustomizerConfig.class)
.run(context -> { .run(context -> {
Gson gson = context.getBean(Gson.class); Gson gson = context.getBean(Gson.class);
assertThat(gson.toJson(new DataObject())).isEqualTo("{}"); assertThat(gson.toJson(new DataObject())).isEqualTo("{}");
}); });
} }
@Test
public void customGsonBuilder() {
this.contextRunner.withUserConfiguration(GsonBuilderConfig.class)
.run(context -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson.toJson(new DataObject()))
.isEqualTo("{\"data\":1,\"owner\":null}");
});
}
@Test @Test
public void withPrettyPrinting() { public void withPrettyPrinting() {
this.contextRunner.withPropertyValues("spring.gson.pretty-printing:true") this.contextRunner.withPropertyValues("spring.gson.pretty-printing:true")
@ -164,7 +177,7 @@ public class GsonAutoConfigurationTests {
} }
@Test @Test
public void withoutLenient() throws Exception { public void withoutLenient() {
this.contextRunner.run(context -> { this.contextRunner.run(context -> {
Gson gson = context.getBean(Gson.class); Gson gson = context.getBean(Gson.class);
/* /*
@ -180,7 +193,7 @@ public class GsonAutoConfigurationTests {
} }
@Test @Test
public void withLenient() throws Exception { public void withLenient() {
this.contextRunner.withPropertyValues("spring.gson.lenient:true").run(context -> { this.contextRunner.withPropertyValues("spring.gson.lenient:true").run(context -> {
Gson gson = context.getBean(Gson.class); Gson gson = context.getBean(Gson.class);
@ -225,7 +238,8 @@ public class GsonAutoConfigurationTests {
}); });
} }
protected static class GsonBuilderCustomConfig { @Configuration
static class GsonBuilderCustomizerConfig {
@Bean @Bean
public GsonBuilderCustomizer customSerializationExclusionStrategy() { public GsonBuilderCustomizer customSerializationExclusionStrategy() {
@ -245,6 +259,16 @@ public class GsonAutoConfigurationTests {
} }
@Configuration
static class GsonBuilderConfig {
@Bean
public GsonBuilder customGsonBuilder() {
return new GsonBuilder().serializeNulls();
}
}
public class DataObject { public class DataObject {
public static final String STATIC_DATA = "bye"; public static final String STATIC_DATA = "bye";
@ -252,6 +276,9 @@ public class GsonAutoConfigurationTests {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private Long data = 1L; private Long data = 1L;
@SuppressWarnings("unused")
private String owner = null;
public void setData(Long data) { public void setData(Long data) {
this.data = data; this.data = data;
} }

Loading…
Cancel
Save