|
| 1 | +package tools.jackson.dataformat.protobuf; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import tools.jackson.core.*; |
| 6 | + |
| 7 | +import tools.jackson.dataformat.protobuf.schema.ProtobufSchema; |
| 8 | +import tools.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test for issue #598: Protobuf parser state handling wrong for implicit close (END_OBJECT) |
| 14 | + */ |
| 15 | +public class ParserStateEndTest extends ProtobufTestBase |
| 16 | +{ |
| 17 | + private final ProtobufMapper MAPPER = newObjectMapper(); |
| 18 | + |
| 19 | + /** |
| 20 | + * Test that verifies the parser properly handles the end-of-input state. |
| 21 | + * The parser should NOT be closed when returning the final END_OBJECT token; |
| 22 | + * it should only be closed on the subsequent nextToken() call. |
| 23 | + */ |
| 24 | + @Test |
| 25 | + public void testParserStateAtEndObject() throws Exception |
| 26 | + { |
| 27 | + // Use a simple Point schema |
| 28 | + ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_POINT); |
| 29 | + |
| 30 | + // Create test data |
| 31 | + Point input = new Point(42, 13); |
| 32 | + byte[] bytes = MAPPER.writerFor(Point.class) |
| 33 | + .with(schema) |
| 34 | + .writeValueAsBytes(input); |
| 35 | + |
| 36 | + // Parse with streaming parser |
| 37 | + try (JsonParser p = MAPPER.reader() |
| 38 | + .with(schema) |
| 39 | + .createParser(bytes)) { |
| 40 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 41 | + |
| 42 | + // First field: "x" |
| 43 | + assertToken(JsonToken.PROPERTY_NAME, p.nextToken()); |
| 44 | + assertEquals("x", p.currentName()); |
| 45 | + |
| 46 | + assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); |
| 47 | + assertEquals(42, p.getIntValue()); |
| 48 | + |
| 49 | + // Second field: "y" |
| 50 | + assertToken(JsonToken.PROPERTY_NAME, p.nextToken()); |
| 51 | + assertEquals("y", p.currentName()); |
| 52 | + |
| 53 | + assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); |
| 54 | + assertEquals(13, p.getIntValue()); |
| 55 | + |
| 56 | + // END_OBJECT - This is the critical test |
| 57 | + assertToken(JsonToken.END_OBJECT, p.nextToken()); |
| 58 | + |
| 59 | + // THIS IS THE KEY ASSERTION: Parser should NOT be closed yet |
| 60 | + // The parser should only be closed on the NEXT nextToken() call |
| 61 | + assertFalse(p.isClosed(), |
| 62 | + "Parser should NOT be closed immediately after returning END_OBJECT"); |
| 63 | + |
| 64 | + // Verify currentToken() returns END_OBJECT (not null) |
| 65 | + assertEquals(JsonToken.END_OBJECT, p.currentToken(), |
| 66 | + "currentToken() should return END_OBJECT, not null"); |
| 67 | + |
| 68 | + // Now the next token should be null AND close the parser |
| 69 | + assertNull(p.nextToken(), "After END_OBJECT, nextToken() should return null"); |
| 70 | + assertTrue(p.isClosed(), "Parser should be closed after nextToken() returns null"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Similar test but using nextName() optimization |
| 76 | + */ |
| 77 | + @Test |
| 78 | + public void testParserStateAtEndObjectWithNextName() throws Exception |
| 79 | + { |
| 80 | + ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_POINT); |
| 81 | + |
| 82 | + Point input = new Point(42, 13); |
| 83 | + byte[] bytes = MAPPER.writerFor(Point.class) |
| 84 | + .with(schema) |
| 85 | + .writeValueAsBytes(input); |
| 86 | + |
| 87 | + try (JsonParser p = MAPPER.reader() |
| 88 | + .with(schema) |
| 89 | + .createParser(bytes)) { |
| 90 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 91 | + |
| 92 | + // Use nextName() for field access |
| 93 | + assertEquals("x", p.nextName()); |
| 94 | + assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); |
| 95 | + |
| 96 | + assertEquals("y", p.nextName()); |
| 97 | + assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); |
| 98 | + |
| 99 | + // Should get null from nextName() at end |
| 100 | + assertNull(p.nextName()); |
| 101 | + |
| 102 | + // Current token should be END_OBJECT |
| 103 | + assertEquals(JsonToken.END_OBJECT, p.currentToken(), |
| 104 | + "currentToken() should return END_OBJECT after nextName() returns null"); |
| 105 | + |
| 106 | + // Parser should NOT be closed yet |
| 107 | + assertFalse(p.isClosed(), |
| 108 | + "Parser should NOT be closed when currentToken is END_OBJECT"); |
| 109 | + |
| 110 | + // Next token should be null and close parser |
| 111 | + assertNull(p.nextToken()); |
| 112 | + assertTrue(p.isClosed()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Test with empty message (no fields) |
| 118 | + */ |
| 119 | + @Test |
| 120 | + public void testParserStateWithEmptyMessage() throws Exception |
| 121 | + { |
| 122 | + final String PROTOC_EMPTY = "message Empty {}\n"; |
| 123 | + ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_EMPTY); |
| 124 | + |
| 125 | + // Empty message = just START_OBJECT, END_OBJECT |
| 126 | + byte[] bytes = MAPPER.writer() |
| 127 | + .with(schema) |
| 128 | + .writeValueAsBytes(new Object()); |
| 129 | + |
| 130 | + try (JsonParser p = MAPPER.reader() |
| 131 | + .with(schema) |
| 132 | + .createParser(bytes)) { |
| 133 | + // START_OBJECT |
| 134 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 135 | + assertFalse(p.isClosed()); |
| 136 | + |
| 137 | + // END_OBJECT immediately |
| 138 | + assertToken(JsonToken.END_OBJECT, p.nextToken()); |
| 139 | + |
| 140 | + // Parser should NOT be closed yet |
| 141 | + assertFalse(p.isClosed(), |
| 142 | + "Parser should NOT be closed immediately after END_OBJECT"); |
| 143 | + assertEquals(JsonToken.END_OBJECT, p.currentToken()); |
| 144 | + |
| 145 | + // Next token closes |
| 146 | + assertNull(p.nextToken()); |
| 147 | + assertTrue(p.isClosed()); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments