Skip to content

Commit 2ceafa5

Browse files
committed
Add parser feature flag to turn on/off tagged array BigDecimal exponent negation, so bug FasterXML#139 fix can be toggled on/off to support backwards compatibility.
1 parent c02d615 commit 2ceafa5

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ public class CBORParser extends ParserMinimalBase
2424
*/
2525
public enum Feature implements FormatFeature
2626
{
27-
// BOGUS(false)
28-
;
27+
/**
28+
* Feature that can be eanbled to negate a typed array BigDecimal.
29+
*<p>
30+
* Default provides compatibility with {@code CBORGenerator} from < v2.10.
31+
*
32+
* @since 2.10.1.SNF
33+
*/
34+
CBOR_BIG_DECIMAL_EXPONENT_NEGATE(false),
35+
;
2936

3037
final boolean _defaultState;
3138
final int _mask;
@@ -73,6 +80,12 @@ private Feature(boolean defaultState) {
7380
* Codec used for data binding when (if) requested.
7481
*/
7582
protected ObjectCodec _objectCodec;
83+
84+
/**
85+
* Toggle backwards-compatibility with < v2.10 typed array
86+
* BigDecimal handling.
87+
*/
88+
final private boolean _bigDecimalNegate;
7689

7790
/*
7891
/**********************************************************
@@ -366,6 +379,8 @@ public CBORParser(IOContext ctxt, int parserFeatures, int cborFeatures,
366379

367380
_tokenInputRow = -1;
368381
_tokenInputCol = -1;
382+
383+
_bigDecimalNegate = Feature.CBOR_BIG_DECIMAL_EXPONENT_NEGATE.enabledIn(cborFeatures);
369384
}
370385

371386
@Override
@@ -884,7 +899,7 @@ protected JsonToken _handleTaggedArray(int tag, int len) throws IOException
884899
// 27-Nov-2019, tatu: As per [dataformats-binary#139] need to change sign here
885900
// if decoding CBOR generated < v2.10
886901
int exp = getIntValue();
887-
if ( exp < 0 ) {
902+
if ( _bigDecimalNegate ) {
888903
exp = -exp;
889904
}
890905

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/BigNumbersTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.math.BigInteger;
66

77
import com.fasterxml.jackson.core.JsonToken;
8+
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
89
import com.fasterxml.jackson.dataformat.cbor.CBORGenerator;
910
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
1011
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
@@ -20,10 +21,8 @@ public void testBigDecimalShort() throws Exception
2021
_testBigDecimal(BigDecimal.ONE.scaleByPowerOfTen(-1));
2122
_testBigDecimal(BigDecimal.ONE.scaleByPowerOfTen(-3));
2223
_testBigDecimal(BigDecimal.ONE.scaleByPowerOfTen(-100));
23-
/* Disabled for SNF patch
2424
_testBigDecimal(BigDecimal.ONE.scaleByPowerOfTen(3));
2525
_testBigDecimal(BigDecimal.ONE.scaleByPowerOfTen(137));
26-
*/
2726

2827
_testBigDecimal(new BigDecimal("0.01"));
2928
_testBigDecimal(new BigDecimal("0.33"));
@@ -56,7 +55,9 @@ private void _testBigDecimal(BigDecimal expValue) throws Exception
5655
byte[] b = sourceBytes.toByteArray();
5756

5857
// but verify that the original content can be parsed
59-
CBORParser parser = cborParser(b);
58+
final CBORFactory f = new CBORFactory();
59+
f.enable(CBORParser.Feature.CBOR_BIG_DECIMAL_EXPONENT_NEGATE);
60+
CBORParser parser = cborParser(f, b);
6061
assertToken(JsonToken.START_OBJECT, parser.nextToken());
6162
assertToken(JsonToken.FIELD_NAME, parser.nextToken());
6263
assertEquals("a", parser.getCurrentName());

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/parse/ParserNumbersTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ public void testBigDecimalType() throws IOException {
335335
generator.close();
336336

337337
final byte[] b = out.toByteArray();
338-
try (CBORParser parser = cborParser(b)) {
338+
CBORFactory f = new CBORFactory();
339+
f.enable(CBORParser.Feature.CBOR_BIG_DECIMAL_EXPONENT_NEGATE);
340+
try (CBORParser parser = cborParser(f, b)) {
339341
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
340342
assertEquals(NumberType.BIG_DECIMAL, parser.getNumberType());
341343
assertEquals(NR, parser.getDecimalValue());
@@ -354,7 +356,9 @@ public void testBigDecimalType2() throws IOException {
354356
0x21, // int -- -2
355357
0x19, 0x6a, (byte) 0xb3 // int 27315
356358
};
357-
try (CBORParser parser = cborParser(spec)) {
359+
CBORFactory f = new CBORFactory();
360+
f.enable(CBORParser.Feature.CBOR_BIG_DECIMAL_EXPONENT_NEGATE);
361+
try (CBORParser parser = cborParser(f, spec)) {
358362
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
359363
assertEquals(NumberType.BIG_DECIMAL, parser.getNumberType());
360364
assertEquals(new BigDecimal("273.15"), parser.getDecimalValue());

0 commit comments

Comments
 (0)