[
https://issues.apache.org/jira/browse/TINKERPOP-3277?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18098001#comment-18098001
]
ASF GitHub Bot commented on TINKERPOP-3277:
-------------------------------------------
GumpacG opened a new pull request, #3540:
URL: https://github.com/apache/tinkerpop/pull/3540
## Summary
`gremlin-go` serialized a zero `BigInteger` as a GraphBinary length of `0`
with no value
bytes. Java's `new BigInteger(new byte[0])` throws `NumberFormatException`,
so a Java server
could not read zero values sent by Go. The fix encodes zero as the canonical
single `0x00`
byte (length 1), matching Java's `BigInteger.toByteArray()`. `BigDecimal`
with a zero unscaled
value is fixed transitively (it serializes its unscaled value as a
`BigInteger`).
## Before vs After
| Value | Before | After |
|---|---|---|
| `BigInteger(0)` | `00 00 00 00` (Java rejects) | `00 00 00 01 00` |
| `BigDecimal` with unscaled `0` | `...00 00 00 00` (Java rejects) | `...00
00 00 01 00` |
### Example
```go
// Sending a zero-valued BigInteger property to a Java Gremlin Server
g.AddV("event").Property("count", big.NewInt(0)).Iterate()
```
Before: the Java server fails to deserialize the request
(`NumberFormatException: Zero length BigInteger`).
After: zero serializes to canonical bytes and is read correctly.
## Changes
- `gremlin-go/driver/graphBinary.go` - `getSignedBytesFromBigInt` returns
`[]byte{0}` for zero.
- `gremlin-go/driver/graphBinary_test.go` - added zero exact-bytes, zero
round-trip, and
zero-unscaled `BigDecimal` tests.
## Testing
Added `TestGraphBinaryV1` subtests for the zero cases.
Assisted-by: Kiro:claude-opus-4.8
> gremlin-go can't serialize zero in bigdecimal/biginteger
> --------------------------------------------------------
>
> Key: TINKERPOP-3277
> URL: https://issues.apache.org/jira/browse/TINKERPOP-3277
> Project: TinkerPop
> Issue Type: Bug
> Components: go
> Affects Versions: 3.7.6, 3.8.1
> Reporter: Ken Hu
> Priority: Minor
>
> Caveat: I haven't fully verified this, just something AI told me when I was
> writing the "model" tests for gremlin-go. Just putting here so it can be
> checked later.
> Gremlin-Go serializes zero BigInteger as a GraphBinary length of 0 with no
> bytes. Java expects a valid two’s-complement BigInteger byte array, where
> zero is encoded as length 1 with byte 0x00. When Java reads Go’s zero-length
> form, new BigInteger(new byte[0]) throws NumberFormatException, so Java
> servers cannot read zero BigInteger values sent by Go.
> The same affects BigDecimal when its unscaled value is zero, because that
> unscaled value is serialized as a BigInteger.
> {code:java}
> func TestZeroBigIntegerGraphBinaryV1Bytes(t *testing.T) {
> var buffer bytes.Buffer
> got, err := bigIntWriter(*big.NewInt(0), &buffer, nil)
> if err != nil {
> t.Fatal(err)
> }
> want := []byte{0x00, 0x00, 0x00, 0x01, 0x00}
> if !bytes.Equal(got, want) {
> t.Fatalf("zero BigInteger bytes = % x, want % x", got, want)
> }
> }
> Current output:
> 00 00 00 00
> Expected output:
> 00 00 00 01 00
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)