From ccdbde57264be45a6a05137b59bb5bcb81b6f7b3 Mon Sep 17 00:00:00 2001 From: Dai MIKURUBE Date: Thu, 21 Sep 2023 17:48:38 +0900 Subject: [PATCH 1/2] Test callbacks for TokenFilter from FilteringParserDelegate --- .../core/filter/BasicParserFilteringTest.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) 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); + } } From 3100fea20d128ad46bd1e1029720a3d8f200e635 Mon Sep 17 00:00:00 2001 From: Dai MIKURUBE Date: Thu, 21 Sep 2023 17:50:44 +0900 Subject: [PATCH 2/2] Call the right filterFinishArray/Object from FilteringParserDelegate --- .../jackson/core/filter/FilteringParserDelegate.java | 6 +++++- 1 file changed, 5 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();