Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serialize empty has-many array #176

Merged

Conversation

bf4
Copy link
Contributor

@bf4 bf4 commented Jun 5, 2024

Fixes #174

Root cause:

[].all? # => true
[].any? # => false
[ StoreModel::Model.allocate ].all? { |v| v.is_a?(StoreModel::Model) # => true

So, to check if all are StoreModel::Model, we need to

  1. (new) check the array has something in it
  2. check that everything in it is a StoreModel::Model

@bf4
Copy link
Contributor Author

bf4 commented Jun 5, 2024

Oddly, even though master is green in Github, I get the same errors locally on master as here.

Failures:

  1) StoreModel::CombineErrorsStrategies::MergeErrorStrategy when parent model has validation keeps parent validation messages
     Failure/Error:
       expect(record.errors.messages).to eq(
         name: ["can't be blank"], configuration: ["Color can't be blank"]
       )

       expected: {:configuration=>["Color can't be blank"], :name=>["can't be blank"]}
            got: #<ActiveModel::DeprecationHandlingMessageHash({:configuration=>#<ActiveModel::DeprecationHandlingMess...or can't be blank"])>, :name=>#<ActiveModel::DeprecationHandlingMessageArray(["can't be blank"])>})>

       (compared using ==)

       Diff:
       @@ -1,3 +1,3 @@
       -:configuration => ["Color can't be blank"],
       -:name => ["can't be blank"],
       +{:name=>["can't be blank"],
       + :configuration=>["is invalid", "Color can't be blank"]}

     # ./spec/store_model/combine_error_strategies/merge_error_strategy_spec.rb:63:in `block (3 levels) in <top (required)>'

  2) StoreModel::Model#parent array store model parent updates parent after assignment
     Failure/Error: expect(configuration.parent).to eq(subject)

       expected: #< configuration: [#<Configuration color: nil, model: nil, active: true, disabled_at: nil, encrypted_serial: nil, type: nil>]>
            got: nil

       (compared using ==)
     Shared Example Group: "for array type" called from ./spec/store_model/model_spec.rb:270
     # ./spec/store_model/model_spec.rb:213:in `block (4 levels) in <top (required)>'

  3) StoreModel::Model#parent json store model parent updates parent after assignment
     Failure/Error: expect(configuration.parent).to eq(subject)

       expected: #< configuration: #<Configuration color: nil, model: nil, active: true, disabled_at: nil, encrypted_serial: nil, type: nil>>
            got: nil

       (compared using ==)
     Shared Example Group: "for singular type" called from ./spec/store_model/model_spec.rb:244
     # ./spec/store_model/model_spec.rb:190:in `block (4 levels) in <top (required)>'

Finished in 0.33743 seconds (files took 0.98431 seconds to load)
342 examples, 3 failures

Failed examples:

rspec ./spec/store_model/combine_error_strategies/merge_error_strategy_spec.rb:49 # StoreModel::CombineErrorsStrategies::MergeErrorStrategy when parent model has validation keeps parent validation messages
rspec ./spec/store_model/model_spec.rb[1:6:2:2:2] # StoreModel::Model#parent array store model parent updates parent after assignment
rspec ./spec/store_model/model_spec.rb[1:6:1:2:2] # StoreModel::Model#parent json store model parent updates parent after assignment

Randomized with seed 16673

@bf4 bf4 force-pushed the serialization_of_empty_array branch from c3ad1c5 to 91c1b1a Compare June 5, 2024 03:40
@bf4 bf4 force-pushed the serialization_of_empty_array branch from 91c1b1a to 7d35fbd Compare June 5, 2024 03:42
Copy link
Owner

@DmitryTsepelev DmitryTsepelev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you 🙏

@DmitryTsepelev DmitryTsepelev merged commit c9fe39f into DmitryTsepelev:master Jun 5, 2024
16 checks passed
@bf4 bf4 deleted the serialization_of_empty_array branch June 5, 2024 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NoMethodError serialize_unknown_attributes? on upgrading to 3.0.0
2 participants