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

feat!: treat empty string as a nil value in deser #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/typed/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def deserialize_from_creation_params(creation_params)

if value.nil? && !field.default.nil?
Success.new(Validations::ValidatedValue.new(name: field.name, value: field.default))
elsif !field.required && (value.nil? || value == "")
Copy link
Collaborator

Choose a reason for hiding this comment

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

(nitpick) the if before and the elsif after it have the value check first and then the field check so it's a bit faster to read as if we follow that ordering here elsif (value.nil? || value == "") && !field.required

Success.new(Validations::ValidatedValue.new(name: field.name, value: nil))
elsif value.nil? || field.works_with?(value)
field.validate(value)
elsif !coercer.nil?
Expand Down
7 changes: 7 additions & 0 deletions test/typed/hash_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ def test_with_boolean_string_true_it_can_deserialize
assert_payload(DC_CITY, result)
end

def test_with_empty_string_for_nilable_it_can_deserialize
result = Typed::HashSerializer.new(schema: Typed::Schema.from_struct(Job)).deserialize({title: "Software Developer", salary: {cents: 9000000, currency: "USD"}, start_date: ""})

assert_success(result)
assert_payload(DEVELOPER_JOB, result)
end

def test_with_array_it_can_deserialize
result = Typed::HashSerializer.new(schema: Typed::Schema.from_struct(Country)).deserialize({name: "US", cities: [NEW_YORK_CITY, DC_CITY], national_items: {bird: "bald eagle", anthem: "The Star-Spangled Banner"}})

Expand Down
7 changes: 7 additions & 0 deletions test/typed/json_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ def test_with_boolean_string_true_it_can_deserialize
assert_payload(DC_CITY, result)
end

def test_with_empty_string_for_nilable_it_can_deserialize
result = Typed::JSONSerializer.new(schema: Typed::Schema.from_struct(Job)).deserialize('{"title":"Software Developer","salary":{"cents":9000000,"currency":"USD"},"start_date":""}')

assert_success(result)
assert_payload(DEVELOPER_JOB, result)
end

def test_with_array_it_can_deep_deserialize
result = Typed::JSONSerializer.new(schema: Typed::Schema.from_struct(Country)).deserialize('{"name":"US","cities":[{"name":"New York","capital":false},{"name":"DC","capital":true}],"national_items":{"bird":"bald eagle","anthem":"The Star-Spangled Banner"}}')

Expand Down
Loading