From fbcb1b91c2195c0823584282b127fa0cd55b5446 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 31 Jan 2025 16:36:15 -0800 Subject: [PATCH] Fix #561: avoid returning extra END_OBJECT at end root-level array value --- .../dataformat/protobuf/ProtobufParser.java | 16 ++++++++++------ .../protobuf/ReadPackedRepeatedTest.java | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/protobuf/src/main/java/tools/jackson/dataformat/protobuf/ProtobufParser.java b/protobuf/src/main/java/tools/jackson/dataformat/protobuf/ProtobufParser.java index 3013ead6c..654dbb0ae 100644 --- a/protobuf/src/main/java/tools/jackson/dataformat/protobuf/ProtobufParser.java +++ b/protobuf/src/main/java/tools/jackson/dataformat/protobuf/ProtobufParser.java @@ -492,21 +492,22 @@ protected void _releaseBuffers() @Override public JsonToken nextToken() throws JacksonException { + System.out.println("nextToken(): state="+_state+", ptr="+_inputPtr); JsonToken t = nextTokenX(); if (t == JsonToken.PROPERTY_NAME) { - System.out.print("Field name: "+currentName()); + System.out.print(" Field name: "+currentName()); } else if (t == JsonToken.VALUE_NUMBER_INT) { - System.out.print("Int: "+getIntValue()); + System.out.print(" Int: "+getIntValue()); } else if (t == JsonToken.VALUE_STRING) { - System.out.print("String: '"+getText()+"'"); + System.out.print(" String: '"+getString()+"'"); } else { - System.out.print("Next: "+t); + System.out.print(" Next: "+t); } System.out.println(" (state now: "+_state+", ptr "+_inputPtr+")"); return t; } - public JsonToken nextTokenX() throws JacksonException { + public JsonToken nextTokenX() throws JacksonException */ @Override @@ -1041,6 +1042,7 @@ public String nextName() throws JacksonException return name; } if (_state == STATE_MESSAGE_END) { + close(); // sets state to STATE_CLOSED _updateToken(JsonToken.END_OBJECT); return null; } @@ -1126,6 +1128,7 @@ public boolean nextName(SerializableString sstr) throws JacksonException return name.equals(sstr.getValue()); } if (_state == STATE_MESSAGE_END) { + close(); // sets state to STATE_CLOSED _updateToken(JsonToken.END_OBJECT); return false; } @@ -1215,6 +1218,7 @@ public int nextNameMatch(PropertyNameMatcher matcher) throws JacksonException return matcher.matchName(name); } if (_state == STATE_MESSAGE_END) { + close(); // sets state to STATE_CLOSED _updateToken(JsonToken.END_OBJECT); return PropertyNameMatcher.MATCH_END_OBJECT; } @@ -1236,7 +1240,7 @@ private int _nextNameMatch2(PropertyNameMatcher matcher) throws JacksonException /* /********************************************************************** - /* Public API, traversal, optimized: nextFieldName() + /* Public API, traversal, optimized: nextName() /********************************************************************** */ diff --git a/protobuf/src/test/java/tools/jackson/dataformat/protobuf/ReadPackedRepeatedTest.java b/protobuf/src/test/java/tools/jackson/dataformat/protobuf/ReadPackedRepeatedTest.java index 43851c398..dc4d79ae8 100644 --- a/protobuf/src/test/java/tools/jackson/dataformat/protobuf/ReadPackedRepeatedTest.java +++ b/protobuf/src/test/java/tools/jackson/dataformat/protobuf/ReadPackedRepeatedTest.java @@ -44,14 +44,14 @@ public void testSparse561() throws Exception + " repeated uint32 f = 1;\n" + "}"; - Map input = Map.of("f", new int[] { 1, 2 }); + Map input = Map.of("f", new int[] { 100, 200 }); ProtobufSchema schema = MAPPER.schemaLoader().load(new StringReader(SCHEMA_STR)); byte[] encoded = MAPPER.writer(schema).writeValueAsBytes(input); JsonNode t = MAPPER.readerFor(JsonNode.class).with(schema).readValue(encoded); assertEquals(2, t.get("f").size()); - assertEquals(1, t.get("f").get(0).asInt()); - assertEquals(2, t.get("f").get(1).asInt()); + assertEquals(100, t.get("f").get(0).asInt()); + assertEquals(200, t.get("f").get(1).asInt()); } }