hvnsweeting opened a new issue, #680:
URL: https://github.com/apache/avro-rs/issues/680
```rs
#[test]
fn test_round_trip_decimal_length_zero() -> TestResult {
use crate::decode::decode;
use crate::encode::encode;
use crate::encode::tests::success;
let schema = Schema::parse_str(
r#"{"type": "bytes", "logicalType": "decimal", "precision": 4,
"scale": 2}"#,
)?;
let expected = Value::Decimal(Decimal::from(Vec::<u8>::new()));
dbg!(&expected);
// 0x00 is the varint encoding of length 0 (zero-length bytes)
let mut buf: &[u8] = &[0x00];
let decoded = decode(&schema, &mut buf)?;
assert_eq!(decoded, expected);
// Re-encoding the decoded value should round-trip, but it fails
let mut buffer = Vec::new();
encode(&decoded, &schema, &mut buffer).expect(&success(&decoded,
&schema));
Ok(())
}
```
Output:
```
---- types::tests::test_round_trip_decimal_length_zero stdout ----
[avro/src/types.rs:1921:9] &expected = Decimal(
Decimal {
value: 0,
len: 0,
},
)
thread 'types::tests::test_round_trip_decimal_length_zero' (126727) panicked
at avro/src/types.rs:1929:48:
Value: Decimal(Decimal { value: 0, len: 0 })
should encode with schema:
Decimal(DecimalSchema { precision: 4, scale: 2, inner: Bytes }): Error {
details: Number of bytes requested for decimal sign extension 0 is less than
the number of bytes needed to decode 1 }
failures:
types::tests::test_round_trip_decimal_length_zero
```
For avro cpp, it treats this as bytes so it succeeds in both encode/decode.
Java failed to decode.
```
Exception in thread "main" java.lang.NumberFormatException: Zero length
BigInteger
at java.base/java.math.BigInteger.<init>(BigInteger.java:312)
at java.base/java.math.BigInteger.<init>(BigInteger.java:346)
at
org.apache.avro.Conversions$DecimalConversion.fromBytes(Conversions.java:111)
at DecimalTest.main(DecimalTest.java:19)
```
I'm happy to make PR to fix this but it first needs to decide which behavior
whether to:
- reject on decode (like java)
- accept and fix encode so it can round-trip (like cpp)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]