Add documentation json parse rescue repetition

This commit is contained in:
Damien Le Thiec 2023-02-13 14:17:53 +01:00
parent ea126612a1
commit 899c8a6589
2 changed files with 7 additions and 1 deletions

View file

@ -20,7 +20,6 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
value.map.with_index do |repetition, index|
PrefillRepetitionRow.new(champ, repetition, index).to_assignable_attributes
rescue JSON::ParserError
end.compact
end
@ -65,6 +64,7 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
return unless subchamp
TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ).to_assignable_attributes(subchamp, value)
rescue JSON::ParserError # On ignore les valeurs qu'on n'arrive pas à parser
end.compact
end
end

View file

@ -62,6 +62,12 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: {
it { is_expected.to match([]) }
end
context 'when the value is an array with some wrong keys' do
let(:value) { ["{\"#{type_de_champ_child.libelle}\":\"value\"}", "{\"blabla\":\"value2\"}"] }
it { is_expected.to match([[{ id: type_de_champ_child.champ.first.id, value: "value" }]]) }
end
context 'when the value is an array with right keys' do
let(:value) { ["{\"#{type_de_champ_child.libelle}\":\"value\"}", "{\"#{type_de_champ_child.libelle}\":\"value2\"}"] }