Skip to content

Commit afcdbd9

Browse files
committed
Fix FasterXML#311 by wrapping IonExceptions in IonParser
1 parent b082f58 commit afcdbd9

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/IonParser.java

+29-6
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,13 @@ public String getText() throws IOException
265265
case FIELD_NAME:
266266
return getCurrentName();
267267
case VALUE_STRING:
268-
return _reader.stringValue();
268+
try {
269+
// stringValue() will throw an UnknownSymbolException if we're
270+
// trying to get the text for a symbol id that cannot be resolved.
271+
return _reader.stringValue();
272+
} catch (UnknownSymbolException e) {
273+
throw _constructError(e.getMessage(), e);
274+
}
269275
case VALUE_NUMBER_INT:
270276
case VALUE_NUMBER_FLOAT:
271277
Number n = getNumberValue();
@@ -512,7 +518,12 @@ public JsonToken nextToken() throws IOException
512518
}
513519

514520
// any more tokens in this scope?
515-
IonType type = _reader.next();
521+
IonType type = null;
522+
try {
523+
type = _reader.next();
524+
} catch (IonException e) {
525+
_wrapError(e.getMessage(), e);
526+
}
516527
if (type == null) {
517528
if (_parsingContext.inRoot()) { // EOF?
518529
close();
@@ -528,7 +539,13 @@ public JsonToken nextToken() throws IOException
528539
boolean inStruct = !_parsingContext.inRoot() && _reader.isInStruct();
529540
// (isInStruct can return true for the first value read if the reader
530541
// was created from an IonValue that has a parent container)
531-
_parsingContext.setCurrentName(inStruct ? _reader.getFieldName() : null);
542+
try {
543+
// getFieldName() can throw an UnknownSymbolException if the text of the
544+
// field name symbol cannot be resolved.
545+
_parsingContext.setCurrentName(inStruct ? _reader.getFieldName() : null);
546+
} catch (UnknownSymbolException e) {
547+
_wrapError(e.getMessage(), e);
548+
}
532549
JsonToken t = _tokenFromType(type);
533550
// and return either field name first
534551
if (inStruct) {
@@ -542,9 +559,15 @@ public JsonToken nextToken() throws IOException
542559
/**
543560
* @see com.fasterxml.jackson.dataformat.ion.polymorphism.IonAnnotationTypeDeserializer
544561
*/
545-
public String[] getTypeAnnotations() {
546-
// Per its spec, will not return null
547-
return _reader.getTypeAnnotations();
562+
public String[] getTypeAnnotations() throws JsonParseException {
563+
try {
564+
// Per its spec, will not return null
565+
return _reader.getTypeAnnotations();
566+
} catch (UnknownSymbolException e) {
567+
// IonReader.getTypeAnnotations() can throw an UnknownSymbolException if the text of any
568+
// the annotation symbols cannot be resolved.
569+
throw _constructError(e.getMessage(), e);
570+
}
548571
}
549572

550573
@Override

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/IonParserTest.java

+52-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
package com.fasterxml.jackson.dataformat.ion;
1616

17-
import com.fasterxml.jackson.core.JsonParser;
18-
import com.fasterxml.jackson.core.JsonToken;
19-
import com.fasterxml.jackson.core.StreamReadCapability;
17+
import com.fasterxml.jackson.core.*;
2018

2119
import org.junit.Assert;
2220

@@ -28,6 +26,7 @@
2826
import java.io.IOException;
2927
import java.math.BigDecimal;
3028
import java.math.BigInteger;
29+
import java.util.List;
3130
import org.junit.Test;
3231

3332
@SuppressWarnings("resource")
@@ -125,4 +124,54 @@ public void testParserCapabilities() throws Exception {
125124
}
126125
}
127126

127+
128+
@Test(expected = JsonParseException.class)
129+
public void testIonExceptionIsWrapped() throws IOException {
130+
IonFactory f = new IonFactory();
131+
try (IonParser parser = (IonParser) f.createParser("[ 12, true ) ]")) {
132+
Assert.assertEquals(JsonToken.START_ARRAY, parser.nextToken());
133+
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextValue());
134+
Assert.assertEquals(12, parser.getIntValue());
135+
Assert.assertEquals(JsonToken.VALUE_TRUE, parser.nextValue());
136+
parser.nextValue();
137+
}
138+
}
139+
140+
@Test(expected = JsonParseException.class)
141+
public void testUnknownSymbolExceptionForValueIsWrapped() throws IOException {
142+
IonFactory f = new IonFactory();
143+
try (IonParser parser = (IonParser) f.createParser("[ 12, $99 ]")) {
144+
Assert.assertEquals(JsonToken.START_ARRAY, parser.nextToken());
145+
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextValue());
146+
Assert.assertEquals(12, parser.getIntValue());
147+
Assert.assertEquals(JsonToken.VALUE_STRING, parser.nextValue());
148+
parser.getValueAsString(); // Should encounter unknown symbol and fail
149+
}
150+
}
151+
152+
@Test(expected = JsonParseException.class)
153+
public void testUnknownSymbolExceptionForFieldNameIsWrapped() throws IOException {
154+
IonFactory f = new IonFactory();
155+
try (IonParser parser = (IonParser) f.createParser("{ a: 1, $99: 2 }")) {
156+
Assert.assertEquals(JsonToken.START_OBJECT, parser.nextToken());
157+
Assert.assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
158+
Assert.assertEquals("a", parser.getCurrentName());
159+
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextValue());
160+
Assert.assertEquals(1, parser.getIntValue());
161+
parser.nextValue(); // Should encounter unknown symbol and fail
162+
}
163+
}
164+
165+
@Test(expected = JsonParseException.class)
166+
public void testUnknownSymbolExceptionForAnnotationIsWrapped() throws IOException {
167+
IonFactory f = new IonFactory();
168+
try (IonParser parser = (IonParser) f.createParser("{ a: $99::1 }")) {
169+
Assert.assertEquals(JsonToken.START_OBJECT, parser.nextToken());
170+
Assert.assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
171+
Assert.assertEquals("a", parser.getCurrentName());
172+
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextValue());
173+
Assert.assertEquals(1, parser.getIntValue());
174+
parser.getTypeAnnotations(); // Should encounter unknown symbol and fail
175+
}
176+
}
128177
}

0 commit comments

Comments
 (0)