Deprecate Gson lenient property and introduce strictness replacement
Closes gh-41430
This commit is contained in:
parent
291fe282e4
commit
8676cc6256
|
|
@ -20,7 +20,6 @@ import java.util.List;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.Strictness;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
|
@ -93,7 +92,7 @@ public class GsonAutoConfiguration {
|
||||||
map.from(properties::getLongSerializationPolicy).to(builder::setLongSerializationPolicy);
|
map.from(properties::getLongSerializationPolicy).to(builder::setLongSerializationPolicy);
|
||||||
map.from(properties::getFieldNamingPolicy).to(builder::setFieldNamingPolicy);
|
map.from(properties::getFieldNamingPolicy).to(builder::setFieldNamingPolicy);
|
||||||
map.from(properties::getPrettyPrinting).whenTrue().toCall(builder::setPrettyPrinting);
|
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::getDisableHtmlEscaping).whenTrue().toCall(builder::disableHtmlEscaping);
|
||||||
map.from(properties::getDateFormat).to(builder::setDateFormat);
|
map.from(properties::getDateFormat).to(builder::setDateFormat);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.FieldNamingPolicy;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.LongSerializationPolicy;
|
import com.google.gson.LongSerializationPolicy;
|
||||||
|
import com.google.gson.Strictness;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration properties to configure {@link Gson}.
|
* Configuration properties to configure {@link Gson}.
|
||||||
|
|
@ -75,9 +77,10 @@ public class GsonProperties {
|
||||||
private Boolean prettyPrinting;
|
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.
|
* Whether to disable the escaping of HTML characters such as '<', '>', etc.
|
||||||
|
|
@ -153,12 +156,22 @@ public class GsonProperties {
|
||||||
this.prettyPrinting = prettyPrinting;
|
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() {
|
public Boolean getLenient() {
|
||||||
return this.lenient;
|
return (this.strictness != null) && (this.strictness == Strictness.LENIENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLenient(Boolean lenient) {
|
public void setLenient(Boolean lenient) {
|
||||||
this.lenient = lenient;
|
setStrictness((lenient != null && lenient) ? Strictness.LENIENT : Strictness.STRICT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean getDisableHtmlEscaping() {
|
public Boolean getDisableHtmlEscaping() {
|
||||||
|
|
|
||||||
|
|
@ -210,6 +210,7 @@ class GsonAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||||
void withoutLenient() {
|
void withoutLenient() {
|
||||||
this.contextRunner.run((context) -> {
|
this.contextRunner.run((context) -> {
|
||||||
Gson gson = context.getBean(Gson.class);
|
Gson gson = context.getBean(Gson.class);
|
||||||
|
|
@ -218,6 +219,7 @@ class GsonAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||||
void withLenientTrue() {
|
void withLenientTrue() {
|
||||||
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);
|
||||||
|
|
@ -226,13 +228,46 @@ class GsonAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||||
void withLenientFalse() {
|
void withLenientFalse() {
|
||||||
this.contextRunner.withPropertyValues("spring.gson.lenient:false").run((context) -> {
|
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);
|
Gson gson = context.getBean(Gson.class);
|
||||||
assertThat(gson).hasFieldOrPropertyWithValue("strictness", null);
|
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
|
@Test
|
||||||
void withoutDisableHtmlEscaping() {
|
void withoutDisableHtmlEscaping() {
|
||||||
this.contextRunner.run((context) -> {
|
this.contextRunner.run((context) -> {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue