diff --git a/.generator/src/generator/templates/model_generic.j2 b/.generator/src/generator/templates/model_generic.j2 index 8ebe035e1b7e..256a4c749ea3 100644 --- a/.generator/src/generator/templates/model_generic.j2 +++ b/.generator/src/generator/templates/model_generic.j2 @@ -108,6 +108,21 @@ {%- endif %} end +{%- if model.get("x-keep-typed-in-additional-properties") %} + + # Builds the object from hash, also mirroring typed fields into additional_properties. + # @param attributes [Hash] Model attributes in the form of hash + # @return [Object] Returns the model itself + # @!visibility private + def build_from_hash(attributes) + super + self.class.attribute_map.each_pair do |attr, json_key| + self.additional_properties[json_key] = self.send(attr) if attributes.key?(json_key) + end + self + end +{%- endif %} + {%- set ns = namespace(hasValidation=False) %} {%- for attr, definition in model.get("properties", {}).items() %} {%- set required = attr in model.get("required", []) %} diff --git a/lib/datadog_api_client/v1/models/usage_summary_date.rb b/lib/datadog_api_client/v1/models/usage_summary_date.rb index ceccbdc71fa8..1a1b006e6103 100644 --- a/lib/datadog_api_client/v1/models/usage_summary_date.rb +++ b/lib/datadog_api_client/v1/models/usage_summary_date.rb @@ -3110,6 +3110,18 @@ def initialize(attributes = {}) self.additional_properties[:'workflow_executions_usage_sum'] = self.workflow_executions_usage_sum if attributes.key?(:'workflow_executions_usage_sum') end + # Builds the object from hash, also mirroring typed fields into additional_properties. + # @param attributes [Hash] Model attributes in the form of hash + # @return [Object] Returns the model itself + # @!visibility private + def build_from_hash(attributes) + super + self.class.attribute_map.each_pair do |attr, json_key| + self.additional_properties[json_key] = self.send(attr) if attributes.key?(json_key) + end + self + end + # Returns the object in the form of hash, with additionalProperties support. # @return [Hash] Returns the object in the form of hash # @!visibility private diff --git a/lib/datadog_api_client/v1/models/usage_summary_date_org.rb b/lib/datadog_api_client/v1/models/usage_summary_date_org.rb index 5f94c9ef8123..d614768c93d2 100644 --- a/lib/datadog_api_client/v1/models/usage_summary_date_org.rb +++ b/lib/datadog_api_client/v1/models/usage_summary_date_org.rb @@ -3168,6 +3168,18 @@ def initialize(attributes = {}) self.additional_properties[:'workflow_executions_usage_sum'] = self.workflow_executions_usage_sum if attributes.key?(:'workflow_executions_usage_sum') end + # Builds the object from hash, also mirroring typed fields into additional_properties. + # @param attributes [Hash] Model attributes in the form of hash + # @return [Object] Returns the model itself + # @!visibility private + def build_from_hash(attributes) + super + self.class.attribute_map.each_pair do |attr, json_key| + self.additional_properties[json_key] = self.send(attr) if attributes.key?(json_key) + end + self + end + # Returns the object in the form of hash, with additionalProperties support. # @return [Hash] Returns the object in the form of hash # @!visibility private diff --git a/lib/datadog_api_client/v1/models/usage_summary_response.rb b/lib/datadog_api_client/v1/models/usage_summary_response.rb index 9db199de7b58..2699bbd38c86 100644 --- a/lib/datadog_api_client/v1/models/usage_summary_response.rb +++ b/lib/datadog_api_client/v1/models/usage_summary_response.rb @@ -3221,6 +3221,18 @@ def initialize(attributes = {}) self.additional_properties[:'workflow_executions_usage_agg_sum'] = self.workflow_executions_usage_agg_sum if attributes.key?(:'workflow_executions_usage_agg_sum') end + # Builds the object from hash, also mirroring typed fields into additional_properties. + # @param attributes [Hash] Model attributes in the form of hash + # @return [Object] Returns the model itself + # @!visibility private + def build_from_hash(attributes) + super + self.class.attribute_map.each_pair do |attr, json_key| + self.additional_properties[json_key] = self.send(attr) if attributes.key?(json_key) + end + self + end + # Returns the object in the form of hash, with additionalProperties support. # @return [Hash] Returns the object in the form of hash # @!visibility private diff --git a/spec/v1/deserialization_spec.rb b/spec/v1/deserialization_spec.rb index d27493e9f81a..d3f7abeb5200 100644 --- a/spec/v1/deserialization_spec.rb +++ b/spec/v1/deserialization_spec.rb @@ -94,4 +94,27 @@ expect(data.data.attributes.destination.data[:type]).to eq "A non existent destination" end end + + describe 'build_from_hash typed fields in additional_properties' do + it 'should mirror typed fields into additional_properties when x-keep-typed-in-additional-properties is set' do + data = DatadogAPIClient::V1::UsageSummaryResponse.build_from_hash({ + agent_host_top99p_sum: 42, + apm_host_top99p_sum: 99 + }) + + expect(data).to be_a DatadogAPIClient::V1::UsageSummaryResponse + expect(data.agent_host_top99p_sum).to eq 42 + expect(data.additional_properties[:'agent_host_top99p_sum']).to eq 42 + expect(data.additional_properties[:'apm_host_top99p_sum']).to eq 99 + end + + it 'should not include typed fields absent from the JSON in additional_properties' do + data = DatadogAPIClient::V1::UsageSummaryResponse.build_from_hash({ + agent_host_top99p_sum: 7 + }) + + expect(data.additional_properties[:'agent_host_top99p_sum']).to eq 7 + expect(data.additional_properties.key?(:'apm_host_top99p_sum')).to be false + end + end end