Skip to content

Commit 9efec42

Browse files
mergify[bot]maharifujulienrbrt
authored
fix(x/tx): sort with oneof field name in amino-json (backport #21782) (#22228)
Co-authored-by: Luis Carvalho <mail.lmcarvalho@gmail.com> Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent 955304a commit 9efec42

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

x/tx/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Since v0.13.0, x/tx follows Cosmos SDK semver: https://github.com/cosmos/cosmos-
3333

3434
## [Unreleased]
3535

36+
### Bug Fixes
37+
38+
* [#21782](https://github.com/cosmos/cosmos-sdk/pull/21782) Fix JSON attribute sort order on messages with oneof fields.
39+
3640
## [v0.13.5](https://github.com/cosmos/cosmos-sdk/releases/tag/x/tx/v0.13.5) - 2024-09-18
3741

3842
### Improvements

x/tx/signing/aminojson/json_marshal.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,11 @@ func (enc Encoder) marshal(value protoreflect.Value, fd protoreflect.FieldDescri
268268
}
269269

270270
type nameAndIndex struct {
271-
i int
272-
name string
271+
i int
272+
name string
273+
oneof protoreflect.OneofDescriptor
274+
oneofFieldName string
275+
oneofTypeName string
273276
}
274277

275278
func (enc Encoder) marshalMessage(msg protoreflect.Message, writer io.Writer) error {
@@ -300,14 +303,37 @@ func (enc Encoder) marshalMessage(msg protoreflect.Message, writer io.Writer) er
300303
indices := make([]*nameAndIndex, 0, fields.Len())
301304
for i := 0; i < fields.Len(); i++ {
302305
f := fields.Get(i)
303-
name := getAminoFieldName(f)
304-
indices = append(indices, &nameAndIndex{i: i, name: name})
306+
entry := &nameAndIndex{
307+
i: i,
308+
name: getAminoFieldName(f),
309+
oneof: f.ContainingOneof(),
310+
}
311+
312+
if entry.oneof != nil {
313+
var err error
314+
entry.oneofFieldName, entry.oneofTypeName, err = getOneOfNames(f)
315+
if err != nil {
316+
return err
317+
}
318+
}
319+
320+
indices = append(indices, entry)
305321
}
306322

307323
if shouldSortFields := !enc.doNotSortFields; shouldSortFields {
308324
sort.Slice(indices, func(i, j int) bool {
309325
ni, nj := indices[i], indices[j]
310-
return ni.name < nj.name
326+
niName, njName := ni.name, nj.name
327+
328+
if indices[i].oneof != nil {
329+
niName = indices[i].oneofFieldName
330+
}
331+
332+
if indices[j].oneof != nil {
333+
njName = indices[j].oneofFieldName
334+
}
335+
336+
return niName < njName
311337
})
312338
}
313339

@@ -316,22 +342,17 @@ func (enc Encoder) marshalMessage(msg protoreflect.Message, writer io.Writer) er
316342
name := ni.name
317343
f := fields.Get(i)
318344
v := msg.Get(f)
319-
oneof := f.ContainingOneof()
320-
isOneOf := oneof != nil
321-
oneofFieldName, oneofTypeName, err := getOneOfNames(f)
322-
if err != nil && isOneOf {
323-
return err
324-
}
345+
isOneOf := ni.oneof != nil
325346
writeNil := false
326347

327348
if !msg.Has(f) {
328349
// msg.WhichOneof(oneof) == nil: no field of the oneof has been set
329350
// !emptyOneOfWritten: we haven't written a null for this oneof yet (only write one null per empty oneof)
330351
switch {
331-
case isOneOf && msg.WhichOneof(oneof) == nil && !emptyOneOfWritten[oneofFieldName]:
332-
name = oneofFieldName
352+
case isOneOf && msg.WhichOneof(ni.oneof) == nil && !emptyOneOfWritten[ni.oneofFieldName]:
353+
name = ni.oneofFieldName
333354
writeNil = true
334-
emptyOneOfWritten[oneofFieldName] = true
355+
emptyOneOfWritten[ni.oneofFieldName] = true
335356
case omitEmpty(f):
336357
continue
337358
case f.Kind() == protoreflect.MessageKind &&
@@ -349,7 +370,7 @@ func (enc Encoder) marshalMessage(msg protoreflect.Message, writer io.Writer) er
349370
}
350371

351372
if isOneOf && !writeNil {
352-
_, err = fmt.Fprintf(writer, `"%s":{"type":"%s","value":{`, oneofFieldName, oneofTypeName)
373+
_, err = fmt.Fprintf(writer, `"%s":{"type":"%s","value":{`, ni.oneofFieldName, ni.oneofTypeName)
353374
if err != nil {
354375
return err
355376
}

0 commit comments

Comments
 (0)