Skip to content

Commit

Permalink
Fix #582
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 3, 2019
1 parent fedefb1 commit cf1f6b1
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 2 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Expand Up @@ -19,6 +19,8 @@ JSON library.
#580: FilteringGeneratorDelegate writeRawValue delegate to `writeRaw()`
instead of `writeRawValue()`
(reported by Arnaud R)
#582: `FilteringGeneratorDelegate` bug when filtering arrays (in 2.10.1)
(reported by alarribeau@github)

2.10.1 (09-Nov-2019)

Expand Down
Expand Up @@ -173,7 +173,7 @@ public void writeStartArray() throws IOException
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
}
}

@Override
public void writeStartArray(int size) throws IOException
{
Expand Down Expand Up @@ -202,6 +202,64 @@ public void writeStartArray(int size) throws IOException
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
}
}

@Override
public void writeStartArray(Object forValue) throws IOException
{
if (_itemFilter == null) {
_filterContext = _filterContext.createChildArrayContext(null, false);
return;
}
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
delegate.writeStartArray(forValue);
return;
}
_itemFilter = _filterContext.checkValue(_itemFilter);
if (_itemFilter == null) {
_filterContext = _filterContext.createChildArrayContext(null, false);
return;
}
if (_itemFilter != TokenFilter.INCLUDE_ALL) {
_itemFilter = _itemFilter.filterStartArray();
}
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
_checkParentPath();
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
delegate.writeStartArray(forValue);
} else {
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
}
}

@Override
public void writeStartArray(Object forValue, int size) throws IOException
{
if (_itemFilter == null) {
_filterContext = _filterContext.createChildArrayContext(null, false);
return;
}
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
delegate.writeStartArray(forValue, size);
return;
}
_itemFilter = _filterContext.checkValue(_itemFilter);
if (_itemFilter == null) {
_filterContext = _filterContext.createChildArrayContext(null, false);
return;
}
if (_itemFilter != TokenFilter.INCLUDE_ALL) {
_itemFilter = _itemFilter.filterStartArray();
}
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
_checkParentPath();
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
delegate.writeStartArray(forValue, size);
} else {
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
}
}

@Override
public void writeEndArray() throws IOException
Expand Down Expand Up @@ -242,7 +300,7 @@ public void writeStartObject() throws IOException
_filterContext = _filterContext.createChildObjectContext(f, false);
}
}

@Override
public void writeStartObject(Object forValue) throws IOException
{
Expand Down Expand Up @@ -273,6 +331,36 @@ public void writeStartObject(Object forValue) throws IOException
}
}

@Override
public void writeStartObject(Object forValue, int size) throws IOException
{
if (_itemFilter == null) {
_filterContext = _filterContext.createChildObjectContext(_itemFilter, false);
return;
}
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
_filterContext = _filterContext.createChildObjectContext(_itemFilter, true);
delegate.writeStartObject(forValue, size);
return;
}

TokenFilter f = _filterContext.checkValue(_itemFilter);
if (f == null) {
return;
}

if (f != TokenFilter.INCLUDE_ALL) {
f = f.filterStartObject();
}
if (f == TokenFilter.INCLUDE_ALL) {
_checkParentPath();
_filterContext = _filterContext.createChildObjectContext(f, true);
delegate.writeStartObject(forValue, size);
} else {
_filterContext = _filterContext.createChildObjectContext(f, false);
}
}

@Override
public void writeEndObject() throws IOException
{
Expand Down Expand Up @@ -322,6 +410,12 @@ public void writeFieldName(SerializableString name) throws IOException
}
}

// 02-Dec-2019, tatu: Not sure what else to do... so use default impl from base class
@Override
public void writeFieldId(long id) throws IOException {
writeFieldName(Long.toString(id));
}

/*
/**********************************************************
/* Public API, write methods, text/String values
Expand Down
Expand Up @@ -108,4 +108,62 @@ private void _assert(String input, String pathExpr, boolean includeParent, Strin

assertEquals(aposToQuotes(exp), w.toString());
}


// for [core#582]: regression wrt array filtering

public void testArrayFiltering582WithoutObject() throws IOException {
_testArrayFiltering582(0);
}

public void testArrayFiltering582WithoutSize() throws IOException {
_testArrayFiltering582(1);
}

public void testArrayFiltering582WithSize() throws IOException {
_testArrayFiltering582(2);
}

private void _testArrayFiltering582(int mode) throws IOException
{
StringWriter output = new StringWriter();
JsonGenerator jg = JSON_F.createGenerator(output);

FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(jg,
new JsonPointerBasedFilter("/noMatch"), true, true);
final String[] stuff = new String[] { "foo", "bar" };

switch (mode) {
case 0:
gen.writeStartArray();
break;
case 1:
gen.writeStartArray(stuff);
break;
default:
gen.writeStartArray(stuff, stuff.length);
}
gen.writeString(stuff[0]);
gen.writeString(stuff[1]);
gen.writeEndArray();
gen.close();
jg.close();

assertEquals("", output.toString());
}

/*
// for [core#582]: regression wrt array filtering
public void arrayFilterOut_workaroundFix() throws IOException {
StringWriter output = new StringWriter();
JsonGenerator jg = new JsonFactory().createGenerator(output);
FilteringGeneratorDelegate filteringGeneratorDelegate = new FixedFilteringGeneratorDelegate(jg, new JsonPointerBasedFilter("/noMatch"), true, true);
new ObjectMapper().writeValue(filteringGeneratorDelegate, ARRAY_WRAPPER);
Assert.assertEquals("", output.toString());
}
*/
}

0 comments on commit cf1f6b1

Please sign in to comment.