Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CosmosDB state store handling of nulls #1974

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion state/azure/cosmosdb/cosmosdb.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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