Skip to content

Commit a27cedc

Browse files
committed
Merge branch '3.2.x'
Closes gh-39525
2 parents 7f55cae + 5746886 commit a27cedc

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -78,17 +78,22 @@ public int getOrder() {
7878
public void customize(GsonBuilder builder) {
7979
GsonProperties properties = this.properties;
8080
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
81-
map.from(properties::getGenerateNonExecutableJson).toCall(builder::generateNonExecutableJson);
81+
map.from(properties::getGenerateNonExecutableJson).whenTrue().toCall(builder::generateNonExecutableJson);
8282
map.from(properties::getExcludeFieldsWithoutExposeAnnotation)
83+
.whenTrue()
8384
.toCall(builder::excludeFieldsWithoutExposeAnnotation);
8485
map.from(properties::getSerializeNulls).whenTrue().toCall(builder::serializeNulls);
85-
map.from(properties::getEnableComplexMapKeySerialization).toCall(builder::enableComplexMapKeySerialization);
86-
map.from(properties::getDisableInnerClassSerialization).toCall(builder::disableInnerClassSerialization);
86+
map.from(properties::getEnableComplexMapKeySerialization)
87+
.whenTrue()
88+
.toCall(builder::enableComplexMapKeySerialization);
89+
map.from(properties::getDisableInnerClassSerialization)
90+
.whenTrue()
91+
.toCall(builder::disableInnerClassSerialization);
8792
map.from(properties::getLongSerializationPolicy).to(builder::setLongSerializationPolicy);
8893
map.from(properties::getFieldNamingPolicy).to(builder::setFieldNamingPolicy);
89-
map.from(properties::getPrettyPrinting).toCall(builder::setPrettyPrinting);
90-
map.from(properties::getLenient).toCall(builder::setLenient);
91-
map.from(properties::getDisableHtmlEscaping).toCall(builder::disableHtmlEscaping);
94+
map.from(properties::getPrettyPrinting).whenTrue().toCall(builder::setPrettyPrinting);
95+
map.from(properties::getLenient).whenTrue().toCall(builder::setLenient);
96+
map.from(properties::getDisableHtmlEscaping).whenFalse().toCall(builder::disableHtmlEscaping);
9297
map.from(properties::getDateFormat).to(builder::setDateFormat);
9398
}
9499

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

+70-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -59,7 +59,7 @@ void gsonRegistration() {
5959
}
6060

6161
@Test
62-
void generateNonExecutableJson() {
62+
void generateNonExecutableJsonTrue() {
6363
this.contextRunner.withPropertyValues("spring.gson.generate-non-executable-json:true").run((context) -> {
6464
Gson gson = context.getBean(Gson.class);
6565
assertThat(gson.toJson(new DataObject())).isNotEqualTo("{\"data\":1}");
@@ -68,14 +68,31 @@ void generateNonExecutableJson() {
6868
}
6969

7070
@Test
71-
void excludeFieldsWithoutExposeAnnotation() {
71+
void generateNonExecutableJsonFalse() {
72+
this.contextRunner.withPropertyValues("spring.gson.generate-non-executable-json:false").run((context) -> {
73+
Gson gson = context.getBean(Gson.class);
74+
assertThat(gson.toJson(new DataObject())).isEqualTo("{\"data\":1}");
75+
});
76+
}
77+
78+
@Test
79+
void excludeFieldsWithoutExposeAnnotationTrue() {
7280
this.contextRunner.withPropertyValues("spring.gson.exclude-fields-without-expose-annotation:true")
7381
.run((context) -> {
7482
Gson gson = context.getBean(Gson.class);
7583
assertThat(gson.toJson(new DataObject())).isEqualTo("{}");
7684
});
7785
}
7886

87+
@Test
88+
void excludeFieldsWithoutExposeAnnotationFalse() {
89+
this.contextRunner.withPropertyValues("spring.gson.exclude-fields-without-expose-annotation:false")
90+
.run((context) -> {
91+
Gson gson = context.getBean(Gson.class);
92+
assertThat(gson.toJson(new DataObject())).isEqualTo("{\"data\":1}");
93+
});
94+
}
95+
7996
@Test
8097
void serializeNullsTrue() {
8198
this.contextRunner.withPropertyValues("spring.gson.serialize-nulls:true").run((context) -> {
@@ -93,7 +110,7 @@ void serializeNullsFalse() {
93110
}
94111

95112
@Test
96-
void enableComplexMapKeySerialization() {
113+
void enableComplexMapKeySerializationTrue() {
97114
this.contextRunner.withPropertyValues("spring.gson.enable-complex-map-key-serialization:true")
98115
.run((context) -> {
99116
Gson gson = context.getBean(Gson.class);
@@ -103,6 +120,17 @@ void enableComplexMapKeySerialization() {
103120
});
104121
}
105122

123+
@Test
124+
void enableComplexMapKeySerializationFalse() {
125+
this.contextRunner.withPropertyValues("spring.gson.enable-complex-map-key-serialization:false")
126+
.run((context) -> {
127+
Gson gson = context.getBean(Gson.class);
128+
Map<DataObject, String> original = new LinkedHashMap<>();
129+
original.put(new DataObject(), "a");
130+
assertThat(gson.toJson(original)).contains(DataObject.class.getName()).doesNotContain("\"data\":");
131+
});
132+
}
133+
106134
@Test
107135
void notDisableInnerClassSerialization() {
108136
this.contextRunner.run((context) -> {
@@ -113,14 +141,23 @@ void notDisableInnerClassSerialization() {
113141
}
114142

115143
@Test
116-
void disableInnerClassSerialization() {
144+
void disableInnerClassSerializationTrue() {
117145
this.contextRunner.withPropertyValues("spring.gson.disable-inner-class-serialization:true").run((context) -> {
118146
Gson gson = context.getBean(Gson.class);
119147
WrapperObject wrapperObject = new WrapperObject();
120148
assertThat(gson.toJson(wrapperObject.new NestedObject())).isEqualTo("null");
121149
});
122150
}
123151

152+
@Test
153+
void disableInnerClassSerializationFalse() {
154+
this.contextRunner.withPropertyValues("spring.gson.disable-inner-class-serialization:false").run((context) -> {
155+
Gson gson = context.getBean(Gson.class);
156+
WrapperObject wrapperObject = new WrapperObject();
157+
assertThat(gson.toJson(wrapperObject.new NestedObject())).isEqualTo("{\"data\":\"nested\"}");
158+
});
159+
}
160+
124161
@Test
125162
void withLongSerializationPolicy() {
126163
this.contextRunner.withPropertyValues("spring.gson.long-serialization-policy:" + LongSerializationPolicy.STRING)
@@ -156,13 +193,21 @@ void customGsonBuilder() {
156193
}
157194

158195
@Test
159-
void withPrettyPrinting() {
196+
void withPrettyPrintingTrue() {
160197
this.contextRunner.withPropertyValues("spring.gson.pretty-printing:true").run((context) -> {
161198
Gson gson = context.getBean(Gson.class);
162199
assertThat(gson.toJson(new DataObject())).isEqualTo("{\n \"data\": 1\n}");
163200
});
164201
}
165202

203+
@Test
204+
void withPrettyPrintingFalse() {
205+
this.contextRunner.withPropertyValues("spring.gson.pretty-printing:false").run((context) -> {
206+
Gson gson = context.getBean(Gson.class);
207+
assertThat(gson.toJson(new DataObject())).isEqualTo("{\"data\":1}");
208+
});
209+
}
210+
166211
@Test
167212
void withoutLenient() {
168213
this.contextRunner.run((context) -> {
@@ -172,28 +217,43 @@ void withoutLenient() {
172217
}
173218

174219
@Test
175-
void withLenient() {
220+
void withLenientTrue() {
176221
this.contextRunner.withPropertyValues("spring.gson.lenient:true").run((context) -> {
177222
Gson gson = context.getBean(Gson.class);
178223
assertThat(gson).hasFieldOrPropertyWithValue("lenient", true);
179224
});
180225
}
181226

182227
@Test
183-
void withHtmlEscaping() {
228+
void withLenientFalse() {
229+
this.contextRunner.withPropertyValues("spring.gson.lenient:false").run((context) -> {
230+
Gson gson = context.getBean(Gson.class);
231+
assertThat(gson).hasFieldOrPropertyWithValue("lenient", false);
232+
});
233+
}
234+
235+
@Test
236+
void withoutDisableHtmlEscaping() {
184237
this.contextRunner.run((context) -> {
185238
Gson gson = context.getBean(Gson.class);
186239
assertThat(gson.htmlSafe()).isTrue();
187240
});
188241
}
189242

190243
@Test
191-
void withoutHtmlEscaping() {
244+
void withDisableHtmlEscapingTrue() {
192245
this.contextRunner.withPropertyValues("spring.gson.disable-html-escaping:true").run((context) -> {
193246
Gson gson = context.getBean(Gson.class);
194-
assertThat(gson.htmlSafe()).isFalse();
247+
assertThat(gson.htmlSafe()).isTrue();
195248
});
249+
}
196250

251+
@Test
252+
void withDisableHtmlEscapingFalse() {
253+
this.contextRunner.withPropertyValues("spring.gson.disable-html-escaping:false").run((context) -> {
254+
Gson gson = context.getBean(Gson.class);
255+
assertThat(gson.htmlSafe()).isFalse();
256+
});
197257
}
198258

199259
@Test

0 commit comments

Comments
 (0)