mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Fix mapping of boolean properties to Gson's builder
Fixes gh-39504
This commit is contained in:
parent
8ffcfc9b77
commit
4319554f8a
@ -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");
|
* 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.
|
||||||
@ -78,17 +78,22 @@ public class GsonAutoConfiguration {
|
|||||||
public void customize(GsonBuilder builder) {
|
public void customize(GsonBuilder builder) {
|
||||||
GsonProperties properties = this.properties;
|
GsonProperties properties = this.properties;
|
||||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||||
map.from(properties::getGenerateNonExecutableJson).toCall(builder::generateNonExecutableJson);
|
map.from(properties::getGenerateNonExecutableJson).whenTrue().toCall(builder::generateNonExecutableJson);
|
||||||
map.from(properties::getExcludeFieldsWithoutExposeAnnotation)
|
map.from(properties::getExcludeFieldsWithoutExposeAnnotation)
|
||||||
|
.whenTrue()
|
||||||
.toCall(builder::excludeFieldsWithoutExposeAnnotation);
|
.toCall(builder::excludeFieldsWithoutExposeAnnotation);
|
||||||
map.from(properties::getSerializeNulls).whenTrue().toCall(builder::serializeNulls);
|
map.from(properties::getSerializeNulls).whenTrue().toCall(builder::serializeNulls);
|
||||||
map.from(properties::getEnableComplexMapKeySerialization).toCall(builder::enableComplexMapKeySerialization);
|
map.from(properties::getEnableComplexMapKeySerialization)
|
||||||
map.from(properties::getDisableInnerClassSerialization).toCall(builder::disableInnerClassSerialization);
|
.whenTrue()
|
||||||
|
.toCall(builder::enableComplexMapKeySerialization);
|
||||||
|
map.from(properties::getDisableInnerClassSerialization)
|
||||||
|
.whenTrue()
|
||||||
|
.toCall(builder::disableInnerClassSerialization);
|
||||||
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).toCall(builder::setPrettyPrinting);
|
map.from(properties::getPrettyPrinting).whenTrue().toCall(builder::setPrettyPrinting);
|
||||||
map.from(properties::getLenient).toCall(builder::setLenient);
|
map.from(properties::getLenient).whenTrue().toCall(builder::setLenient);
|
||||||
map.from(properties::getDisableHtmlEscaping).toCall(builder::disableHtmlEscaping);
|
map.from(properties::getDisableHtmlEscaping).whenFalse().toCall(builder::disableHtmlEscaping);
|
||||||
map.from(properties::getDateFormat).to(builder::setDateFormat);
|
map.from(properties::getDateFormat).to(builder::setDateFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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");
|
* 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.
|
||||||
@ -59,7 +59,7 @@ class GsonAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateNonExecutableJson() {
|
void generateNonExecutableJsonTrue() {
|
||||||
this.contextRunner.withPropertyValues("spring.gson.generate-non-executable-json:true").run((context) -> {
|
this.contextRunner.withPropertyValues("spring.gson.generate-non-executable-json:true").run((context) -> {
|
||||||
Gson gson = context.getBean(Gson.class);
|
Gson gson = context.getBean(Gson.class);
|
||||||
assertThat(gson.toJson(new DataObject())).isNotEqualTo("{\"data\":1}");
|
assertThat(gson.toJson(new DataObject())).isNotEqualTo("{\"data\":1}");
|
||||||
@ -68,7 +68,15 @@ class GsonAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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")
|
this.contextRunner.withPropertyValues("spring.gson.exclude-fields-without-expose-annotation:true")
|
||||||
.run((context) -> {
|
.run((context) -> {
|
||||||
Gson gson = context.getBean(Gson.class);
|
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
|
@Test
|
||||||
void serializeNullsTrue() {
|
void serializeNullsTrue() {
|
||||||
this.contextRunner.withPropertyValues("spring.gson.serialize-nulls:true").run((context) -> {
|
this.contextRunner.withPropertyValues("spring.gson.serialize-nulls:true").run((context) -> {
|
||||||
@ -93,7 +110,7 @@ class GsonAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void enableComplexMapKeySerialization() {
|
void enableComplexMapKeySerializationTrue() {
|
||||||
this.contextRunner.withPropertyValues("spring.gson.enable-complex-map-key-serialization:true")
|
this.contextRunner.withPropertyValues("spring.gson.enable-complex-map-key-serialization:true")
|
||||||
.run((context) -> {
|
.run((context) -> {
|
||||||
Gson gson = context.getBean(Gson.class);
|
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
|
@Test
|
||||||
void notDisableInnerClassSerialization() {
|
void notDisableInnerClassSerialization() {
|
||||||
this.contextRunner.run((context) -> {
|
this.contextRunner.run((context) -> {
|
||||||
@ -113,7 +141,7 @@ class GsonAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void disableInnerClassSerialization() {
|
void disableInnerClassSerializationTrue() {
|
||||||
this.contextRunner.withPropertyValues("spring.gson.disable-inner-class-serialization:true").run((context) -> {
|
this.contextRunner.withPropertyValues("spring.gson.disable-inner-class-serialization:true").run((context) -> {
|
||||||
Gson gson = context.getBean(Gson.class);
|
Gson gson = context.getBean(Gson.class);
|
||||||
WrapperObject wrapperObject = new WrapperObject();
|
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
|
@Test
|
||||||
void withLongSerializationPolicy() {
|
void withLongSerializationPolicy() {
|
||||||
this.contextRunner.withPropertyValues("spring.gson.long-serialization-policy:" + LongSerializationPolicy.STRING)
|
this.contextRunner.withPropertyValues("spring.gson.long-serialization-policy:" + LongSerializationPolicy.STRING)
|
||||||
@ -156,13 +193,21 @@ class GsonAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void withPrettyPrinting() {
|
void withPrettyPrintingTrue() {
|
||||||
this.contextRunner.withPropertyValues("spring.gson.pretty-printing:true").run((context) -> {
|
this.contextRunner.withPropertyValues("spring.gson.pretty-printing:true").run((context) -> {
|
||||||
Gson gson = context.getBean(Gson.class);
|
Gson gson = context.getBean(Gson.class);
|
||||||
assertThat(gson.toJson(new DataObject())).isEqualTo("{\n \"data\": 1\n}");
|
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
|
@Test
|
||||||
void withoutLenient() {
|
void withoutLenient() {
|
||||||
this.contextRunner.run((context) -> {
|
this.contextRunner.run((context) -> {
|
||||||
@ -172,7 +217,7 @@ class GsonAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void withLenient() {
|
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);
|
||||||
assertThat(gson).hasFieldOrPropertyWithValue("lenient", true);
|
assertThat(gson).hasFieldOrPropertyWithValue("lenient", true);
|
||||||
@ -180,7 +225,15 @@ class GsonAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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) -> {
|
this.contextRunner.run((context) -> {
|
||||||
Gson gson = context.getBean(Gson.class);
|
Gson gson = context.getBean(Gson.class);
|
||||||
assertThat(gson.htmlSafe()).isTrue();
|
assertThat(gson.htmlSafe()).isTrue();
|
||||||
@ -188,12 +241,19 @@ class GsonAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void withoutHtmlEscaping() {
|
void withDisableHtmlEscapingTrue() {
|
||||||
this.contextRunner.withPropertyValues("spring.gson.disable-html-escaping:true").run((context) -> {
|
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);
|
Gson gson = context.getBean(Gson.class);
|
||||||
assertThat(gson.htmlSafe()).isFalse();
|
assertThat(gson.htmlSafe()).isFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user