Skip to content

Commit cf1f6b1

Browse files
committed
Fix #582
1 parent fedefb1 commit cf1f6b1

3 files changed

Lines changed: 156 additions & 2 deletions

File tree

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ JSON library.
1919
#580: FilteringGeneratorDelegate writeRawValue delegate to `writeRaw()`
2020
instead of `writeRawValue()`
2121
(reported by Arnaud R)
22+
#582: `FilteringGeneratorDelegate` bug when filtering arrays (in 2.10.1)
23+
(reported by alarribeau@github)
2224

2325
2.10.1 (09-Nov-2019)
2426

src/main/java/com/fasterxml/jackson/core/filter/FilteringGeneratorDelegate.java

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void writeStartArray() throws IOException
173173
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
174174
}
175175
}
176-
176+
177177
@Override
178178
public void writeStartArray(int size) throws IOException
179179
{
@@ -202,6 +202,64 @@ public void writeStartArray(int size) throws IOException
202202
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
203203
}
204204
}
205+
206+
@Override
207+
public void writeStartArray(Object forValue) throws IOException
208+
{
209+
if (_itemFilter == null) {
210+
_filterContext = _filterContext.createChildArrayContext(null, false);
211+
return;
212+
}
213+
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
214+
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
215+
delegate.writeStartArray(forValue);
216+
return;
217+
}
218+
_itemFilter = _filterContext.checkValue(_itemFilter);
219+
if (_itemFilter == null) {
220+
_filterContext = _filterContext.createChildArrayContext(null, false);
221+
return;
222+
}
223+
if (_itemFilter != TokenFilter.INCLUDE_ALL) {
224+
_itemFilter = _itemFilter.filterStartArray();
225+
}
226+
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
227+
_checkParentPath();
228+
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
229+
delegate.writeStartArray(forValue);
230+
} else {
231+
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
232+
}
233+
}
234+
235+
@Override
236+
public void writeStartArray(Object forValue, int size) throws IOException
237+
{
238+
if (_itemFilter == null) {
239+
_filterContext = _filterContext.createChildArrayContext(null, false);
240+
return;
241+
}
242+
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
243+
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
244+
delegate.writeStartArray(forValue, size);
245+
return;
246+
}
247+
_itemFilter = _filterContext.checkValue(_itemFilter);
248+
if (_itemFilter == null) {
249+
_filterContext = _filterContext.createChildArrayContext(null, false);
250+
return;
251+
}
252+
if (_itemFilter != TokenFilter.INCLUDE_ALL) {
253+
_itemFilter = _itemFilter.filterStartArray();
254+
}
255+
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
256+
_checkParentPath();
257+
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
258+
delegate.writeStartArray(forValue, size);
259+
} else {
260+
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
261+
}
262+
}
205263

206264
@Override
207265
public void writeEndArray() throws IOException
@@ -242,7 +300,7 @@ public void writeStartObject() throws IOException
242300
_filterContext = _filterContext.createChildObjectContext(f, false);
243301
}
244302
}
245-
303+
246304
@Override
247305
public void writeStartObject(Object forValue) throws IOException
248306
{
@@ -273,6 +331,36 @@ public void writeStartObject(Object forValue) throws IOException
273331
}
274332
}
275333

334+
@Override
335+
public void writeStartObject(Object forValue, int size) throws IOException
336+
{
337+
if (_itemFilter == null) {
338+
_filterContext = _filterContext.createChildObjectContext(_itemFilter, false);
339+
return;
340+
}
341+
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
342+
_filterContext = _filterContext.createChildObjectContext(_itemFilter, true);
343+
delegate.writeStartObject(forValue, size);
344+
return;
345+
}
346+
347+
TokenFilter f = _filterContext.checkValue(_itemFilter);
348+
if (f == null) {
349+
return;
350+
}
351+
352+
if (f != TokenFilter.INCLUDE_ALL) {
353+
f = f.filterStartObject();
354+
}
355+
if (f == TokenFilter.INCLUDE_ALL) {
356+
_checkParentPath();
357+
_filterContext = _filterContext.createChildObjectContext(f, true);
358+
delegate.writeStartObject(forValue, size);
359+
} else {
360+
_filterContext = _filterContext.createChildObjectContext(f, false);
361+
}
362+
}
363+
276364
@Override
277365
public void writeEndObject() throws IOException
278366
{
@@ -322,6 +410,12 @@ public void writeFieldName(SerializableString name) throws IOException
322410
}
323411
}
324412

413+
// 02-Dec-2019, tatu: Not sure what else to do... so use default impl from base class
414+
@Override
415+
public void writeFieldId(long id) throws IOException {
416+
writeFieldName(Long.toString(id));
417+
}
418+
325419
/*
326420
/**********************************************************
327421
/* Public API, write methods, text/String values

src/test/java/com/fasterxml/jackson/core/filter/JsonPointerGeneratorFilteringTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,62 @@ private void _assert(String input, String pathExpr, boolean includeParent, Strin
108108

109109
assertEquals(aposToQuotes(exp), w.toString());
110110
}
111+
112+
113+
// for [core#582]: regression wrt array filtering
114+
115+
public void testArrayFiltering582WithoutObject() throws IOException {
116+
_testArrayFiltering582(0);
117+
}
118+
119+
public void testArrayFiltering582WithoutSize() throws IOException {
120+
_testArrayFiltering582(1);
121+
}
122+
123+
public void testArrayFiltering582WithSize() throws IOException {
124+
_testArrayFiltering582(2);
125+
}
126+
127+
private void _testArrayFiltering582(int mode) throws IOException
128+
{
129+
StringWriter output = new StringWriter();
130+
JsonGenerator jg = JSON_F.createGenerator(output);
131+
132+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(jg,
133+
new JsonPointerBasedFilter("/noMatch"), true, true);
134+
final String[] stuff = new String[] { "foo", "bar" };
135+
136+
switch (mode) {
137+
case 0:
138+
gen.writeStartArray();
139+
break;
140+
case 1:
141+
gen.writeStartArray(stuff);
142+
break;
143+
default:
144+
gen.writeStartArray(stuff, stuff.length);
145+
}
146+
gen.writeString(stuff[0]);
147+
gen.writeString(stuff[1]);
148+
gen.writeEndArray();
149+
gen.close();
150+
jg.close();
151+
152+
assertEquals("", output.toString());
153+
}
154+
155+
/*
156+
// for [core#582]: regression wrt array filtering
157+
public void arrayFilterOut_workaroundFix() throws IOException {
158+
159+
StringWriter output = new StringWriter();
160+
JsonGenerator jg = new JsonFactory().createGenerator(output);
161+
162+
FilteringGeneratorDelegate filteringGeneratorDelegate = new FixedFilteringGeneratorDelegate(jg, new JsonPointerBasedFilter("/noMatch"), true, true);
163+
164+
new ObjectMapper().writeValue(filteringGeneratorDelegate, ARRAY_WRAPPER);
165+
166+
Assert.assertEquals("", output.toString());
167+
}
168+
*/
111169
}

0 commit comments

Comments
 (0)