arangodb / go-driver

The official ArangoDB go driver.
Apache License 2.0
340 stars 78 forks source link

Driver v2 with VPACK not able to deserialize byte slices #620

Open metsmarko opened 1 week ago

metsmarko commented 1 week ago

Driver version: 2.1.0 Arango version: 3.12.0 Arango startup mode: docker run -e ARANGO_ROOT_PASSWORD="" -p 8529:8529 arangodb OS: Ubuntu x86_64

Not able to deserialize byte slices (returns nil) when using Arango Go Driver V2 with VPACK content type. Content-type JSON works. Code to reproduce:

import (
    "context"
    "testing"

    "github.com/arangodb/go-driver/v2/arangodb"
    "github.com/arangodb/go-driver/v2/connection"
    "github.com/stretchr/testify/require"
)

type Doc struct {
    Data []byte
}

var (
    username = "root"
    password = ""
    endpoint = "http://localhost:8529"
    dbName   = "test_db"
    colName  = "test_collection"
)

func Test(t *testing.T) {
    ctx := context.Background()
    conn := connection.NewHttpConnection(connection.HttpConfiguration{
        Authentication: connection.NewBasicAuth(username, password),
        Endpoint:       connection.NewRoundRobinEndpoints([]string{connection.FixupEndpointURLScheme(endpoint)}),
        ContentType:    connection.ApplicationVPack, // connection.ApplicationJSON works
    })
    client := arangodb.NewClient(conn)

        // create DB and collection if needed
    db := openDB(t, ctx, client, dbName)
    collection := getCollection(t, ctx, db)

    docIn := Doc{
        Data: []byte("binary-data"),
    }
    meta, err := collection.CreateDocument(ctx, docIn)
    require.NoError(t, err)

    var docOut Doc
    _, err = collection.ReadDocument(ctx, meta.Key, &docOut)
    require.NoError(t, err)

    require.Equal(t, docIn, docOut) // fails
}

func getCollection(t *testing.T, ctx context.Context, db arangodb.Database) arangodb.Collection {
    exists, err := db.CollectionExists(ctx, colName)
    require.NoError(t, err)
    var collection arangodb.Collection
    if !exists {
        collection, err = db.CreateCollection(ctx, colName, &arangodb.CreateCollectionProperties{})
    } else {
        collection, err = db.Collection(ctx, colName)
    }
    require.NoError(t, err)
    return collection
}

func openDB(t *testing.T, ctx context.Context, c arangodb.Client, dbName string) arangodb.Database {
    exists, err := c.DatabaseExists(ctx, dbName)
    require.NoError(t, err)
    var database arangodb.Database
    if !exists {
        database, err = c.CreateDatabase(ctx, dbName, nil)
    } else {
        database, err = c.Database(ctx, dbName)
    }
    require.NoError(t, err)
    return database
}
jwierzbo commented 1 week ago

@metsmarko thanks for reporting that.

It fails if we use the[]byte type. The following example will work (beside the Data field):

type DocWithRev struct {
    Rev       string         `json:"_rev,omitempty"`
    Key       string         `json:"_key,omitempty"`
    Name      string         `json:"name"`
    Age       *int           `json:"age"`
    Countries map[string]int `json:"countries"`
    Data      []byte         // not works!
}

We will prepare the fix