feat(go): enforce required field presence in UnmarshalJSON - #6495
Draft
Fluf22 wants to merge 4 commits into
Draft
Conversation
Go's encoding/json performs case-insensitive field matching during Unmarshal. This causes data corruption when a customer record has a property whose name differs from a reserved field only by casing (e.g. 'ObjectID' vs 'objectID'): - If the customer's property is a string, it silently overwrites the reserved field value (data loss) - If it is a non-string type, Unmarshal fails with a type error (crash) Fix: rewrite the UnmarshalJSON template for models with additionalProperties to parse into a raw map[string]json.RawMessage first, then extract known fields by exact (case-sensitive) key match. Remaining keys are preserved as additional properties with their original casing and types. This affects all Go models with additionalProperties: Hit, RecommendHit, composition Hit, and others. Requires regeneration: yarn cli generate go
The type alias was only needed by the old UnmarshalJSON approach that used json.Unmarshal into the alias to avoid recursive unmarshalling. The new raw-map-based UnmarshalJSON no longer needs it.
Required fields now return an error when absent from the JSON payload instead of silently accepting the zero value. This aligns Go with Python, Swift, Kotlin, Dart, and Scala which all hard-fail on missing required fields.
Contributor
✔️ Code generated!
📊 Benchmark resultsBenchmarks performed on the method using a mock server, the results might not reflect the real-world performance.
|
Fluf22
force-pushed
the
fix/go-object-id
branch
2 times, most recently
from
June 11, 2026 20:42
ac413df to
f2446ff
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Stacked on top of #6494 (case-sensitive JSON key matching fix).
Problem
Go's
UnmarshalJSONfor models withadditionalPropertiessilently accepts missing required fields, leaving them at their zero value (e.g.""forobjectID). This is inconsistent with 5 of our other client languages.Current behavior across languages
ValidationErrorDecodingErrorthrownNoSuchElementExceptionnullnilundefined"",0, etc.)Solution
In the
UnmarshalJSONtemplate, required fields now return an error when their exact JSON key is absent:Optional fields remain unchanged — missing keys are silently accepted.
Affected models
~11 models with
additionalProperties+ required fields, including:search/Hit,composition/Hit,recommend/RecommendHit(objectID)search/SearchResponse(~35 fields, many required)search/SearchSynonymsResponse(hits,nbHits)search/DictionaryEntry(objectID)ingestion/PushTaskRecords(objectID)Checklist
templates/go/model_simple.mustacheyarn cli generate goyarn cli cts run go