Skip to content

Commit 27c1215

Browse files
authored
Fix #1394: bad error for extra close marker (#1395)
1 parent 4bccb40 commit 27c1215

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ a pure JSON library.
2222
(contributed by Eduard G)
2323
#1361: `JsonPointer` parsing of '~' not followed by "0" or "1" unexpected
2424
(reported by @slz30)
25+
#1394: Wrong/misleading error for "extra" close token (`]` or `}`) when
26+
at root level
2527

2628
2.18.3 (not yet released)
2729

src/main/java/com/fasterxml/jackson/core/base/ParserBase.java

+14
Original file line numberDiff line numberDiff line change
@@ -1389,13 +1389,27 @@ protected char _handleUnrecognizedCharacterEscape(char ch) throws JsonProcessing
13891389

13901390
protected void _reportMismatchedEndMarker(int actCh, char expCh) throws JsonParseException {
13911391
final JsonReadContext ctxt = getParsingContext();
1392+
1393+
// 31-Jan-2025, tatu: [core#1394] Need to check case of no open scope
1394+
if (ctxt.inRoot()) {
1395+
_reportExtraEndMarker(actCh);
1396+
return;
1397+
}
13921398
final String msg = String.format(
13931399
"Unexpected close marker '%s': expected '%c' (for %s starting at %s)",
13941400
(char) actCh, expCh, ctxt.typeDesc(),
13951401
ctxt.startLocation(_contentReference()));
13961402
throw _constructReadException(msg, _currentLocationMinusOne());
13971403
}
13981404

1405+
// @since 2.19
1406+
protected void _reportExtraEndMarker(int actCh) throws JsonParseException {
1407+
final String scopeDesc = (actCh == '}') ? "Object" : "Array";
1408+
final String msg = String.format(
1409+
"Unexpected close marker '%s': no open %s to close", (char) actCh, scopeDesc);
1410+
throw _constructReadException(msg, _currentLocationMinusOne());
1411+
}
1412+
13991413
/**
14001414
* Method called to report a problem with unquoted control character.
14011415
* Note: it is possible to suppress some instances of

src/test/java/com/fasterxml/jackson/core/read/ParserScopeMatchingTest.java

+47
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.jupiter.api.Test;
77

88
import com.fasterxml.jackson.core.*;
9+
import com.fasterxml.jackson.core.exc.StreamReadException;
910

1011
import static org.junit.jupiter.api.Assertions.assertEquals;
1112
import static org.junit.jupiter.api.Assertions.fail;
@@ -188,4 +189,50 @@ private void _testMisssingColon(int mode) throws Exception
188189
}
189190
p.close();
190191
}
192+
193+
// [core#1394]
194+
@Test
195+
void extraEndArray() throws Exception
196+
{
197+
for (int mode : ALL_MODES) {
198+
_extraEndArray(mode);
199+
}
200+
}
201+
202+
public void _extraEndArray(int mode) throws Exception
203+
{
204+
try (JsonParser p = createParser(mode, "{ }]")) {
205+
assertToken(JsonToken.START_OBJECT, p.nextToken());
206+
assertToken(JsonToken.END_OBJECT, p.nextToken());
207+
try {
208+
p.nextToken();
209+
fail("Should have thrown an exception");
210+
} catch (StreamReadException e) {
211+
verifyException(e, "Unexpected close marker ']': no open Array");
212+
}
213+
}
214+
}
215+
216+
// [core#1394]
217+
@Test
218+
void extraEndObject() throws Exception
219+
{
220+
for (int mode : ALL_MODES) {
221+
_extraEndObject(mode);
222+
}
223+
}
224+
225+
public void _extraEndObject(int mode) throws Exception
226+
{
227+
try (JsonParser p = createParser(mode, "[ ]}")) {
228+
assertToken(JsonToken.START_ARRAY, p.nextToken());
229+
assertToken(JsonToken.END_ARRAY, p.nextToken());
230+
try {
231+
p.nextToken();
232+
fail("Should have thrown an exception");
233+
} catch (StreamReadException e) {
234+
verifyException(e, "Unexpected close marker '}': no open Object");
235+
}
236+
}
237+
}
191238
}

0 commit comments

Comments
 (0)