Skip to content

Commit

Permalink
Merge pull request #17 from FundingCircle/allow_nil_in_json_serializer
Browse files Browse the repository at this point in the history
Allow nil values in JSON serializer.
  • Loading branch information
finalwharf authored Oct 16, 2018
2 parents c09936e + e4135e9 commit 7d979c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
19 changes: 4 additions & 15 deletions lib/vault/rails/serializers/json_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "json"

module Vault
module Rails
module Serializers
Expand All @@ -8,27 +10,14 @@ module JSONSerializer
}.freeze

def self.encode(raw)
self._init!

raw = {} if raw.nil?

return if raw.nil?
JSON.fast_generate(raw)
end

def self.decode(raw)
self._init!

return {} if raw.nil? || raw.empty?
return if raw.nil?
JSON.parse(raw, DECODE_OPTIONS)
end

protected

def self._init!
return if defined?(@_init)
require "json"
@_init = true
end
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions spec/integration/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,18 @@
Vault::Rails.logical.write("transit/keys/dummy_people_details")
end

it "has a default value for unpersisted records" do
it "allows nil for unpersisted records" do
person = Person.new
expect(person.details).to eq({})
expect(person.details).to be_nil
end

it "has a default value for persisted records" do
it "allows nil for persisted records" do
person = Person.create!
expect(person.details).to be_nil
end

it 'saves an empty hash' do
person = Person.create!(details: {})
expect(person.details).to eq({})
end

Expand Down

0 comments on commit 7d979c5

Please sign in to comment.