From 9097a1a23d37b4e8595aef59d79387212e32994d Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 23 Jun 2020 16:11:49 +0300 Subject: [PATCH] smartcontract: update (Parameter).MarshalJSON method MarshalJSON should be defined on structure (not pointer), as we use structures to marshal parameters (e.g. in NotificationEvent and Invoke of RPC result package) and never use pointers for that purpose. Also added marshalling of nil array into `[]` instead of `null` to follow C# implementation. --- pkg/core/native/blocked_accounts_test.go | 11 +++++++++++ pkg/smartcontract/parameter.go | 8 ++++++-- pkg/smartcontract/parameter_test.go | 9 ++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pkg/core/native/blocked_accounts_test.go b/pkg/core/native/blocked_accounts_test.go index ac2a35a5a..76709395e 100644 --- a/pkg/core/native/blocked_accounts_test.go +++ b/pkg/core/native/blocked_accounts_test.go @@ -1,9 +1,11 @@ package native import ( + "encoding/json" "testing" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes" + "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/stretchr/testify/require" @@ -51,3 +53,12 @@ func TestToStackItem(t *testing.T) { actual = stackitem.NewArray([]stackitem.Item{}) require.Equal(t, expected.ToStackItem(), actual) } + +func TestMarshallJSON(t *testing.T) { + ba := &BlockedAccounts{} + p := smartcontract.ParameterFromStackItem(ba.ToStackItem(), make(map[stackitem.Item]bool)) + actual, err := json.Marshal(p) + require.NoError(t, err) + expected := `{"type":"Array","value":[]}` + require.Equal(t, expected, string(actual)) +} diff --git a/pkg/smartcontract/parameter.go b/pkg/smartcontract/parameter.go index e3ce46f1e..c4dfe7fc5 100644 --- a/pkg/smartcontract/parameter.go +++ b/pkg/smartcontract/parameter.go @@ -57,7 +57,7 @@ type rawParameter struct { } // MarshalJSON implements Marshaler interface. -func (p *Parameter) MarshalJSON() ([]byte, error) { +func (p Parameter) MarshalJSON() ([]byte, error) { var ( resultRawValue json.RawMessage resultErr error @@ -83,7 +83,11 @@ func (p *Parameter) MarshalJSON() ([]byte, error) { } case ArrayType: var value = p.Value.([]Parameter) - resultRawValue, resultErr = json.Marshal(value) + if value == nil { + resultRawValue, resultErr = json.Marshal([]Parameter{}) + } else { + resultRawValue, resultErr = json.Marshal(value) + } case MapType: ppair := p.Value.([]ParameterPair) resultRawValue, resultErr = json.Marshal(ppair) diff --git a/pkg/smartcontract/parameter_test.go b/pkg/smartcontract/parameter_test.go index a10a127a7..818181834 100644 --- a/pkg/smartcontract/parameter_test.go +++ b/pkg/smartcontract/parameter_test.go @@ -131,6 +131,13 @@ var marshalJSONTestCases = []struct { }, result: `{"type":"InteropInterface","value":null}`, }, + { + input: Parameter{ + Type: ArrayType, + Value: []Parameter{}, + }, + result: `{"type":"Array","value":[]}`, + }, } var marshalJSONErrorCases = []Parameter{ @@ -146,7 +153,7 @@ var marshalJSONErrorCases = []Parameter{ func TestParam_MarshalJSON(t *testing.T) { for _, tc := range marshalJSONTestCases { - res, err := json.Marshal(&tc.input) + res, err := json.Marshal(tc.input) assert.NoError(t, err) var actual, expected Parameter assert.NoError(t, json.Unmarshal(res, &actual))