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(x/tx): add an option to encode maps using amino json (ref #23513) #23539

Merged
merged 3 commits into from
Jan 28, 2025
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
8 changes: 8 additions & 0 deletions x/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Since v0.13.0, x/tx follows Cosmos SDK semver: https://github.com/cosmos/cosmos-

## [Unreleased]

## [v1.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/x/tx/v1.1.0) - 2025-01-28

* [#23513](https://github.com/cosmos/cosmos-sdk/pull/23513), [#23539](https://github.com/cosmos/cosmos-sdk/pull/23539) Add map marshalling support (as option) to Amino JSON encoder.

## [v1.0.1](https://github.com/cosmos/cosmos-sdk/releases/tag/x/tx/v1.0.1) - 2025-01-17

* [#23324](https://github.com/cosmos/cosmos-sdk/pull/23324) Sign over unordered and timeout timestamp fields.
Expand All @@ -57,6 +61,10 @@ Identical to v1.0.0-alpha.3.
* [#21825](https://github.com/cosmos/cosmos-sdk/pull/21825) Fix decimal encoding and field ordering in Amino JSON encoder.
* [#21850](https://github.com/cosmos/cosmos-sdk/pull/21850) Support bytes field as signer.

## [v0.13.7](https://github.com/cosmos/cosmos-sdk/releases/tag/x/tx/v0.13.7) - 2025-01-28

* [#23513](https://github.com/cosmos/cosmos-sdk/pull/23513), [#23539](https://github.com/cosmos/cosmos-sdk/pull/23539) Add map marshalling support (as option) to Amino JSON encoder.

## [v0.13.6](https://github.com/cosmos/cosmos-sdk/releases/tag/x/tx/v0.13.6) - 2024-12-12

### Bug Fixes
Expand Down
13 changes: 12 additions & 1 deletion x/tx/signing/aminojson/json_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,18 @@ func (enc Encoder) marshal(value protoreflect.Value, fd protoreflect.FieldDescri

case protoreflect.Map:
if enc.marshalMappings {
return jsonMarshal(writer, value)
if !val.IsValid() {
_, err := io.WriteString(writer, "null")
return err
}

mapData := make(map[string]interface{})
val.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
mapData[k.String()] = v.Interface()
return true
})

return jsonMarshal(writer, mapData)
}
return errors.New("maps are not supported")

Expand Down
29 changes: 26 additions & 3 deletions x/tx/signing/aminojson/json_marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ func TestIndent(t *testing.T) {

bz, err := encoder.Marshal(msg)
require.NoError(t, err)
fmt.Println(string(bz))
require.Equal(t, `{
"type": "ABitOfEverything",
"value": {
Expand Down Expand Up @@ -325,7 +324,6 @@ func TestEnumAsString(t *testing.T) {

bz, err := encoder.Marshal(msg)
require.NoError(t, err)
fmt.Println(string(bz))
require.Equal(t, `{
"type": "ABitOfEverything",
"value": {
Expand Down Expand Up @@ -384,7 +382,6 @@ func TestAminoNameAsTypeURL(t *testing.T) {

bz, err := encoder.Marshal(msg)
require.NoError(t, err)
fmt.Println(string(bz))
require.Equal(t, `{
"type": "/testpb.ABitOfEverything",
"value": {
Expand Down Expand Up @@ -416,6 +413,32 @@ func TestAminoNameAsTypeURL(t *testing.T) {
}`, string(bz))
}

func TestMarshalMappings(t *testing.T) {
// valid
encoder := aminojson.NewEncoder(aminojson.EncoderOptions{Indent: " ", MarshalMappings: true})

msg := &testpb.WithAMap{
StrMap: map[string]string{
"foo": "bar",
"baz": "qux",
},
}

bz, err := encoder.Marshal(msg)
require.NoError(t, err)
require.Equal(t, `{
"str_map": {
"baz": "qux",
"foo": "bar"
}
}`, string(bz))

// invalid
encoder = aminojson.NewEncoder(aminojson.EncoderOptions{Indent: " ", MarshalMappings: false})
_, err = encoder.Marshal(msg)
require.Error(t, err)
}

func TestCustomBytesEncoder(t *testing.T) {
cdc := amino.NewCodec()
cdc.RegisterConcrete(&testpb.ABitOfEverything{}, "ABitOfEverything", nil)
Expand Down
Loading