Skip to content

Commit

Permalink
fix(custom_aggregation): Ensure custom properties are parsed as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Nov 5, 2024
1 parent 81f2bf3 commit dc143a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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

0 comments on commit dc143a6

Please sign in to comment.