From 0d13fecf5bd490cbe149f90283211455cc5daf99 Mon Sep 17 00:00:00 2001 From: Sam Meyer Date: Fri, 18 Jan 2019 15:09:29 -0800 Subject: [PATCH 1/3] Add spec to demonstrate boolean value bug - Currently there is a bug in the gem that incorrectly renders false boolean values - This adds the attributes and spec to demonstrate the failure - A following commit will fix this bug and pass the specs --- spec/activerecord_helper.rb | 3 ++- spec/factories/model_factories.rb | 1 + spec/integrations/base_spec.rb | 3 ++- spec/integrations/shared/base_render_examples.rb | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/spec/activerecord_helper.rb b/spec/activerecord_helper.rb index 6fccdccf..62d59476 100644 --- a/spec/activerecord_helper.rb +++ b/spec/activerecord_helper.rb @@ -11,7 +11,7 @@ class Vehicle < ActiveRecord::Base end class User < ActiveRecord::Base - attr_accessor :company, :description, :position + attr_accessor :company, :description, :position, :active has_many :vehicles end @@ -25,6 +25,7 @@ class User < ActiveRecord::Base t.datetime "created_at", null: false t.datetime "updated_at", null: false t.datetime "deleted_at" + t.boolean "active" end create_table "vehicles", force: :cascade do |t| diff --git a/spec/factories/model_factories.rb b/spec/factories/model_factories.rb index 31d02c85..1d0f6459 100644 --- a/spec/factories/model_factories.rb +++ b/spec/factories/model_factories.rb @@ -9,6 +9,7 @@ company { 'Procore' } birthday { Date.new(1994, 3, 4) } deleted_at { nil } + active { false } end factory :vehicle do diff --git a/spec/integrations/base_spec.rb b/spec/integrations/base_spec.rb index 5aeecf28..142c20a1 100644 --- a/spec/integrations/base_spec.rb +++ b/spec/integrations/base_spec.rb @@ -20,7 +20,8 @@ description: 'A person', company: 'Procore', birthday: Date.new(1994, 3, 4), - deleted_at: nil + deleted_at: nil, + active: false } end let(:object_with_attributes) { OpenStruct.new(obj_hash) } diff --git a/spec/integrations/shared/base_render_examples.rb b/spec/integrations/shared/base_render_examples.rb index 7e5b5b39..49e389ac 100644 --- a/spec/integrations/shared/base_render_examples.rb +++ b/spec/integrations/shared/base_render_examples.rb @@ -10,6 +10,20 @@ it('returns json with specified fields') { should eq(result) } end + context 'Given blueprint has ::field with all data types' do + let(:result) { '{"active":false,"birthday":"1994-03-04","deleted_at":null,"first_name":"Meg","id":' + obj_id + '}' } + let(:blueprint) do + Class.new(Blueprinter::Base) do + field :id # number + field :first_name # string + field :active # boolean + field :birthday # date + field :deleted_at # null + end + end + it('returns json with the correct values for each data type') { should eq(result) } + end + context 'Given blueprint has ::fields' do let(:result) do '{"id":' + obj_id + ',"description":"A person","first_name":"Meg"}' From 9d566e1d223f6b50e922137db85c1ad6870e9fe6 Mon Sep 17 00:00:00 2001 From: Sam Meyer Date: Fri, 18 Jan 2019 15:19:34 -0800 Subject: [PATCH 2/3] Fix extraction of boolean values - When HashExtractor accessed a field name in the object that resulted in a `false` boolean value, the method should return _that_ value. Instead it would trigger second part of the OR Operator, which would try to lookup `object["false"]` which does not exist thus returning `nil` --- lib/blueprinter/extractors/hash_extractor.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/blueprinter/extractors/hash_extractor.rb b/lib/blueprinter/extractors/hash_extractor.rb index b150476f..76d494cb 100644 --- a/lib/blueprinter/extractors/hash_extractor.rb +++ b/lib/blueprinter/extractors/hash_extractor.rb @@ -1,7 +1,7 @@ module Blueprinter class HashExtractor < Extractor - def extract(field_name, object, local_options, options = {}) - object[field_name] || object[field_name.to_s] + def extract(field_name, object, _local_options, _options = {}) + object[field_name] end end end From ed5f21aa72510e01b3aeefeadf3d3734ff46db14 Mon Sep 17 00:00:00 2001 From: Sam Meyer Date: Thu, 24 Jan 2019 09:29:36 -0800 Subject: [PATCH 3/3] Bump lib version and update Changelog --- CHANGELOG.md | 4 ++++ lib/blueprinter/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b1978ef..bff4727f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.12.1 - 2019/1/24 + +* 🐛 [BUGFIX] Fix boolean `false` values getting serialized as `null`. Please see PR [#132](https://github.com/procore/blueprinter/pull/132). + ## 0.12.0 - 2019/1/16 * 🚀 [FEATURE] Enables the setting of global `:field_default` and `:association_default` option value in the Blueprinter Configuration that will be used as default values for fields and associations that evaluate to nil. [#128](https://github.com/procore/blueprinter/pull/128). Thanks to [@mcclayton](https://github.com/mcclayton). diff --git a/lib/blueprinter/version.rb b/lib/blueprinter/version.rb index 222cfd55..05f7fb4e 100644 --- a/lib/blueprinter/version.rb +++ b/lib/blueprinter/version.rb @@ -1,3 +1,3 @@ module Blueprinter - VERSION = '0.12.0' + VERSION = '0.12.1' end