Skip to content

Commit

Permalink
Fix bug: Binary data is not properly encoded. (#957)
Browse files Browse the repository at this point in the history
* Fix bug: Binary data is not properly encoded.

* Amend the test for entity for using the correct encoded data.
  • Loading branch information
shizhMSFT authored and jhendrixMSFT committed Jan 11, 2018
1 parent ef92a8b commit 0183b70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions storage/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package storage

import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -246,7 +247,7 @@ func (e *Entity) MarshalJSON() ([]byte, error) {
switch t := v.(type) {
case []byte:
completeMap[typeKey] = OdataBinary
completeMap[k] = string(t)
completeMap[k] = t
case time.Time:
completeMap[typeKey] = OdataDateTime
completeMap[k] = t.Format(time.RFC3339Nano)
Expand Down Expand Up @@ -320,7 +321,10 @@ func (e *Entity) UnmarshalJSON(data []byte) error {
}
switch v {
case OdataBinary:
props[valueKey] = []byte(str)
props[valueKey], err = base64.StdEncoding.DecodeString(str)
if err != nil {
return fmt.Errorf(errorTemplate, err)
}
case OdataDateTime:
t, err := time.Parse("2006-01-02T15:04:05Z", str)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions storage/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func (s *StorageEntitySuite) TestExecuteQueryNextResults(c *chk.C) {
}

func (s *StorageEntitySuite) Test_entityMarshalJSON(c *chk.C) {
expected := `{"Address":"Mountain View","Age":23,"AmountDue":200.23,"Binary":"abcd","Binary@odata.type":"Edm.Binary","CustomerCode":"c9da6455-213d-42c9-9a79-3e9149a57833","CustomerCode@odata.type":"Edm.Guid","CustomerSince":"1992-12-20T21:55:00Z","CustomerSince@odata.type":"Edm.DateTime","IsActive":true,"NumberOfOrders":"255","NumberOfOrders@odata.type":"Edm.Int64","PartitionKey":"mypartitionkey","RowKey":"myrowkey"}`
expected := `{"Address":"Mountain View","Age":23,"AmountDue":200.23,"Binary":"YWJjZA==","Binary@odata.type":"Edm.Binary","CustomerCode":"c9da6455-213d-42c9-9a79-3e9149a57833","CustomerCode@odata.type":"Edm.Guid","CustomerSince":"1992-12-20T21:55:00Z","CustomerSince@odata.type":"Edm.DateTime","IsActive":true,"NumberOfOrders":"255","NumberOfOrders@odata.type":"Edm.Int64","PartitionKey":"mypartitionkey","RowKey":"myrowkey"}`

entity := Entity{
PartitionKey: "mypartitionkey",
Expand Down Expand Up @@ -504,7 +504,7 @@ func (s *StorageEntitySuite) Test_entityUnmarshalJSON(c *chk.C) {
"Age": 23,
"AmountDue":200.23,
"Binary@odata.type": "Edm.Binary",
"Binary": "abcd",
"Binary": "YWJjZA==",
"CustomerCode@odata.type":"Edm.Guid",
"CustomerCode":"c9da6455-213d-42c9-9a79-3e9149a57833",
"CustomerSince@odata.type":"Edm.DateTime",
Expand Down

0 comments on commit 0183b70

Please sign in to comment.