Skip to content

Conversation

@passuied
Copy link
Contributor

@passuied passuied commented Oct 22, 2025

Fixes issue
#299

Description
Fixing intermittent error experienced with union types

Error: cannot encode textual map: value for \"policy_id\" does not match its schema: cannot encode textual union: cannot encode textual null: expected: Go nil; received: int64

Even though policy_id is defined as

 {
			 "default": null,
		      "name": "policy_id",
		      "type": [
		        "null",
		        "long"
		      ]
},

and encoded value is 140866783150163
or also

Error: cannot encode textual map: value for \"plans\" does not match its schema: cannot encode textual union: cannot encode textual array item 1; map[carrier_id:map[null:63919423854] hippospace:ellie_insurance-api-dev-feature-clone-car id:map[null:63919489391] name:Kaiser Senior practice_id:655364]: cannot encode textual map: value for \"carrier_id\" does not match its schema: cannot encode textual union: cannot encode textual null: expected: Go nil; received: int64.
  • Refactored sorting of allowed types in nativeAvroFromTextualJSON to avoid mutating shared state. Create a local copy for sorting to ensure thread safety and maintain integrity of codecInfo.

Signed-off-by: Patrick Assuied [email protected]

…12 where calling NativeFromBinary() then TextFromNative() on union types would sometime throw error such as:

```
Error: cannot encode textual map: value for \"policy_id\" does not match its schema: cannot encode textual union: cannot encode textual null: expected: Go nil; received: int64
```
Even though `policy_id` is defined as
```
 {
			 "default": null,
		      "name": "policy_id",
		      "type": [
		        "null",
		        "long"
		      ]
},
```
and encoded value is `140866783150163`
or also

```Error: cannot encode textual map: value for \"plans\" does not match its schema: cannot encode textual union: cannot encode textual array item 1; map[carrier_id:map[null:63919423854] hippospace:ellie_insurance-api-dev-feature-clone-car id:map[null:63919489391] name:Kaiser Senior practice_id:655364]: cannot encode textual map: value for \"carrier_id\" does not match its schema: cannot encode textual union: cannot encode textual null: expected: Go nil; received: int64.
```

- Refactored sorting of allowed types in nativeAvroFromTextualJSON to avoid mutating shared state. Create a local copy for sorting to ensure thread safety and maintain integrity of codecInfo.

Signed-off-by: Patrick Assuied <[email protected]>
union.go Outdated
// 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

@passuied passuied force-pushed the bug/intermittent-mutation-shared-state-issue branch from da1388e to 46a0328 Compare October 22, 2025 04:49
@passuied passuied changed the title Fixing intermittent error experienced with uniion types since upgrading > 2.12 Fixing intermittent error experienced with union types since upgrading > 2.12 Oct 22, 2025
@passuied
Copy link
Contributor Author

@mihaitodor and @rockwotj this issue is blocking us. Any way escalate review and approval?
Thanks in advance

@passuied passuied changed the title Fixing intermittent error experienced with union types since upgrading > 2.12 Fixing intermittent error experienced with union types Oct 22, 2025
@rockwotj rockwotj merged commit 78091ec into linkedin:master Oct 22, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants