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

JSON: serialize empty arrays to nil #68

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions lib/superstore/types/json_type.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module Superstore
module Types
class JsonType < Base
def serialize(value)
value if value.present?
end
end
end
end
16 changes: 16 additions & 0 deletions test/unit/types/json_type_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
require 'test_helper'

class Superstore::Types::JsonTypeTest < Superstore::Types::TestCase
test 'serializes empty array to nil' do
matthinea marked this conversation as resolved.
Show resolved Hide resolved
comments = { 'foo' => 'bar' }
issue1 = Issue.new(comments:)
issue2 = Issue.new(comments: [])
issue3 = Issue.new(comments: {})

assert_equal comments, issue1.comments
assert_equal [], issue2.comments
assert_equal({}, issue3.comments)

[issue1, issue2].each(&:save!).each(&:reload)
matthinea marked this conversation as resolved.
Show resolved Hide resolved

assert_equal comments, issue1.comments
assert_nil issue2.comments
assert_equal({}, issue3.comments)
matthinea marked this conversation as resolved.
Show resolved Hide resolved
end
end
Loading