From 4b4b33caedee81e103d72ad284ee5c0c9d17874a Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 13 Apr 2020 20:14:42 -0700 Subject: [PATCH] Fix #202 --- .../dataformat/protobuf/ProtobufParser.java | 27 ++++++++++--------- .../ReadUnknownFields202Test.java | 3 +-- release-notes/VERSION-2.x | 5 ++++ 3 files changed, 21 insertions(+), 14 deletions(-) rename protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/{failing => }/ReadUnknownFields202Test.java (97%) diff --git a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufParser.java b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufParser.java index ec52e170e..daa3ac0e0 100644 --- a/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufParser.java +++ b/protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufParser.java @@ -699,7 +699,7 @@ private boolean _checkEnd() throws IOException private JsonToken _handleRootKey(int tag) throws IOException { int wireType = (tag & 0x7); - int id = (tag >> 3); + final int id = (tag >> 3); ProtobufField f; if (_currentField != null) { @@ -997,11 +997,11 @@ public boolean nextFieldName(SerializableString sstr) throws IOException return false; } } - int tag = _decodeVInt(); + final int tag = _decodeVInt(); // inlined _handleRootKey() - int wireType = (tag & 0x7); - int id = (tag >> 3); + final int wireType = (tag & 0x7); + final int id = (tag >> 3); ProtobufField f = _findField(id); if (f == null) { @@ -1033,11 +1033,11 @@ public boolean nextFieldName(SerializableString sstr) throws IOException _currToken = JsonToken.END_OBJECT; return false; } - int tag = _decodeVInt(); + final int tag = _decodeVInt(); // inlined '_handleNestedKey()' - int wireType = (tag & 0x7); - int id = (tag >> 3); + final int wireType = (tag & 0x7); + final int id = (tag >> 3); ProtobufField f = _findField(id); if (f == null) { @@ -1078,11 +1078,11 @@ public String nextFieldName() throws IOException return null; } } - int tag = _decodeVInt(); + final int tag = _decodeVInt(); // inlined _handleRootKey() int wireType = (tag & 0x7); - int id = (tag >> 3); + final int id = (tag >> 3); ProtobufField f = _findField(id); if (f == null) { @@ -1090,6 +1090,8 @@ public String nextFieldName() throws IOException return null; } // sub-optimal as skip method already set it, but: + // [dataformats-binary#202]: need to reset after skipping + wireType = _currentField.wireType; } String name = _currentField.name; _parsingContext.setCurrentName(name); @@ -1115,11 +1117,11 @@ public String nextFieldName() throws IOException _currToken = JsonToken.END_OBJECT; return null; } - int tag = _decodeVInt(); + final int tag = _decodeVInt(); // inlined '_handleNestedKey()' int wireType = (tag & 0x7); - int id = (tag >> 3); + final int id = (tag >> 3); ProtobufField f = _findField(id); if (f == null) { @@ -1127,6 +1129,8 @@ public String nextFieldName() throws IOException return null; } // sub-optimal as skip method already set it, but: + // [dataformats-binary#202]: need to reset after skipping + wireType = _currentField.wireType; } final String name = _currentField.name; _parsingContext.setCurrentName(name); @@ -2111,7 +2115,6 @@ protected ByteArrayBuilder _getByteArrayBuilder() { return _byteArrayBuilder; } - @SuppressWarnings("deprecation") protected void _closeInput() throws IOException { if (_inputStream != null) { if (_ioContext.isResourceManaged() || isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)) { diff --git a/protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/failing/ReadUnknownFields202Test.java b/protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ReadUnknownFields202Test.java similarity index 97% rename from protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/failing/ReadUnknownFields202Test.java rename to protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ReadUnknownFields202Test.java index 283bd1512..684284e75 100644 --- a/protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/failing/ReadUnknownFields202Test.java +++ b/protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ReadUnknownFields202Test.java @@ -1,4 +1,4 @@ -package com.fasterxml.jackson.dataformat.protobuf.failing; +package com.fasterxml.jackson.dataformat.protobuf; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonParser; @@ -123,7 +123,6 @@ public void testV1toV0() throws Exception { TestMessageV0 messageV0 = MAPPER .readerFor(TestMessageV0.class) .with(schemaV0) - .with(JsonParser.Feature.IGNORE_UNDEFINED) .readValue(protobufData); assertEquals(messageV1.getId(), messageV0.getId()); diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index cad547c51..3bf729f9d 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -8,6 +8,11 @@ Project: jackson-datatypes-binaryModules: === Releases === ------------------------------------------------------------------------ +2.10.4 (not yet released) + +#202: Parsing a protobuf message doesn't properly skip unknown fields + (reported by dmitry-timin@github) + 2.10.3 (03-Mar-2020) No changes since 2.10.2