Skip to content

Commit 8676cc6

Browse files
committed
Deprecate Gson lenient property and introduce strictness replacement
Closes gh-41430
1 parent 291fe28 commit 8676cc6

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.google.gson.Gson;
2222
import com.google.gson.GsonBuilder;
23-
import com.google.gson.Strictness;
2423

2524
import org.springframework.boot.autoconfigure.AutoConfiguration;
2625
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -93,7 +92,7 @@ public void customize(GsonBuilder builder) {
9392
map.from(properties::getLongSerializationPolicy).to(builder::setLongSerializationPolicy);
9493
map.from(properties::getFieldNamingPolicy).to(builder::setFieldNamingPolicy);
9594
map.from(properties::getPrettyPrinting).whenTrue().toCall(builder::setPrettyPrinting);
96-
map.from(properties::getLenient).whenTrue().toCall(() -> builder.setStrictness(Strictness.LENIENT));
95+
map.from(properties::getStrictness).to(builder::setStrictness);
9796
map.from(properties::getDisableHtmlEscaping).whenTrue().toCall(builder::disableHtmlEscaping);
9897
map.from(properties::getDateFormat).to(builder::setDateFormat);
9998
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 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.
@@ -19,8 +19,10 @@
1919
import com.google.gson.FieldNamingPolicy;
2020
import com.google.gson.Gson;
2121
import com.google.gson.LongSerializationPolicy;
22+
import com.google.gson.Strictness;
2223

2324
import org.springframework.boot.context.properties.ConfigurationProperties;
25+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
2426

2527
/**
2628
* Configuration properties to configure {@link Gson}.
@@ -75,9 +77,10 @@ public class GsonProperties {
7577
private Boolean prettyPrinting;
7678

7779
/**
78-
* Whether to be lenient about parsing JSON that doesn't conform to RFC 4627.
80+
* Sets how strictly the RFC 8259 specification will be enforced when reading and
81+
* writing JSON.
7982
*/
80-
private Boolean lenient;
83+
private Strictness strictness;
8184

8285
/**
8386
* Whether to disable the escaping of HTML characters such as '<', '>', etc.
@@ -153,12 +156,22 @@ public void setPrettyPrinting(Boolean prettyPrinting) {
153156
this.prettyPrinting = prettyPrinting;
154157
}
155158

159+
public Strictness getStrictness() {
160+
return this.strictness;
161+
}
162+
163+
public void setStrictness(Strictness strictness) {
164+
this.strictness = strictness;
165+
}
166+
167+
@Deprecated(since = "3.4.0", forRemoval = true)
168+
@DeprecatedConfigurationProperty(replacement = "spring.gson.strictness", since = "3.4.0")
156169
public Boolean getLenient() {
157-
return this.lenient;
170+
return (this.strictness != null) && (this.strictness == Strictness.LENIENT);
158171
}
159172

160173
public void setLenient(Boolean lenient) {
161-
this.lenient = lenient;
174+
setStrictness((lenient != null && lenient) ? Strictness.LENIENT : Strictness.STRICT);
162175
}
163176

164177
public Boolean getDisableHtmlEscaping() {

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ void withPrettyPrintingFalse() {
210210
}
211211

212212
@Test
213+
@Deprecated(since = "3.4.0", forRemoval = true)
213214
void withoutLenient() {
214215
this.contextRunner.run((context) -> {
215216
Gson gson = context.getBean(Gson.class);
@@ -218,6 +219,7 @@ void withoutLenient() {
218219
}
219220

220221
@Test
222+
@Deprecated(since = "3.4.0", forRemoval = true)
221223
void withLenientTrue() {
222224
this.contextRunner.withPropertyValues("spring.gson.lenient:true").run((context) -> {
223225
Gson gson = context.getBean(Gson.class);
@@ -226,13 +228,46 @@ void withLenientTrue() {
226228
}
227229

228230
@Test
231+
@Deprecated(since = "3.4.0", forRemoval = true)
229232
void withLenientFalse() {
230233
this.contextRunner.withPropertyValues("spring.gson.lenient:false").run((context) -> {
234+
Gson gson = context.getBean(Gson.class);
235+
assertThat(gson).hasFieldOrPropertyWithValue("strictness", Strictness.STRICT);
236+
});
237+
}
238+
239+
@Test
240+
void withoutStrictness() {
241+
this.contextRunner.run((context) -> {
231242
Gson gson = context.getBean(Gson.class);
232243
assertThat(gson).hasFieldOrPropertyWithValue("strictness", null);
233244
});
234245
}
235246

247+
@Test
248+
void withStrictnessStrict() {
249+
this.contextRunner.withPropertyValues("spring.gson.strictness:strict").run((context) -> {
250+
Gson gson = context.getBean(Gson.class);
251+
assertThat(gson).hasFieldOrPropertyWithValue("strictness", Strictness.STRICT);
252+
});
253+
}
254+
255+
@Test
256+
void withStrictnessLegacyStrict() {
257+
this.contextRunner.withPropertyValues("spring.gson.strictness:legacy-strict").run((context) -> {
258+
Gson gson = context.getBean(Gson.class);
259+
assertThat(gson).hasFieldOrPropertyWithValue("strictness", Strictness.LEGACY_STRICT);
260+
});
261+
}
262+
263+
@Test
264+
void withStrictnessLenient() {
265+
this.contextRunner.withPropertyValues("spring.gson.strictness:lenient").run((context) -> {
266+
Gson gson = context.getBean(Gson.class);
267+
assertThat(gson).hasFieldOrPropertyWithValue("strictness", Strictness.LENIENT);
268+
});
269+
}
270+
236271
@Test
237272
void withoutDisableHtmlEscaping() {
238273
this.contextRunner.run((context) -> {

0 commit comments

Comments
 (0)