From ad4a545a7df0c31cfd28bb3c3451c269321cab54 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Tue, 4 Jun 2024 22:27:05 -0500 Subject: [PATCH 1/3] fix: serialize empty has-many array --- lib/store_model/types/many_base.rb | 11 +++++++---- spec/store_model/types/many_spec.rb | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/store_model/types/many_base.rb b/lib/store_model/types/many_base.rb index 4babd44..5c5d653 100644 --- a/lib/store_model/types/many_base.rb +++ b/lib/store_model/types/many_base.rb @@ -40,11 +40,14 @@ def cast_value(value) def serialize(value) case value when Array - return ActiveSupport::JSON.encode(value) unless value.all? { |v| v.is_a?(StoreModel::Model) } + if value.any? && value.all? { |v| v.is_a?(StoreModel::Model) } + ActiveSupport::JSON.encode(value, + serialize_unknown_attributes: value.first.serialize_unknown_attributes?, + serialize_enums_using_as_json: value.first.serialize_enums_using_as_json?) - ActiveSupport::JSON.encode(value, - serialize_unknown_attributes: value.first.serialize_unknown_attributes?, - serialize_enums_using_as_json: value.first.serialize_enums_using_as_json?) + else + ActiveSupport::JSON.encode(value) + end else super end diff --git a/spec/store_model/types/many_spec.rb b/spec/store_model/types/many_spec.rb index 68848ed..8d5cef3 100644 --- a/spec/store_model/types/many_spec.rb +++ b/spec/store_model/types/many_spec.rb @@ -166,6 +166,12 @@ include_examples "serialize examples" end + context "when any empty Array is passed" do + let(:value) { [] } + + it { is_expected.to eq("[]") } + end + context "when String is passed" do let(:value) { ActiveSupport::JSON.encode(attributes_array) } include_examples "serialize examples" From 438de901348f835201c8c78efaf88977154abdc8 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Tue, 4 Jun 2024 22:37:27 -0500 Subject: [PATCH 2/3] style: make rubocop happy --- lib/store_model/types/many_base.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/store_model/types/many_base.rb b/lib/store_model/types/many_base.rb index 5c5d653..d902af6 100644 --- a/lib/store_model/types/many_base.rb +++ b/lib/store_model/types/many_base.rb @@ -40,14 +40,11 @@ def cast_value(value) def serialize(value) case value when Array - if value.any? && value.all? { |v| v.is_a?(StoreModel::Model) } - ActiveSupport::JSON.encode(value, - serialize_unknown_attributes: value.first.serialize_unknown_attributes?, - serialize_enums_using_as_json: value.first.serialize_enums_using_as_json?) + return ActiveSupport::JSON.encode(value) if value.empty? || value.any? { |v| !v.is_a?(StoreModel::Model) } - else - ActiveSupport::JSON.encode(value) - end + ActiveSupport::JSON.encode(value, + serialize_unknown_attributes: value.first.serialize_unknown_attributes?, + serialize_enums_using_as_json: value.first.serialize_enums_using_as_json?) else super end From 7d35fbdccf5b59a5dde84dd0e8a5bc9475acc795 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Tue, 4 Jun 2024 22:38:36 -0500 Subject: [PATCH 3/3] test: fix tests --- spec/store_model/types/many_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/store_model/types/many_spec.rb b/spec/store_model/types/many_spec.rb index 8d5cef3..2467407 100644 --- a/spec/store_model/types/many_spec.rb +++ b/spec/store_model/types/many_spec.rb @@ -168,8 +168,9 @@ context "when any empty Array is passed" do let(:value) { [] } + let(:attributes_array) { [] } - it { is_expected.to eq("[]") } + include_examples "serialize examples" end context "when String is passed" do