Deprecate Gson lenient property and introduce strictness replacement

Closes gh-41430
This commit is contained in:
Andy Wilkinson 2024-07-12 10:46:43 +01:00
parent 291fe282e4
commit 8676cc6256
3 changed files with 54 additions and 7 deletions

View File

@ -20,7 +20,6 @@ import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.Strictness;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -93,7 +92,7 @@ public class GsonAutoConfiguration {
map.from(properties::getLongSerializationPolicy).to(builder::setLongSerializationPolicy);
map.from(properties::getFieldNamingPolicy).to(builder::setFieldNamingPolicy);
map.from(properties::getPrettyPrinting).whenTrue().toCall(builder::setPrettyPrinting);
map.from(properties::getLenient).whenTrue().toCall(() -> builder.setStrictness(Strictness.LENIENT));
map.from(properties::getStrictness).to(builder::setStrictness);
map.from(properties::getDisableHtmlEscaping).whenTrue().toCall(builder::disableHtmlEscaping);
map.from(properties::getDateFormat).to(builder::setDateFormat);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,8 +19,10 @@ package org.springframework.boot.autoconfigure.gson;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.LongSerializationPolicy;
import com.google.gson.Strictness;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
/**
* Configuration properties to configure {@link Gson}.
@ -75,9 +77,10 @@ public class GsonProperties {
private Boolean prettyPrinting;
/**
* Whether to be lenient about parsing JSON that doesn't conform to RFC 4627.
* Sets how strictly the RFC 8259 specification will be enforced when reading and
* writing JSON.
*/
private Boolean lenient;
private Strictness strictness;
/**
* Whether to disable the escaping of HTML characters such as '<', '>', etc.
@ -153,12 +156,22 @@ public class GsonProperties {
this.prettyPrinting = prettyPrinting;
}
public Strictness getStrictness() {
return this.strictness;
}
public void setStrictness(Strictness strictness) {
this.strictness = strictness;
}
@Deprecated(since = "3.4.0", forRemoval = true)
@DeprecatedConfigurationProperty(replacement = "spring.gson.strictness", since = "3.4.0")
public Boolean getLenient() {
return this.lenient;
return (this.strictness != null) && (this.strictness == Strictness.LENIENT);
}
public void setLenient(Boolean lenient) {
this.lenient = lenient;
setStrictness((lenient != null && lenient) ? Strictness.LENIENT : Strictness.STRICT);
}
public Boolean getDisableHtmlEscaping() {

View File

@ -210,6 +210,7 @@ class GsonAutoConfigurationTests {
}
@Test
@Deprecated(since = "3.4.0", forRemoval = true)
void withoutLenient() {
this.contextRunner.run((context) -> {
Gson gson = context.getBean(Gson.class);
@ -218,6 +219,7 @@ class GsonAutoConfigurationTests {
}
@Test
@Deprecated(since = "3.4.0", forRemoval = true)
void withLenientTrue() {
this.contextRunner.withPropertyValues("spring.gson.lenient:true").run((context) -> {
Gson gson = context.getBean(Gson.class);
@ -226,13 +228,46 @@ class GsonAutoConfigurationTests {
}
@Test
@Deprecated(since = "3.4.0", forRemoval = true)
void withLenientFalse() {
this.contextRunner.withPropertyValues("spring.gson.lenient:false").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson).hasFieldOrPropertyWithValue("strictness", Strictness.STRICT);
});
}
@Test
void withoutStrictness() {
this.contextRunner.run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson).hasFieldOrPropertyWithValue("strictness", null);
});
}
@Test
void withStrictnessStrict() {
this.contextRunner.withPropertyValues("spring.gson.strictness:strict").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson).hasFieldOrPropertyWithValue("strictness", Strictness.STRICT);
});
}
@Test
void withStrictnessLegacyStrict() {
this.contextRunner.withPropertyValues("spring.gson.strictness:legacy-strict").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson).hasFieldOrPropertyWithValue("strictness", Strictness.LEGACY_STRICT);
});
}
@Test
void withStrictnessLenient() {
this.contextRunner.withPropertyValues("spring.gson.strictness:lenient").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson).hasFieldOrPropertyWithValue("strictness", Strictness.LENIENT);
});
}
@Test
void withoutDisableHtmlEscaping() {
this.contextRunner.run((context) -> {