Skip to content

Commit 4d1717f

Browse files
liucheng.zhangsnicoll
authored andcommitted
Disable nulls serialization when serialize-nulls property is false
Closes gh-16332
1 parent 877a47f commit 4d1717f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,7 +84,8 @@ public void customize(GsonBuilder builder) {
8484
.toCall(builder::generateNonExecutableJson);
8585
map.from(properties::getExcludeFieldsWithoutExposeAnnotation)
8686
.toCall(builder::excludeFieldsWithoutExposeAnnotation);
87-
map.from(properties::getSerializeNulls).toCall(builder::serializeNulls);
87+
map.from(properties::getSerializeNulls).whenTrue()
88+
.toCall(builder::serializeNulls);
8889
map.from(properties::getEnableComplexMapKeySerialization)
8990
.toCall(builder::enableComplexMapKeySerialization);
9091
map.from(properties::getDisableInnerClassSerialization)

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,14 +81,23 @@ public void excludeFieldsWithoutExposeAnnotation() {
8181
}
8282

8383
@Test
84-
public void serializeNulls() {
84+
public void serializeNullsTrue() {
8585
this.contextRunner.withPropertyValues("spring.gson.serialize-nulls:true")
8686
.run((context) -> {
8787
Gson gson = context.getBean(Gson.class);
8888
assertThat(gson.serializeNulls()).isTrue();
8989
});
9090
}
9191

92+
@Test
93+
public void serializeNullsFalse() {
94+
this.contextRunner.withPropertyValues("spring.gson.serialize-nulls:false")
95+
.run((context) -> {
96+
Gson gson = context.getBean(Gson.class);
97+
assertThat(gson.serializeNulls()).isFalse();
98+
});
99+
}
100+
92101
@Test
93102
public void enableComplexMapKeySerialization() {
94103
this.contextRunner

0 commit comments

Comments
 (0)