fix: Handle nil processed_params for WhatsApp templates without params (#12177)

WhatsApp templates without parameters (body-only templates like
notifications, confirmations) were failing to send with the error:
ArgumentError (Unknown legacy format: NilClass). This affected all
parameter-less templates across marketing messages, notifications, and
utility templates.
This commit is contained in:
Muhsin Keloth
2025-08-12 22:56:53 +05:30
committed by GitHub
parent 469e724e3a
commit 48fa7bf72b
3 changed files with 84 additions and 0 deletions

View File

@@ -86,6 +86,9 @@ class Whatsapp::TemplateParameterConverterService
# Hash format: {"1": "John", "name": "Jane"} → {body: {"1": "John", "name": "Jane"}}
body_params = convert_hash_to_body_params(legacy_params)
enhanced['body'] = body_params unless body_params.empty?
when NilClass
# Templates without parameters (nil processed_params)
# Return empty enhanced structure
else
raise ArgumentError, "Unknown legacy format: #{legacy_params.class}"
end

View File

@@ -63,6 +63,25 @@ FactoryBot.define do
],
'sub_category' => 'CUSTOM',
'parameter_format' => 'NAMED'
},
{
'name' => 'test_no_params_template',
'status' => 'APPROVED',
'category' => 'UTILITY',
'language' => 'en',
'namespace' => 'ed41a221_133a_4558_a1d6_192960e3aee9',
'id' => '9876543210987654',
'length' => 1,
'parameter_format' => 'POSITIONAL',
'previous_category' => 'MARKETING',
'sub_category' => 'CUSTOM',
'components' => [
{
'text' => 'Thank you for contacting us! Your request has been processed successfully. Have a great day! 🙂',
'type' => 'BODY'
}
],
'rejected_reason' => 'NONE'
}]
end
message_templates_last_updated { Time.now.utc }

View File

@@ -133,6 +133,48 @@ describe Whatsapp::TemplateParameterConverterService do
end
end
context 'when processed_params is nil (parameter-less templates)' do
let(:nil_params) do
{
'processed_params' => nil
}
end
let(:parameterless_template) do
{
'name' => 'test_no_params_template',
'language' => 'en',
'parameter_format' => 'POSITIONAL',
'id' => '9876543210987654',
'status' => 'APPROVED',
'category' => 'UTILITY',
'previous_category' => 'MARKETING',
'sub_category' => 'CUSTOM',
'components' => [
{
'type' => 'BODY',
'text' => 'Thank you for contacting us! Your request has been processed successfully. Have a great day! 🙂'
}
]
}
end
it 'converts nil to empty enhanced format' do
converter = described_class.new(nil_params, parameterless_template)
result = converter.normalize_to_enhanced
expect(result['processed_params']).to eq({})
expect(result['format_version']).to eq('legacy')
end
it 'does not raise ArgumentError for nil processed_params' do
expect do
converter = described_class.new(nil_params, parameterless_template)
converter.normalize_to_enhanced
end.not_to raise_error
end
end
context 'when invalid format' do
let(:invalid_params) do
{
@@ -174,6 +216,26 @@ describe Whatsapp::TemplateParameterConverterService do
end
describe 'simplified conversion methods' do
describe '#convert_legacy_to_enhanced' do
it 'handles nil processed_params without raising error' do
converter = described_class.new({}, template)
result = converter.send(:convert_legacy_to_enhanced, nil, template)
expect(result).to eq({})
end
it 'returns empty hash for parameter-less templates' do
parameterless_template = {
'name' => 'no_params_template',
'language' => 'en',
'components' => [{ 'type' => 'BODY', 'text' => 'Hello World!' }]
}
converter = described_class.new({}, parameterless_template)
result = converter.send(:convert_legacy_to_enhanced, nil, parameterless_template)
expect(result).to eq({})
end
end
describe '#convert_array_to_body_params' do
it 'converts empty array' do
converter = described_class.new({}, template)