Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ private IonValue getIonValue() throws IOException {
_updateToken(JsonToken.VALUE_EMBEDDED_OBJECT);
IonList l = _system.newEmptyList();
IonWriter writer = _system.newWriter(l);
writer.writeValue(_reader);
_copyValue(writer);
IonValue v = l.get(0);
v.removeFromContainer();

Expand All @@ -622,6 +622,42 @@ private IonValue getIonValue() throws IOException {
return v;
}

// [dataformats-binary#358]: copy the current value one container level at
// a time, instead of `IonWriter.writeValue(IonReader)` for the whole subtree,
// so that nesting depth is validated as with `nextToken()`
private void _copyValue(IonWriter writer) throws IOException {
final int startDepth = _reader.getDepth();
final int baseDepth = _parsingContext.getNestingDepth() - startDepth;
IonType type = _reader.getType();
if (type == null) { // not positioned on a value
writer.writeValue(_reader);
return;
}
while (true) {
if (type == null) { // end of container
_reader.stepOut();
writer.stepOut();
if (_reader.getDepth() == startDepth) {
return;
}
} else if (IonType.isContainer(type) && !_reader.isNullValue()) {
if (writer.isInStruct()) {
writer.setFieldNameSymbol(_reader.getFieldNameSymbol());
}
writer.setTypeAnnotationSymbols(_reader.getTypeAnnotationSymbols());
writer.stepIn(type);
_reader.stepIn();
_streamReadConstraints.validateNestingDepth(baseDepth + _reader.getDepth());
} else {
writer.writeValue(_reader);
if (_reader.getDepth() == startDepth) {
return;
}
}
type = _reader.next();
}
}

@Override
public Object getEmbeddedObject() throws IOException {
if (_currToken == JsonToken.VALUE_EMBEDDED_OBJECT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
import com.fasterxml.jackson.databind.JsonMappingException;

import com.fasterxml.jackson.dataformat.ion.IonFactory;
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
import com.fasterxml.jackson.dataformat.ion.ionvalue.IonValueMapper;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -49,6 +52,15 @@ public class DeeplyNestedIonReadTest
CONSTRAINED_MAPPER = IonObjectMapper.builder(CONSTRAINED_FACTORY).build();
}

// For `IonValue` binding: needs `IonValueModule`
private final IonObjectMapper CONSTRAINED_VALUE_MAPPER = IonValueMapper.builder(
IonFactory.builderForTextualWriters()
.ionSystem(ION_SYSTEM)
.streamReadConstraints(StreamReadConstraints.builder()
.maxNestingDepth(MAX_DEPTH).build())
.build())
.build();

@Test
public void testDeepNestingListReadTextual() throws Exception {
_verifyTripsLimit(TEXT_MAPPER.writeValueAsString(_nestedLists(OVER_LIMIT)));
Expand Down Expand Up @@ -99,6 +111,25 @@ public void testDeepNestingReadFromIonValue() throws Exception {
_verifyTripsLimit(CONSTRAINED_FACTORY.createParser(value));
}

// [dataformats-binary#358]: `IonValue` binding copies the value with
// `getEmbeddedObject()`, not via `nextToken()`: must also be checked
@Test
public void testDeepNestingReadAsIonValue() throws Exception {
_verifyTripsLimit(TEXT_MAPPER.writeValueAsString(_nestedLists(OVER_LIMIT)), IonValue.class);
_verifyTripsLimit(TEXT_MAPPER.writeValueAsString(_nestedStructs(OVER_LIMIT)), IonValue.class);
_verifyTripsLimit(BINARY_MAPPER.writeValueAsBytes(_nestedLists(OVER_LIMIT)), IonValue.class);
}

@Test
public void testDeepNestingReadAsIonValueProperty() throws Exception {
// property value starts one level down, so it trips the limit one level sooner
_verifyTripsLimit(TEXT_MAPPER.writeValueAsString(
Collections.singletonMap("value", _nestedLists(MAX_DEPTH))), IonValueWrapper.class);
assertNotNull(CONSTRAINED_VALUE_MAPPER.readValue(TEXT_MAPPER.writeValueAsString(
Collections.singletonMap("value", _nestedLists(MAX_DEPTH - 1))),
IonValueWrapper.class).value);
}

@Test
public void testNestingAtLimitPasses() throws Exception {
// Documents exactly at the limit must still be readable, textual or binary
Expand All @@ -109,6 +140,30 @@ public void testNestingAtLimitPasses() throws Exception {

assertNotNull(CONSTRAINED_MAPPER.readTree(
TEXT_MAPPER.writeValueAsString(_nestedLists(MAX_DEPTH))));

final String doc = TEXT_MAPPER.writeValueAsString(_nestedStructs(MAX_DEPTH));
assertEquals(ION_SYSTEM.singleValue(doc), CONSTRAINED_VALUE_MAPPER.readValue(doc, IonValue.class));
}

private void _verifyTripsLimit(Object doc, Class<?> type) throws Exception {
try {
if (doc instanceof byte[]) {
CONSTRAINED_VALUE_MAPPER.readValue((byte[]) doc, type);
} else {
CONSTRAINED_VALUE_MAPPER.readValue((String) doc, type);
}
fail("Should not pass: nesting depth should exceed limit");
} catch (StreamConstraintsException e) {
_verifyException(e);
} catch (JsonMappingException e) {
// wrapped by databind when thrown for a POJO property
assertTrue(e.getCause() instanceof StreamConstraintsException, "Unexpected exception: "+e);
_verifyException((StreamConstraintsException) e.getCause());
}
}

static class IonValueWrapper {
public IonValue value;
}

private void _verifyTripsLimit(String doc) throws Exception {
Expand Down