Skip to content

Commit 3c7875a

Browse files
committed
Fix #599: handle skipping of CBOR string refs (#627)
1 parent acc862a commit 3c7875a

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3199,6 +3199,25 @@ protected void _skipIncomplete() throws IOException
31993199
&& type != CBORConstants.MAJOR_TYPE_BYTES) {
32003200
_throwInternal();
32013201
}
3202+
3203+
// [dataformats-binary#599]: If we are in a stringref namespace, we need to
3204+
// actually read and store the string/bytes value instead of just skipping it,
3205+
// so that later string references can find it.
3206+
// The finish methods will determine if the value should be added to the
3207+
// reference table based on shouldReferenceString().
3208+
if (!_stringRefs.empty()) {
3209+
if (type == CBORConstants.MAJOR_TYPE_TEXT) {
3210+
// Need to actually read the text (which may add to stringRefs)
3211+
_finishTextToken(_typeByte);
3212+
} else {
3213+
// For bytes: decode length then read (which may add to stringRefs)
3214+
int len = _decodeExplicitLength(_typeByte & 0x1F);
3215+
_finishBytes(len);
3216+
}
3217+
return;
3218+
}
3219+
3220+
// Standard skip logic when not in stringref namespace
32023221
final int lowBits = _typeByte & 0x1F;
32033222
if (lowBits <= 23) {
32043223
if (lowBits > 0) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fasterxml.jackson.dataformat.cbor;
2+
3+
import java.util.Arrays;
4+
5+
import com.fasterxml.jackson.core.JsonToken;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
8+
import org.junit.Test;
9+
10+
public class StringRef599Test extends CBORTestBase
11+
{
12+
private final ObjectMapper VANILLA_MAPPER = cborMapper();
13+
private final ObjectMapper REF_MAPPER = cborMapper(cborFactoryBuilder()
14+
.enable(CBORGenerator.Feature.STRINGREF)
15+
.build());
16+
17+
// [dataformats-binary#599]
18+
@Test
19+
public void testDupsNoStringRef() throws Exception
20+
{
21+
_testStringRef(VANILLA_MAPPER);
22+
}
23+
24+
// [dataformats-binary#599]
25+
@Test
26+
public void testDupsWithStringRef() throws Exception
27+
{
28+
_testStringRef(REF_MAPPER);
29+
}
30+
31+
private void _testStringRef(ObjectMapper mapper) throws Exception
32+
{
33+
byte[] cbor = mapper.writeValueAsBytes(Arrays.asList("foo", "foo"));
34+
try (CBORParser p = cborParser(cbor)) {
35+
assertToken(JsonToken.START_ARRAY, p.nextToken());
36+
assertToken(JsonToken.VALUE_STRING, p.nextToken());
37+
// important! Skip String value
38+
assertToken(JsonToken.VALUE_STRING, p.nextToken());
39+
// equally important; try to check second instance
40+
assertEquals("foo", p.getText());
41+
assertToken(JsonToken.END_ARRAY, p.nextToken());
42+
}
43+
}
44+
}

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Active maintainers:
1616

1717
2.18.5 (not yet released)
1818

19+
#599: (cbor) Unable to deserialize stringref-enabled CBOR with ignored properties
20+
(reported by Yohei K)
1921
#623: (ion) Upgrade `ion-java` dep to 1.11.11 (from 1.11.10)
2022
(requested by @Shaurya0108)
2123

0 commit comments

Comments
 (0)