Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(custom_aggregation): Ensure custom properties are stored as Hash #2781

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ def initialize(charge:, properties:)

def call
result.properties = slice_properties || {}

if result.properties[:custom_properties].present? && result.properties[:custom_properties].is_a?(String)
result.properties[:custom_properties] = begin
JSON.parse(result.properties[:custom_properties])
rescue JSON::ParserError
{}
end
end

result
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
per_transaction_max_amount: 100,
per_transaction_min_amount: 10,
volume_ranges: [{from_value: 0, to_value: 100, per_unit_amount: '2', flat_amount: '1'}],
custom_properties: {rate: '20'}
custom_properties:
}
end

let(:custom_properties) { {rate: '20'} }

describe '#call' do
context 'without charge_model' do
it 'returns empty hash' do
Expand Down Expand Up @@ -90,6 +92,21 @@
let(:billable_metric) { build(:custom_billable_metric) }

it { expect(filter_service.call.properties.keys).to include('custom_properties') }
it { expect(filter_service.call.properties[:custom_properties]).to be_a(Hash) }

context 'when custom_properties is a string' do
let(:custom_properties) { '{"rate": 20}' }

it { expect(filter_service.call.properties.keys).to include('custom_properties') }
it { expect(filter_service.call.properties[:custom_properties]).to eq('rate' => 20) }

context 'when properties failed to parse' do
let(:custom_properties) { 'rate: 20' }

it { expect(filter_service.call.properties.keys).to include('custom_properties') }
it { expect(filter_service.call.properties[:custom_properties]).to eq({}) }
end
end
end
end
end