Skip to content

Commit

Permalink
Fix CosmosDB state store handling of nulls (dapr#1974)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Verst <4535280+berndverst@users.noreply.github.com>
  • Loading branch information
berndverst committed Aug 18, 2022
1 parent 942b0d1 commit 10b1168
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
16 changes: 15 additions & 1 deletion state/azure/cosmosdb/cosmosdb.go
Expand Up @@ -249,7 +249,18 @@ func (c *StateStore) Get(req *state.GetRequest) (*state.GetResponse, error) {
}

if items[0].IsBinary {
bytes, _ := base64.StdEncoding.DecodeString(items[0].Value.(string))
if items[0].Value == nil {
return &state.GetResponse{
Data: make([]byte, 0),
ETag: ptr.String(items[0].Etag),
}, nil
}

bytes, decodeErr := base64.StdEncoding.DecodeString(items[0].Value.(string))
if decodeErr != nil {
c.logger.Warnf("CosmosDB state store Get request could not decode binary string: %v. Returning raw string instead.", decodeErr)
bytes = []byte(items[0].Value.(string))
}

return &state.GetResponse{
Data: bytes,
Expand Down Expand Up @@ -583,6 +594,9 @@ func (c *StateStore) findCollection() (*documentdb.Collection, error) {

func createUpsertItem(contentType string, req state.SetRequest, partitionKey string) (CosmosItem, error) {
byteArray, isBinary := req.Value.([]uint8)
if len(byteArray) == 0 {
isBinary = false
}

ttl, err := parseTTL(req.Metadata)
if err != nil {
Expand Down
91 changes: 91 additions & 0 deletions state/azure/cosmosdb/cosmosdb_test.go
Expand Up @@ -182,6 +182,97 @@ func TestCreateCosmosItem(t *testing.T) {

assert.Equal(t, "AQ==", m)
})

t.Run("create item with null data", func(t *testing.T) {
// Bytes are handled the same way, does not matter if is JSON or JPEG.
bytes, err := json.Marshal(nil)
assert.NoError(t, err)

req := state.SetRequest{
Key: "testKey",
Value: bytes,
}

item, err := createUpsertItem("application/json", req, partitionKey)
assert.NoError(t, err)
assert.Equal(t, partitionKey, item.PartitionKey)
assert.Equal(t, "testKey", item.ID)
assert.False(t, item.IsBinary)
assert.Nil(t, item.TTL)

// items need to be marshallable to JSON with encoding/json
b, err := json.Marshal(item)
assert.NoError(t, err)

j := map[string]interface{}{}
err = json.Unmarshal(b, &j)
assert.NoError(t, err)

assert.Nil(t, j["value"])
})

t.Run("create item with empty string data and JSON content type", func(t *testing.T) {
// Bytes are handled the same way, does not matter if is JSON or JPEG.
bytes := []byte("")

req := state.SetRequest{
Key: "testKey",
Value: bytes,
}

item, err := createUpsertItem("application/json", req, partitionKey)
assert.NoError(t, err)
assert.Equal(t, partitionKey, item.PartitionKey)
assert.Equal(t, "testKey", item.ID)
assert.False(t, item.IsBinary)
assert.Nil(t, item.TTL)

// items need to be marshallable to JSON with encoding/json
b, err := json.Marshal(item)
assert.NoError(t, err)

j := map[string]interface{}{}
err = json.Unmarshal(b, &j)
assert.NoError(t, err)

m, ok := (j["value"].(string))

assert.Truef(t, ok, "value should be a string")
assert.NotContains(t, j, "ttl")
assert.Equal(t, "", m)
})

t.Run("create item with empty string data and string content type", func(t *testing.T) {
// Bytes are handled the same way, does not matter if is JSON or JPEG.
bytes := []byte("")

req := state.SetRequest{
Key: "testKey",
Value: bytes,
}

item, err := createUpsertItem("text/plain", req, partitionKey)
assert.NoError(t, err)
assert.Equal(t, partitionKey, item.PartitionKey)
assert.Equal(t, "testKey", item.ID)
assert.False(t, item.IsBinary)
assert.Nil(t, item.TTL)

// items need to be marshallable to JSON with encoding/json
b, err := json.Marshal(item)
assert.NoError(t, err)

j := map[string]interface{}{}
err = json.Unmarshal(b, &j)
assert.NoError(t, err)

m, ok := (j["value"].(string))

assert.Truef(t, ok, "value should be a string")
assert.NotContains(t, j, "ttl")

assert.Equal(t, "", m)
})
}

func TestCreateCosmosItemWithTTL(t *testing.T) {
Expand Down

0 comments on commit 10b1168

Please sign in to comment.