-
Notifications
You must be signed in to change notification settings - Fork 499
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 empty memo field in JSON when memo_type is text #635
Conversation
lechengfan
commented
Sep 5, 2018
•
edited
Loading
edited
- resolves JSON: Memo: An empty "memo" is not being generated for a transaction even if "memo_type" is "text" #149, where the "memo" field does not exist in the JSON response for a transaction whose memo_type is text and the memo happens to be an empty string.
- Fix is to define a custom JSON marshaler that converts the Memo field to be a string pointer. That way if the memo_type is none, it can be set to nil and the resulting JSON response doesn't have a memo field. In all other cases, it points to a string (possibly the empty string), which would cause the JSON response to include "memo" as a field.
- Add some test cases to ensure this behavior, and also make sure that the default marshalling and unmarshalling of Transactions still work.
For the sake of precision, maybe we can change the description to say 'points to a string (possibly the empty string), which would cause the JSON response to have "memo" as a field'. |
protocols/horizon/main.go
Outdated
@@ -372,6 +373,20 @@ type Transaction struct { | |||
ValidBefore string `json:"valid_before,omitempty"` | |||
} | |||
|
|||
func (t Transaction) MarshalJSON() ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a short comment explaining why we need this method.
I'm also wondering if we should change Memo
field tag on Transaction
to json:"-"
to avoid confusion.
protocols/horizon/main_test.go
Outdated
@@ -49,3 +50,50 @@ func TestAccount(t *testing.T) { | |||
}) | |||
}) | |||
} | |||
|
|||
func TestTransactionJSONMarshal(t *testing.T) { | |||
Convey("After marshalling and unmarshalling, the resulting struct should be the exact same as the original", t, func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not written down anywhere (👎 for us) but we want to remove github.com/smartystreets/goconvey
dependency so it's better to write all new tests using Test...
functions and assert
, mock
, etc. packages from github.com/stretchr/testify
.
PS: please use GH closing tags to automatically close issues. This should be used in PR description or one of the commits. |
protocols/horizon/main_test.go
Outdated
t.Errorf("MemoType is none, but memo is not nil") | ||
} | ||
}) | ||
// If a transaction's memo type is None, then memo field should be nil in JSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: s/should be nil in/should be omitted from/