Preserve integer precision in AdditionalProperties via UseNumber#4095
Open
charlie-zhang109 wants to merge 2 commits into
Open
Preserve integer precision in AdditionalProperties via UseNumber#4095charlie-zhang109 wants to merge 2 commits into
charlie-zhang109 wants to merge 2 commits into
Conversation
JSON integers above 2^53 silently lost precision when the value landed
in a generated model's AdditionalProperties map. The generated
UnmarshalJSON populated that map via datadog.Unmarshal
(encoding/json.Unmarshal), which coerces JSON numbers into float64 for
interface{} targets.
Switch the additionalProperties decode in model_simple.j2 to a
UseNumber-enabled decoder via a new datadog.UnmarshalUseNumber helper
(added to encoding_json.j2 and goccy_gojson.j2 for both JSON backends).
Typed schema fields and existing decode paths are unaffected.
After regeneration, values in AdditionalProperties are json.Number
(string-backed) rather than float64. Callers can recover the original
integer via .(json.Number).Int64().
Fixes #4091.
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.
Fixes #4091.
Summary
The generated
UnmarshalJSONfor every model with anadditionalPropertiesblock populatesAdditionalProperties map[string]interface{}viadatadog.Unmarshal, which is a thin wrapper aroundencoding/json.Unmarshal. The standard Go decoder unmarshals JSON numbers intointerface{}targets asfloat64, so integers above 2^53 silently lose precision. Typed schema fields decode into concrete*int64targets and are not affected.This PR switches the
additionalPropertiesdecode to aUseNumber-enabled decoder via a newdatadog.UnmarshalUseNumberhelper.Changes
.generator/src/generator/templates/encoding_json.j2— addUnmarshalUseNumberto the stdlib JSON backend..generator/src/generator/templates/goccy_gojson.j2— addUnmarshalUseNumberto thegoccy_gojsonbackend..generator/src/generator/templates/model_simple.j2— one-line change: theadditionalPropertiesdecode now callsdatadog.UnmarshalUseNumberinstead ofdatadog.Unmarshal.Templates-only — happy to regenerate the rendered output into the same PR if reviewers prefer that.
Behavioral contract change
After regeneration, values in
AdditionalPropertieswill bejson.Number(string-backed) rather thanfloat64.val.(float64)will break.val.(json.Number).Int64()or.Float64()get correct precision.Suggest
changelog/Changed+backward-incompatiblelabels.Test plan
api/datadog/encoding_json.go,api/datadog/goccy_gojson.go, and the ~100+ affectedmodel_*.gofiles matches expectation.additionalPropertiesand assert exact recovery via.(json.Number).Int64(). Happy to write this — flag if you'd like it in this PR or as a follow-up.