Skip to content

Commit

Permalink
#1826 fix JsonFieldSelectorTrie logic for objects and parts of those …
Browse files Browse the repository at this point in the history
…objects both being included in selected fields
  • Loading branch information
thjaeckle committed Nov 29, 2023
1 parent 2f88899 commit 624923e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Expand Up @@ -100,7 +100,21 @@ private JsonFieldSelectorTrie addJsonKeyIterator(final Iterator<JsonKey> iterato
if (iterator.hasNext()) {
final JsonKey key = iterator.next();
children.compute(key, (theKey, theChild) -> {
final JsonFieldSelectorTrie child = theChild != null ? theChild : new JsonFieldSelectorTrie();
final JsonFieldSelectorTrie child;
if (theChild != null) {
if (iterator.hasNext()) {
if (theChild.children.isEmpty()) {
return theChild;
} else {
child = theChild;
}
} else {
child = new JsonFieldSelectorTrie();
}
} else {
child = new JsonFieldSelectorTrie();
}

return child.addJsonKeyIterator(iterator);
});
}
Expand Down
Expand Up @@ -48,6 +48,36 @@ public void trieWithNonemptyPaths() {
assertThat(getDescendantKeys(underTest, "c")).isEmpty();
}

@Test
public void trieWithJsonObjectAndCertainFieldInSameJsonObject() {
// given
// a field selector containing both, a "field" inside an "object" and the "object" itself
final JsonFieldSelector fieldSelector = JsonFieldSelector.newInstance("object/field", "object");

// when
final JsonFieldSelectorTrie underTest = JsonFieldSelectorTrie.of(fieldSelector);

// then
assertThat(getDescendantKeys(underTest)).isEqualTo(keySetOf("object"));
// ensure that not only the "field" is contained, but the complete "object" instead:
assertThat(getDescendantKeys(underTest, "object")).isEmpty();
}

@Test
public void trieWithJsonObjectAndCertainFieldInSameJsonObjectOrderSwapped() {
// given
// a field selector containing both, a "field" inside an "object" and the "object" itself
final JsonFieldSelector fieldSelectorReverse = JsonFieldSelector.newInstance("object", "object/field");

// when
final JsonFieldSelectorTrie underTestReverse = JsonFieldSelectorTrie.of(fieldSelectorReverse);

// then
assertThat(getDescendantKeys(underTestReverse)).isEqualTo(keySetOf("object"));
// ensure that not only the "field" is contained, but the complete "object" instead:
assertThat(getDescendantKeys(underTestReverse, "object")).isEmpty();
}

private static Set<JsonKey> keySetOf(final String... keyNames) {
return Arrays.stream(keyNames).map(JsonKey::of).collect(Collectors.toSet());
}
Expand Down

0 comments on commit 624923e

Please sign in to comment.