From 8806fcb4be8b442fb7ae0e14e89ed7eec2c974f0 Mon Sep 17 00:00:00 2001 From: Manuel van Rijn Date: Mon, 3 Jun 2024 10:29:48 +0200 Subject: [PATCH 1/2] Fixed issue for empty nested array --- lib/store_model/model.rb | 2 +- spec/store_model/nested_attributes_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/store_model/model.rb b/lib/store_model/model.rb index a15eca0..898eb47 100644 --- a/lib/store_model/model.rb +++ b/lib/store_model/model.rb @@ -246,7 +246,7 @@ def serialized_attribute(attr) end def serialize_array_attribute(array) - return array.as_json unless array.all? { |value| value.is_a?(StoreModel::Model) } + return array.as_json unless array.any? && array.all? { |value| value.is_a?(StoreModel::Model) } array.as_json( serialize_unknown_attributes: array.first.serialize_unknown_attributes?, diff --git a/spec/store_model/nested_attributes_spec.rb b/spec/store_model/nested_attributes_spec.rb index c30f6ce..9e21363 100644 --- a/spec/store_model/nested_attributes_spec.rb +++ b/spec/store_model/nested_attributes_spec.rb @@ -102,6 +102,7 @@ class Store unknown: "nested array unknown" } ], + nested_empty_array: [], status: "active", unknown: "unknown" } end @@ -123,6 +124,7 @@ class Store unknown: "nested array unknown" } ], + nested_empty_array: [], status: "active", unknown: "unknown" } end @@ -146,6 +148,16 @@ class Store expect(very_nested_value).to eq([record.id, "nested public"]) end + describe "empty nested array" do + it "persists empty nested array" do + record.save + query = Anything.where(id: record.id).select(:store).to_sql + persisted_store = JSON.parse(ActiveRecord::Base.connection.query(query)[0][0]) + + expect(persisted_store["nested_array"]).to eq([]) + end + end + describe "unknown attributes in nested objects" do [true, false].each do |serialize_unknown_attributes| it "always stores unknown attributes regardless of the serialize_unknown_attributes option" do From 44062bd5a0c5b26ba67c09a74f4aa5d0954fa3dc Mon Sep 17 00:00:00 2001 From: Manuel van Rijn Date: Mon, 3 Jun 2024 10:59:43 +0200 Subject: [PATCH 2/2] make the tests pass --- spec/store_model/nested_attributes_spec.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/store_model/nested_attributes_spec.rb b/spec/store_model/nested_attributes_spec.rb index 9e21363..e46307e 100644 --- a/spec/store_model/nested_attributes_spec.rb +++ b/spec/store_model/nested_attributes_spec.rb @@ -77,6 +77,7 @@ class Store attribute :non_enc_val attribute :nested, NestedStore.to_type attribute :nested_array, NestedStore.to_array_type + attribute :empty_nested_array, NestedStore.to_array_type, default: [] enum :status, in: { active: 1, inactive: 2, archived: 3 } end @@ -102,7 +103,7 @@ class Store unknown: "nested array unknown" } ], - nested_empty_array: [], + empty_nested_array: [], status: "active", unknown: "unknown" } end @@ -124,7 +125,7 @@ class Store unknown: "nested array unknown" } ], - nested_empty_array: [], + empty_nested_array: [], status: "active", unknown: "unknown" } end @@ -154,7 +155,7 @@ class Store query = Anything.where(id: record.id).select(:store).to_sql persisted_store = JSON.parse(ActiveRecord::Base.connection.query(query)[0][0]) - expect(persisted_store["nested_array"]).to eq([]) + expect(persisted_store["empty_nested_array"]).to eq([]) end end