diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java index d61e9a270e7..d8efe7a2b81 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java @@ -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); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonProperties.java index 14e94455388..bc32bd80a95 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonProperties.java @@ -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() { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java index 7481a5b5282..17d2fa3b9af 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java @@ -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) -> {