diff --git a/internal/datastore/crdb/crdb.go b/internal/datastore/crdb/crdb.go index 16f67030f1..80e4f83706 100644 --- a/internal/datastore/crdb/crdb.go +++ b/internal/datastore/crdb/crdb.go @@ -338,7 +338,7 @@ func (cds *crdbDatastore) ReadWriteTx( } // If metadata is to be attached, write that row now. - if config.Metadata != nil { + if config.Metadata != nil && len(config.Metadata.GetFields()) > 0 { expiresAt := time.Now().Add(cds.gcWindow).Add(1 * time.Minute) insertTransactionMetadata := psql.Insert(tableTransactionMetadata). Columns(colExpiresAt, colMetadata). diff --git a/internal/datastore/memdb/memdb.go b/internal/datastore/memdb/memdb.go index 9df7eb7aaf..9327c4e435 100644 --- a/internal/datastore/memdb/memdb.go +++ b/internal/datastore/memdb/memdb.go @@ -205,7 +205,7 @@ func (mdb *memdbDatastore) ReadWriteTx( tracked := common.NewChanges(revisions.TimestampIDKeyFunc, datastore.WatchRelationships|datastore.WatchSchema, 0) if tx != nil { - if config.Metadata != nil { + if config.Metadata != nil && len(config.Metadata.GetFields()) > 0 { if err := tracked.SetRevisionMetadata(ctx, newRevision, config.Metadata.AsMap()); err != nil { return datastore.NoRevision, err } diff --git a/internal/datastore/postgres/postgres.go b/internal/datastore/postgres/postgres.go index 1f37789521..69f018b299 100644 --- a/internal/datastore/postgres/postgres.go +++ b/internal/datastore/postgres/postgres.go @@ -438,7 +438,7 @@ func (pgd *pgDatastore) ReadWriteTx( err = wrapError(pgx.BeginTxFunc(ctx, pgd.writePool, pgx.TxOptions{IsoLevel: pgx.Serializable}, func(tx pgx.Tx) error { var err error var metadata map[string]any - if config.Metadata != nil { + if config.Metadata != nil && len(config.Metadata.GetFields()) > 0 { metadata = config.Metadata.AsMap() } diff --git a/internal/datastore/spanner/spanner.go b/internal/datastore/spanner/spanner.go index 8c80fceb35..1c60df2b4c 100644 --- a/internal/datastore/spanner/spanner.go +++ b/internal/datastore/spanner/spanner.go @@ -272,7 +272,7 @@ func (sd *spannerDatastore) ReadWriteTx(ctx context.Context, fn datastore.TxUser return &traceableRTX{delegate: spannerRWT} } - if config.Metadata != nil { + if config.Metadata != nil && len(config.Metadata.GetFields()) > 0 { // Insert the metadata into the transaction metadata table. mutation := spanner.Insert(tableTransactionMetadata, []string{colTransactionTag, colMetadata}, diff --git a/pkg/tuple/core_test.go b/pkg/tuple/core_test.go index f24e11156a..7f30a08878 100644 --- a/pkg/tuple/core_test.go +++ b/pkg/tuple/core_test.go @@ -1,6 +1,7 @@ package tuple import ( + "strings" "testing" "github.com/stretchr/testify/require" @@ -118,9 +119,13 @@ func TestCoreRelationToString(t *testing.T) { t.Run(tt.name, func(t *testing.T) { got, err := CoreRelationToString(tt.input) require.NoError(t, err) + + // Golang randomly injects spaces. + got = strings.ReplaceAll(got, " ", "") require.Equal(t, tt.expected, got) got = MustCoreRelationToString(tt.input) + got = strings.ReplaceAll(got, " ", "") require.Equal(t, tt.expected, got) }) } diff --git a/pkg/tuple/strings.go b/pkg/tuple/strings.go index 2a3ddccc78..974bb03916 100644 --- a/pkg/tuple/strings.go +++ b/pkg/tuple/strings.go @@ -4,6 +4,7 @@ import ( "sort" "strings" + "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/types/known/structpb" core "github.com/authzed/spicedb/pkg/proto/core/v1" @@ -131,7 +132,10 @@ func StringCaveatContext(context *structpb.Struct) (string, error) { return "", nil } - contextBytes, err := context.MarshalJSON() + contextBytes, err := protojson.MarshalOptions{ + Multiline: false, + Indent: "", + }.Marshal(context) if err != nil { return "", err }