Skip to content
Merged
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions union.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func buildCodecForTypeDescribedBySliceTwoWayJSON(st map[string]*Codec, enclosing
}

func checkAll(allowedTypes []string, cr *codecInfo, buf []byte) (interface{}, []byte, error) {
for _, name := range cr.allowedTypes {
for _, name := range allowedTypes {
if name == "null" {
// skip null since we know we already got type float64
continue
Expand Down Expand Up @@ -399,17 +399,22 @@ func nativeAvroFromTextualJSON(cr *codecInfo) func(buf []byte) (interface{}, []b
// int
// intNativeFromTextual

// sorted so it would be
// double, float, int, long
// that makes the priorities right by chance
sort.Strings(cr.allowedTypes)
// Sort a local copy to avoid mutating cr.allowedTypes shared state
Copy link
Contributor Author

@passuied passuied Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that union handling was mutating shared cr.allowedTypes and checkAll iterated over it instead of its parameter, which could cause wrong branch labels like "null" instead of "long".
As a result:

  • Use the allowedTypes parameter inside checkAll.
  • Stop sorting cr.allowedTypes and instead create a local copy to use inside checkAll

local := make([]string, len(allowedTypes))
copy(local, allowedTypes)
sort.Strings(local)
allowedTypes = local

case map[string]interface{}:

// try to decode it as a map
// because a map should fail faster than a record
// if that fails assume record and return it
sort.Strings(cr.allowedTypes)
// Sort a local copy to avoid mutating cr.allowedTypes shared state
local := make([]string, len(allowedTypes))
copy(local, allowedTypes)
sort.Strings(local)
allowedTypes = local
}

return checkAll(allowedTypes, cr, buf)
Expand Down