Skip to content

[Breaking] Preserve integer precision in AdditionalProperties via UseNumber#4095

Merged
amaskara-dd merged 3 commits into
masterfrom
charlie.zhang/use-UseNumber
May 26, 2026
Merged

[Breaking] Preserve integer precision in AdditionalProperties via UseNumber#4095
amaskara-dd merged 3 commits into
masterfrom
charlie.zhang/use-UseNumber

Conversation

@charlie-zhang109

Copy link
Copy Markdown
Contributor

Fixes #4091.

Summary

The generated UnmarshalJSON for every model with an additionalProperties block populates AdditionalProperties map[string]interface{} via datadog.Unmarshal, which is a thin wrapper around encoding/json.Unmarshal. The standard Go decoder unmarshals JSON numbers into interface{} targets as float64, so integers above 2^53 silently lose precision. Typed schema fields decode into concrete *int64 targets and are not affected.

This PR switches the additionalProperties decode to a UseNumber-enabled decoder via a new datadog.UnmarshalUseNumber helper.

Changes

  • .generator/src/generator/templates/encoding_json.j2 — add UnmarshalUseNumber to the stdlib JSON backend.
  • .generator/src/generator/templates/goccy_gojson.j2 — add UnmarshalUseNumber to the goccy_gojson backend.
  • .generator/src/generator/templates/model_simple.j2 — one-line change: the additionalProperties decode now calls datadog.UnmarshalUseNumber instead of datadog.Unmarshal.

Templates-only — happy to regenerate the rendered output into the same PR if reviewers prefer that.

Behavioral contract change

After regeneration, values in AdditionalProperties will be json.Number (string-backed) rather than float64.

  • Callers using val.(float64) will break.
  • Callers using val.(json.Number).Int64() or .Float64() get correct precision.

Suggest changelog/Changed + backward-incompatible labels.

Test plan

  • Regenerate output via the apigentools flow and confirm the diff in api/datadog/encoding_json.go, api/datadog/goccy_gojson.go, and the ~100+ affected model_*.go files matches expectation.
  • CI on the regenerated PR (unit tests + cassette-based integration tests).
  • Targeted unit test: round-trip an integer > 2^53 through a model with additionalProperties and 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.

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.
@charlie-zhang109 charlie-zhang109 requested review from a team as code owners May 15, 2026 16:39
@charlie-zhang109 charlie-zhang109 added changelog/Changed Changed features results into a major version bump backward-incompatible Warns for backward incompatible changes labels May 15, 2026
@charlie-zhang109 charlie-zhang109 changed the title Preserve integer precision in AdditionalProperties via UseNumber [Breaking] Preserve integer precision in AdditionalProperties via UseNumber May 26, 2026
@charlie-zhang109

Copy link
Copy Markdown
Contributor Author

Confirmed with documentation team that

merging the PR, no documentation repo changes needed, [Breaking] entry will land in the next release's auto-generated changelog
image

…seNumber

# Conflicts:
#	api/datadogV1/model_monitor_formula_and_function_event_query_definition_compute.go
#	api/datadogV1/model_monitor_formula_and_function_event_query_group_by.go
#	api/datadogV1/model_monitor_thresholds.go
#	api/datadogV1/model_monthly_usage_attribution_values.go
#	api/datadogV1/model_query_value_widget_request.go
#	api/datadogV1/model_synthetics_test_request.go
#	api/datadogV1/model_usage_summary_date.go
#	api/datadogV1/model_usage_summary_date_org.go
#	api/datadogV1/model_usage_summary_response.go
#	api/datadogV2/model_create_degradation_request_data_attributes.go
#	api/datadogV2/model_create_degradation_request_data_attributes_updates_items.go
#	api/datadogV2/model_create_degradation_request_data_attributes_updates_items_components_affected_items.go
#	api/datadogV2/model_create_maintenance_request_data_attributes.go
#	api/datadogV2/model_create_maintenance_request_data_attributes_updates_items.go
#	api/datadogV2/model_create_maintenance_request_data_attributes_updates_items_components_affected_items.go
#	api/datadogV2/model_degradation_data_attributes_updates_items.go
#	api/datadogV2/model_llm_obs_annotated_interaction_item.go
#	api/datadogV2/model_llm_obs_annotation_queue_interaction_item.go
#	api/datadogV2/model_llm_obs_annotation_queue_interaction_response_item.go
#	api/datadogV2/model_observability_pipeline_amazon_s3_generic_destination.go
#	api/datadogV2/model_observability_pipeline_cloud_prem_destination.go
#	api/datadogV2/model_observability_pipeline_databricks_zerobus_destination.go
#	api/datadogV2/model_observability_pipeline_http_client_destination.go
#	api/datadogV2/model_observability_pipeline_http_server_source.go
#	api/datadogV2/model_observability_pipeline_kafka_destination.go
#	api/datadogV2/model_observability_pipeline_splunk_hec_source.go
#	api/datadogV2/model_org_group_data.go
#	api/datadogV2/model_org_group_list_response.go
#	api/datadogV2/model_org_group_relationships.go
#	api/datadogV2/model_security_monitoring_rule_impossible_travel_options.go
#	api/datadogV2/model_single_aggregated_connection_response_data_attributes.go
#	api/datadogV2/model_synthetics_downtime_time_slot_recurrence_request.go
#	api/datadogV2/model_synthetics_downtime_time_slot_recurrence_response.go
@amaskara-dd amaskara-dd merged commit 0434e58 into master May 26, 2026
26 checks passed
@amaskara-dd amaskara-dd deleted the charlie.zhang/use-UseNumber branch May 26, 2026 22:15
github-actions Bot pushed a commit to ConnectionMaster/datadog-api-client-go that referenced this pull request May 27, 2026
…Number (DataDog#4095)

* Preserve integer precision in AdditionalProperties via UseNumber

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 DataDog#4091.

* pre-commit fixes

---------

Co-authored-by: ci.datadog-api-spec <packages@datadoghq.com> 0434e58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backward-incompatible Warns for backward incompatible changes changelog/Changed Changed features results into a major version bump mergequeue-status: done

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Silent precision loss for JSON integers > 2^53 decoded into AdditionalProperties

2 participants