Skip to content

Commit

Permalink
Merge branch '3.1.x' into 3.2.x
Browse files Browse the repository at this point in the history
Closes gh-39524
  • Loading branch information
wilkinsona committed Feb 12, 2024
2 parents 4387b79 + 4319554 commit 5746886
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -78,17 +78,22 @@ public int getOrder() {
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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -59,7 +59,7 @@ void gsonRegistration() {
}

@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}");
Expand All @@ -68,14 +68,31 @@ void generateNonExecutableJson() {
}

@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);
assertThat(gson.toJson(new DataObject())).isEqualTo("{}");
});
}

@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) -> {
Expand All @@ -93,7 +110,7 @@ void serializeNullsFalse() {
}

@Test
void enableComplexMapKeySerialization() {
void enableComplexMapKeySerializationTrue() {
this.contextRunner.withPropertyValues("spring.gson.enable-complex-map-key-serialization:true")
.run((context) -> {
Gson gson = context.getBean(Gson.class);
Expand All @@ -103,6 +120,17 @@ void enableComplexMapKeySerialization() {
});
}

@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) -> {
Expand All @@ -113,14 +141,23 @@ void notDisableInnerClassSerialization() {
}

@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();
assertThat(gson.toJson(wrapperObject.new NestedObject())).isEqualTo("null");
});
}

@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)
Expand Down Expand Up @@ -156,13 +193,21 @@ void customGsonBuilder() {
}

@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) -> {
Expand All @@ -172,28 +217,43 @@ void withoutLenient() {
}

@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);
});
}

@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();
});
}

@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()).isFalse();
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
Expand Down

0 comments on commit 5746886

Please sign in to comment.