From e2d2fc2193f167ddef407d58dbb31af9632061b4 Mon Sep 17 00:00:00 2001 From: Dai MIKURUBE Date: Sun, 24 Sep 2023 02:27:21 +0900 Subject: [PATCH] Call the right filterFinishArray/Object from FilteringParserDelegate (#1111) --- .../core/filter/FilteringParserDelegate.java | 6 +- .../core/filter/BasicParserFilteringTest.java | 84 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java b/src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java index 130e57a264..64276c5fd8 100644 --- a/src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java +++ b/src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java @@ -403,7 +403,11 @@ public JsonToken nextToken() throws IOException boolean returnEnd = _headContext.isStartHandled(); f = _headContext.getFilter(); if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) { - f.filterFinishArray(); + if (t.id() == JsonTokenId.ID_END_ARRAY) { + f.filterFinishArray(); + } else { + f.filterFinishObject(); + } } _headContext = _headContext.getParent(); _itemFilter = _headContext.getFilter(); diff --git a/src/test/java/com/fasterxml/jackson/core/filter/BasicParserFilteringTest.java b/src/test/java/com/fasterxml/jackson/core/filter/BasicParserFilteringTest.java index f0c074258f..a006a4b46f 100644 --- a/src/test/java/com/fasterxml/jackson/core/filter/BasicParserFilteringTest.java +++ b/src/test/java/com/fasterxml/jackson/core/filter/BasicParserFilteringTest.java @@ -95,6 +95,73 @@ public TokenFilter filterStartObject() { } } + static class LoggingFilter extends TokenFilter + { + final TokenFilter _parent; + final ArrayList _log; + + LoggingFilter(final TokenFilter parent) { + _parent = parent; + _log = new ArrayList<>(); + } + + LoggingFilter(final TokenFilter parent, final ArrayList log) { + _parent = parent; + _log = log; + } + + TokenFilter rewrap(final TokenFilter filter) { + if (filter == null) { + return null; + } else if (filter == TokenFilter.INCLUDE_ALL) { + return TokenFilter.INCLUDE_ALL; + } + return new LoggingFilter(filter, _log); + } + + @Override + public TokenFilter includeElement(final int index) { + _log.add("includeElement: " + index); + return rewrap(_parent.includeElement(index)); + } + + @Override + public TokenFilter includeProperty(final String name) { + _log.add("includeProperty: " + name); + return rewrap(_parent.includeProperty(name)); + } + + @Override + public TokenFilter filterStartObject() { + _log.add("filterStartObject"); + return rewrap(_parent.filterStartObject()); + } + + @Override + public TokenFilter filterStartArray() { + _log.add("filterStartArray"); + return rewrap(_parent.filterStartArray()); + } + + @Override + public void filterFinishObject() { + _log.add("filterFinishObject"); + _parent.filterFinishObject(); + } + + @Override + public void filterFinishArray() { + _log.add("filterFinishArray"); + _parent.filterFinishArray(); + } + + @Override + protected boolean _includeScalar() { + _log.add("_includeScalar"); + return _parent._includeScalar(); + } + } + static final TokenFilter INCLUDE_EMPTY_IF_NOT_FILTERED = new TokenFilter() { @Override public boolean includeEmptyArray(boolean contentsFiltered) { @@ -763,4 +830,21 @@ public void testExcludeLastArrayInsideArray() throws Exception { ); assertEquals(a2q("[[]]"), readAndWrite(JSON_F, p)); } + + public void testCallbacksFromFilteringParserDelegate1() throws Exception { + LoggingFilter loggingFilter = new LoggingFilter(new JsonPointerBasedFilter("/parent")); + + JsonParser p0 = JSON_F.createParser(a2q( + "{'parent':{'child':1}}")); + JsonParser p = new FilteringParserDelegate(p0, + loggingFilter, + Inclusion.ONLY_INCLUDE_ALL, + true + ); + assertEquals(a2q("{'child':1}"), readAndWrite(JSON_F, p)); + + assertEquals( + Arrays.asList("filterStartObject", "includeProperty: parent", "filterFinishObject"), + loggingFilter._log); + } }