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

Preserve empty JSON types #81

Merged
merged 4 commits into from
Jun 7, 2019
Merged
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: 1 addition & 1 deletion lib/vault/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require_relative "encrypted_model"
require_relative "rails/configurable"
require_relative "rails/errors"
require_relative "rails/serializer"
require_relative "rails/json_serializer"
require_relative "rails/version"

module Vault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ module JSONSerializer
}.freeze

def self.encode(raw)
self._init!

raw = {} if raw.nil?
_init!

JSON.fast_generate(raw)
end

def self.decode(raw)
self._init!
_init!

return nil if raw == nil || raw == ""

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

Expand Down
10 changes: 0 additions & 10 deletions spec/integration/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,6 @@
Vault::Rails.logical.write("transit/keys/dummy_people_details")
end

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

it "has a default value for persisted records" do
person = Person.create!
expect(person.details).to eq({})
end

it "tracks dirty attributes" do
person = Person.create!(details: { "foo" => "bar" })

Expand Down
42 changes: 42 additions & 0 deletions spec/lib/vault/rails/json_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

RSpec.describe Vault::Rails::JSONSerializer do
[
nil,
false,
true,
"",
"foo",
{},
{ "foo" => "bar" },
[],
["foo", "bar"],
0,
123,
0.0,
0.123,
0xff,
123e123
].each do |object|
it "encodes and decodes #{object.inspect}" do
encoded = described_class.encode(object)
expect(encoded).to be_a(String)
decoded = described_class.decode(encoded)
expect(decoded).to eq(object)
end
end

describe ".decode" do
subject(:decoded) { described_class.decode(raw) }

context "with nil" do
let(:raw) { nil }
it { is_expected.to eq(nil) }
end

context "with an empty string (only possible if column has a default)" do
let(:raw) { "" }
it { is_expected.to eq(nil) }
end
end
end