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!: Ensure that nested structs will deeply serialize #118

Merged
merged 8 commits into from
Jul 19, 2024

Conversation

tbconroy
Copy link
Contributor

The HashSerializer would fail to serialize structs nested more than one level deep even if option should_serialize_values was set to true.

For example, if we update the Job test objects to have a Money Struct for salary, i.e.:

Job.new(title: "Software Developer", salary: Money.new(cents: 90_000_00))

Then, we serialize the Person:

serializer = Typed::HashSerializer.new(schema: Typed::Schema.from_struct(Person), should_serialize_values: true)

serializer.serialize(ALEX_PERSON)
# => {name: "Alex", age: 31, stone_rank: "pretty", job: {title: "Software Developer", salary:<Money cents=9000000 currency=#<Currency::USD>>, needs_credential: false}}, result)

It should probably be:

serializer.serialize(ALEX_PERSON)
# => {name: "Alex", age: 31, stone_rank: "pretty", job: {title: "Software Developer", salary: {cents: 9000000, currency: "USD", needs_credential: false}}, result)

The main change is adding a nested_hash serializer type to lib/sorbet-schema/t/struct.rb. This will deep serialize structs to when hashes.

This new type is used in lib/sorbet-schema/serialize_value.rb so that values of values are recursively serialized.

maxveldink
maxveldink previously approved these changes Jul 18, 2024
test/t/struct_test.rb Outdated Show resolved Hide resolved
lib/sorbet-schema/t/struct.rb Outdated Show resolved Hide resolved
@tbconroy tbconroy changed the title Ensure that nested structs will deeply serialize fix: Ensure that nested structs will deeply serialize Jul 18, 2024
Copy link
Owner

@maxveldink maxveldink left a comment

Choose a reason for hiding this comment

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

Just need to remove the version bump change and we should be good to go!

lib/sorbet-schema/version.rb Outdated Show resolved Hide resolved
test/t/struct_test.rb Outdated Show resolved Hide resolved
test/t/struct_test.rb Outdated Show resolved Hide resolved
@tbconroy tbconroy requested a review from maxveldink July 19, 2024 15:55
@maxveldink maxveldink changed the title fix: Ensure that nested structs will deeply serialize fix!: Ensure that nested structs will deeply serialize Jul 19, 2024
@maxveldink maxveldink merged commit 9216d02 into maxveldink:main Jul 19, 2024
4 of 5 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