Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance javadocs for JsonHelper#toJsonIgnoringPaths #1193

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/org/kiwiproject/json/JsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ public String toJsonFromKeyValuePairs(Object... kvPairs) {

/**
* Convert the given object to JSON, but ignoring (excluding) the given paths.
* <p>
* Note that if the input object is {@code null}, then the returned value is
* the string literal {@code "null"}. The reason is that a {@code null} object
* is represented as a {@link NullNode}, and its {@code toString} method returns
* the literal {@code "null"}.
*
* @param object the object to convert
* @param ignoredPaths the paths to ignore/exclude
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/kiwiproject/json/JsonHelperBasicsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ void shouldIgnoreNestedPaths() {
var workAddress = (Map<String, Object>) sanitizedBob.get("workAddress");
assertThat(workAddress).doesNotContainKeys("street1", "street2");
}

@Test
void shouldHandleNullInput_WhenIgnoringPaths() {
var json = jsonHelper.toJsonIgnoringPaths(null, "username", "password");

assertThat(json)
.describedAs("Null input should result in a literal \"null\" String value")
.isEqualTo("null");
}
}

@Nested
Expand Down Expand Up @@ -515,6 +524,17 @@ void shouldReturnEmptyOptional_WhenGivenNullOrEmptyInput(String value) {
void shouldReturnEmptyOptional_WhenGivenBlankInput(String value) {
assertThat(jsonHelper.toObjectOptional(value, Person.class)).isEmpty();
}

@Test
void shouldReturnDefaultObject_WhenGivenEmptyJson() {
assertThat(jsonHelper.toObjectOptional("{}", Person.class))
.describedAs("Return value should be the 'default' object")
.contains(new Person(null, null, 0));

assertThat(jsonHelper.toObjectOptional("{}", User.class))
.describedAs("Return value should be the 'default' object")
.contains(User.builder().luckyNumbers(null).build());
}
}

@Nested
Expand Down