Skip to content

Commit

Permalink
fix(x/tx): add an option to encode maps using amino json (ref #23513) (
Browse files Browse the repository at this point in the history
…#23539)

Co-authored-by: Alex | Interchain Labs <[email protected]>
  • Loading branch information
julienrbrt and aljo242 authored Jan 28, 2025
1 parent fe5b695 commit aef1a2a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
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

0 comments on commit aef1a2a

Please sign in to comment.