Skip to content

fix: copy typed fields to additional_properties in build_from_hash#3500

Draft
charlie-zhang109 wants to merge 1 commit into
masterfrom
fix/build-from-hash-typed-fields-in-ap
Draft

fix: copy typed fields to additional_properties in build_from_hash#3500
charlie-zhang109 wants to merge 1 commit into
masterfrom
fix/build-from-hash-typed-fields-in-ap

Conversation

@charlie-zhang109

Copy link
Copy Markdown
Contributor

Summary

  • build_from_hash (the path used by the API client deserializer) set typed fields via their accessors but never mirrored them into additional_properties
  • initialize() has an explicit per-field copy at the end, but build_from_hash calls new() with no args, so that copy runs against an empty attributes hash and copies nothing
  • For models with x-keep-typed-in-additional-properties, this meant typed fields were invisible in additionalProperties after a real API call, even though they appeared correctly when the model was constructed directly via new(attributes)

Root cause

BaseGenericModel#build_from_hash in both v1 and v2 model_base.rb routed unknown fields to AP but had no equivalent step for typed fields:

# before — typed fields silently absent from AP after API call
if self.respond_to?(:additional_properties)
  attributes.each_with_object({}) { |(k, v), h|
    if (!self.class.attribute_map.key?(k.to_sym))
      self.additional_properties[k.to_sym] = v  # only unknown fields
    end
  }
end

Fix

After deserialization, loop over openapi_types and copy each typed field that was present in the JSON into AP, matching what initialize() already does:

if self.respond_to?(:additional_properties)
  self.class.openapi_types.each_key do |key|
    json_key = self.class.attribute_map[key]
    next unless json_key && attributes.key?(json_key)
    self.additional_properties[json_key] = self.send(key)
  end
  attributes.each_with_object({}) { |(k, v), h|
    if (!self.class.attribute_map.key?(k.to_sym))
      self.additional_properties[k.to_sym] = v
    end
  }
end

Applied to both v1/model_base.rb and v2/model_base.rb.

Test plan

  • Run existing unit tests
  • Run UsageSummaryResponse integration tests (bundle exec ruby usage_summary_integration_test.rb -v) — the 3 "missing from additionalProperties" failures for custom_live_ts_sum, synthetics_check_calls_count_sum, and custom_live_ts_avg are resolved

🤖 Generated with Claude Code

build_from_hash (used by the API client deserializer) only routed
unknown fields to additional_properties; typed fields were set via
their accessors but never mirrored into the AP hash.  initialize()
already has the per-field copy, but build_from_hash calls new() with
no args so that copy ran against an empty attributes hash.

Add a loop in build_from_hash that copies each typed field that was
present in the JSON attributes into additional_properties after
deserialization, matching the behaviour of initialize().  Applied to
both v1 and v2 model_base.rb.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant