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 issue with serialization options not applied to nested objects #171

Conversation

osanay
Copy link
Contributor

@osanay osanay commented May 25, 2024

In the current latest version 2.4.0, I noticed that serialization options are not applied to nested objects. For example, when specifying the serialize_enums_using_as_json option in the as_json method, it behaves as follows:

StoreModel.config.serialize_enums_using_as_json = false

class Message
  include StoreModel::Model

  attribute :child, to_type

  enum :status, in: { unread: 0, read: 1 }
end

message = Message.new(status: "read", child: { status: "unread" })
message.as_json(
  serialize_enums_using_as_json: true
) # => { "child" => { "child" => nil, "status" => 0 }, "status" => "read" }

The same issue occurs with the serialize_unknown_attributes option.

This problem seems to arise because the as_json method serializes nested objects recursively; however, the serialization options are not passed down to the nested objects.

I resolved this issue by storing the serialization options as attributes in the StoreModel::Model object. This ensures that the options are correctly inherited by nested objects during serialization.

I would appreciate your feedback on this solution.

Resolved an issue where nested `StoreModel::Model` objects did not
inherit serialization options when the `as_json` method was called.
To address this, added two attributes to `StoreModel::Model` to
ensure proper inheritance of serialization settings.
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! Do you think we need a major release for this change?

@osanay
Copy link
Contributor Author

osanay commented May 28, 2024

LGTM! Do you think we need a major release for this change?

Thank you! This pull request addresses a bug related to nested objects. Since it fixes an existing issue and ensures the correct behavior, I believe a minor or patch release would be appropriate. However, there is also the change in the behavior of persisting unknown attributes in the database, so if there are new features or other changes planned for the near future, it might make sense to include this fix in a major release.

@DmitryTsepelev DmitryTsepelev merged commit 1a8eb48 into DmitryTsepelev:master Jun 1, 2024
16 checks passed
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.

2 participants