Merge branch '3.2.x'

Closes gh-39525
This commit is contained in:
Andy Wilkinson 2024-02-12 10:22:56 +00:00
commit a27cedc3ef
2 changed files with 82 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 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.
@ -78,17 +78,22 @@ public class GsonAutoConfiguration {
public void customize(GsonBuilder builder) {
GsonProperties properties = this.properties;
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(properties::getGenerateNonExecutableJson).toCall(builder::generateNonExecutableJson);
map.from(properties::getGenerateNonExecutableJson).whenTrue().toCall(builder::generateNonExecutableJson);
map.from(properties::getExcludeFieldsWithoutExposeAnnotation)
.whenTrue()
.toCall(builder::excludeFieldsWithoutExposeAnnotation);
map.from(properties::getSerializeNulls).whenTrue().toCall(builder::serializeNulls);
map.from(properties::getEnableComplexMapKeySerialization).toCall(builder::enableComplexMapKeySerialization);
map.from(properties::getDisableInnerClassSerialization).toCall(builder::disableInnerClassSerialization);
map.from(properties::getEnableComplexMapKeySerialization)
.whenTrue()
.toCall(builder::enableComplexMapKeySerialization);
map.from(properties::getDisableInnerClassSerialization)
.whenTrue()
.toCall(builder::disableInnerClassSerialization);
map.from(properties::getLongSerializationPolicy).to(builder::setLongSerializationPolicy);
map.from(properties::getFieldNamingPolicy).to(builder::setFieldNamingPolicy);
map.from(properties::getPrettyPrinting).toCall(builder::setPrettyPrinting);
map.from(properties::getLenient).toCall(builder::setLenient);
map.from(properties::getDisableHtmlEscaping).toCall(builder::disableHtmlEscaping);
map.from(properties::getPrettyPrinting).whenTrue().toCall(builder::setPrettyPrinting);
map.from(properties::getLenient).whenTrue().toCall(builder::setLenient);
map.from(properties::getDisableHtmlEscaping).whenFalse().toCall(builder::disableHtmlEscaping);
map.from(properties::getDateFormat).to(builder::setDateFormat);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 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.
@ -59,7 +59,7 @@ class GsonAutoConfigurationTests {
}
@Test
void generateNonExecutableJson() {
void generateNonExecutableJsonTrue() {
this.contextRunner.withPropertyValues("spring.gson.generate-non-executable-json:true").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson.toJson(new DataObject())).isNotEqualTo("{\"data\":1}");
@ -68,7 +68,15 @@ class GsonAutoConfigurationTests {
}
@Test
void excludeFieldsWithoutExposeAnnotation() {
void generateNonExecutableJsonFalse() {
this.contextRunner.withPropertyValues("spring.gson.generate-non-executable-json:false").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson.toJson(new DataObject())).isEqualTo("{\"data\":1}");
});
}
@Test
void excludeFieldsWithoutExposeAnnotationTrue() {
this.contextRunner.withPropertyValues("spring.gson.exclude-fields-without-expose-annotation:true")
.run((context) -> {
Gson gson = context.getBean(Gson.class);
@ -76,6 +84,15 @@ class GsonAutoConfigurationTests {
});
}
@Test
void excludeFieldsWithoutExposeAnnotationFalse() {
this.contextRunner.withPropertyValues("spring.gson.exclude-fields-without-expose-annotation:false")
.run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson.toJson(new DataObject())).isEqualTo("{\"data\":1}");
});
}
@Test
void serializeNullsTrue() {
this.contextRunner.withPropertyValues("spring.gson.serialize-nulls:true").run((context) -> {
@ -93,7 +110,7 @@ class GsonAutoConfigurationTests {
}
@Test
void enableComplexMapKeySerialization() {
void enableComplexMapKeySerializationTrue() {
this.contextRunner.withPropertyValues("spring.gson.enable-complex-map-key-serialization:true")
.run((context) -> {
Gson gson = context.getBean(Gson.class);
@ -103,6 +120,17 @@ class GsonAutoConfigurationTests {
});
}
@Test
void enableComplexMapKeySerializationFalse() {
this.contextRunner.withPropertyValues("spring.gson.enable-complex-map-key-serialization:false")
.run((context) -> {
Gson gson = context.getBean(Gson.class);
Map<DataObject, String> original = new LinkedHashMap<>();
original.put(new DataObject(), "a");
assertThat(gson.toJson(original)).contains(DataObject.class.getName()).doesNotContain("\"data\":");
});
}
@Test
void notDisableInnerClassSerialization() {
this.contextRunner.run((context) -> {
@ -113,7 +141,7 @@ class GsonAutoConfigurationTests {
}
@Test
void disableInnerClassSerialization() {
void disableInnerClassSerializationTrue() {
this.contextRunner.withPropertyValues("spring.gson.disable-inner-class-serialization:true").run((context) -> {
Gson gson = context.getBean(Gson.class);
WrapperObject wrapperObject = new WrapperObject();
@ -121,6 +149,15 @@ class GsonAutoConfigurationTests {
});
}
@Test
void disableInnerClassSerializationFalse() {
this.contextRunner.withPropertyValues("spring.gson.disable-inner-class-serialization:false").run((context) -> {
Gson gson = context.getBean(Gson.class);
WrapperObject wrapperObject = new WrapperObject();
assertThat(gson.toJson(wrapperObject.new NestedObject())).isEqualTo("{\"data\":\"nested\"}");
});
}
@Test
void withLongSerializationPolicy() {
this.contextRunner.withPropertyValues("spring.gson.long-serialization-policy:" + LongSerializationPolicy.STRING)
@ -156,13 +193,21 @@ class GsonAutoConfigurationTests {
}
@Test
void withPrettyPrinting() {
void withPrettyPrintingTrue() {
this.contextRunner.withPropertyValues("spring.gson.pretty-printing:true").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson.toJson(new DataObject())).isEqualTo("{\n \"data\": 1\n}");
});
}
@Test
void withPrettyPrintingFalse() {
this.contextRunner.withPropertyValues("spring.gson.pretty-printing:false").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson.toJson(new DataObject())).isEqualTo("{\"data\":1}");
});
}
@Test
void withoutLenient() {
this.contextRunner.run((context) -> {
@ -172,7 +217,7 @@ class GsonAutoConfigurationTests {
}
@Test
void withLenient() {
void withLenientTrue() {
this.contextRunner.withPropertyValues("spring.gson.lenient:true").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson).hasFieldOrPropertyWithValue("lenient", true);
@ -180,7 +225,15 @@ class GsonAutoConfigurationTests {
}
@Test
void withHtmlEscaping() {
void withLenientFalse() {
this.contextRunner.withPropertyValues("spring.gson.lenient:false").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson).hasFieldOrPropertyWithValue("lenient", false);
});
}
@Test
void withoutDisableHtmlEscaping() {
this.contextRunner.run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson.htmlSafe()).isTrue();
@ -188,12 +241,19 @@ class GsonAutoConfigurationTests {
}
@Test
void withoutHtmlEscaping() {
void withDisableHtmlEscapingTrue() {
this.contextRunner.withPropertyValues("spring.gson.disable-html-escaping:true").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson.htmlSafe()).isTrue();
});
}
@Test
void withDisableHtmlEscapingFalse() {
this.contextRunner.withPropertyValues("spring.gson.disable-html-escaping:false").run((context) -> {
Gson gson = context.getBean(Gson.class);
assertThat(gson.htmlSafe()).isFalse();
});
}
@Test