From f1ac18bb7523eddd76d95c79a4576ee8fb7ff865 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Tue, 14 May 2019 14:29:11 +0200 Subject: [PATCH 1/4] Improve API products create specs We were not testing that Float::Infinity serialization is working correctly here. --- .../spree/api/products_controller_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/api/spec/requests/spree/api/products_controller_spec.rb b/api/spec/requests/spree/api/products_controller_spec.rb index d1e22b78c22..709c759bf77 100644 --- a/api/spec/requests/spree/api/products_controller_spec.rb +++ b/api/spec/requests/spree/api/products_controller_spec.rb @@ -295,6 +295,25 @@ module Spree expect(json_response['shipping_category_id']).to eq shipping_id end + context "when tracking is disabled" do + before { Config.track_inventory_levels = false } + + it "still displays valid json with total_on_hand Float::INFINITY" do + post spree.api_products_path, params: { + product: { + name: "The Other Product", + price: 19.99, + shipping_category_id: create(:shipping_category).id + } + } + + expect(response.status).to eq(201) + expect(json_response['total_on_hand']).to eq nil + end + + after { Config.track_inventory_levels = true } + end + it "puts the created product in the given taxon" do product_data[:taxon_ids] = taxon_1.id.to_s post spree.api_products_path, params: { product: product_data } From a383d8476022af779eaff288a3efde339bcba9b5 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Tue, 14 May 2019 14:48:59 +0200 Subject: [PATCH 2/4] Improve API variants show specs Be sure we are checking that Float::Infinity is being serialized correctly. --- .../requests/spree/api/variants_controller_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/api/spec/requests/spree/api/variants_controller_spec.rb b/api/spec/requests/spree/api/variants_controller_spec.rb index 9352e51ddfa..42ceb6031c3 100644 --- a/api/spec/requests/spree/api/variants_controller_spec.rb +++ b/api/spec/requests/spree/api/variants_controller_spec.rb @@ -248,6 +248,20 @@ module Spree expect(json_response["variant_properties"].first).to have_attributes(expected_attrs) end end + + context "when tracking is disabled" do + before do + Config.track_inventory_levels = false + subject + end + + it "still displays valid json with total_on_hand Float::INFINITY" do + expect(response.status).to eq(200) + expect(json_response['total_on_hand']).to eq nil + end + + after { Config.track_inventory_levels = true } + end end it "can learn how to create a new variant" do From 707e79b4e921e06d8b3c2fa7b2a9ead9fe2500bc Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Tue, 14 May 2019 14:51:33 +0200 Subject: [PATCH 3/4] Create an API helper method for total_on_hand This will be used also in the _product partial. We can't represent Float::INFINITY in JSON: Under JSON this would be NULL Under oj this would error --- api/app/helpers/spree/api/api_helpers.rb | 4 ++++ api/app/views/spree/api/variants/_small.json.jbuilder | 5 +---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/api/app/helpers/spree/api/api_helpers.rb b/api/app/helpers/spree/api/api_helpers.rb index b6024f469c2..0baca0944ba 100644 --- a/api/app/helpers/spree/api/api_helpers.rb +++ b/api/app/helpers/spree/api/api_helpers.rb @@ -183,6 +183,10 @@ def variant_attributes @@variant_attributes end end + + def total_on_hand_for(object) + object.total_on_hand.finite? ? object.total_on_hand : nil + end end end end diff --git a/api/app/views/spree/api/variants/_small.json.jbuilder b/api/app/views/spree/api/variants/_small.json.jbuilder index f7fdc2aaf98..d74f80e1661 100644 --- a/api/app/views/spree/api/variants/_small.json.jbuilder +++ b/api/app/views/spree/api/variants/_small.json.jbuilder @@ -9,10 +9,7 @@ json.cache! [I18n.locale, current_pricing_options, variant] do json.in_stock(variant.in_stock?) json.is_backorderable(variant.is_backorderable?) - # We can't represent Float::INFINITY in JSON - # Under JSON this woulb be NULL - # Under oj this would error - json.total_on_hand(variant.should_track_inventory? ? variant.total_on_hand : nil) + json.total_on_hand(total_on_hand_for(variant)) json.is_destroyed(variant.destroyed?) json.option_values(variant.option_values) do |option_value| From 71a1ef7758a1bc08cac2bc2b48a373d807e95d69 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Tue, 14 May 2019 14:52:32 +0200 Subject: [PATCH 4/4] Avoid JSON serializing Float::INFINITY Float::Infinity serialization is broken with Oj gem, see https://github.com/solidusio/solidus/pull/2495#issuecomment-365352792 This commit makes sure we are always nullifying total_on_hand before passing trying to serialize it, if it is a Float::Infinite. --- api/app/views/spree/api/products/_product.json.jbuilder | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/app/views/spree/api/products/_product.json.jbuilder b/api/app/views/spree/api/products/_product.json.jbuilder index 018ff3c23e6..4984a4107a1 100644 --- a/api/app/views/spree/api/products/_product.json.jbuilder +++ b/api/app/views/spree/api/products/_product.json.jbuilder @@ -2,7 +2,8 @@ @product_attributes ||= product_attributes json.cache! [I18n.locale, @current_user_roles.include?('admin'), current_pricing_options, @product_attributes, @exclude_data, product] do - json.(product, *@product_attributes) + json.(product, *(@product_attributes - [:total_on_hand])) + json.total_on_hand(total_on_hand_for(product)) json.price(product.price_for(current_pricing_options).try(:to_d)) json.display_price(product.price_for(current_pricing_options).to_s)