From ca1200b90f4fddb68cc7189449cac2e5cd82c2b4 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Thu, 16 Nov 2017 11:39:26 -0500 Subject: [PATCH 1/2] add failing tests for stripping null values --- .../{json_ld_spec.rb => json_ld_drop_spec.rb} | 8 ++++++++ spec/jekyll_seo_tag_integration_spec.rb | 4 ++++ 2 files changed, 12 insertions(+) rename spec/jekyll_seo_tag/{json_ld_spec.rb => json_ld_drop_spec.rb} (96%) diff --git a/spec/jekyll_seo_tag/json_ld_spec.rb b/spec/jekyll_seo_tag/json_ld_drop_spec.rb similarity index 96% rename from spec/jekyll_seo_tag/json_ld_spec.rb rename to spec/jekyll_seo_tag/json_ld_drop_spec.rb index df463d5a..ea6a6010 100644 --- a/spec/jekyll_seo_tag/json_ld_spec.rb +++ b/spec/jekyll_seo_tag/json_ld_drop_spec.rb @@ -152,4 +152,12 @@ expect(subject).to have_key("url") expect(subject["url"]).to eql("/page.html") end + + context "with null values" do + let(:metadata) { {} } + + it "does not return null values as json" do + expect(subject.to_json).to_not match(/:null/) + end + end end diff --git a/spec/jekyll_seo_tag_integration_spec.rb b/spec/jekyll_seo_tag_integration_spec.rb index 8f4f589b..9a13ac74 100755 --- a/spec/jekyll_seo_tag_integration_spec.rb +++ b/spec/jekyll_seo_tag_integration_spec.rb @@ -338,6 +338,10 @@ it "minifies JSON-LD" do expect(output).to_not match(%r!{.*?\s.*?}!) end + + it "removes null values from JSON-LD" do + expect(output).to_not match(/:null/) + end end end From da96f4d707adcbedec7bce1b459113093974b50f Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Thu, 16 Nov 2017 11:40:42 -0500 Subject: [PATCH 2/2] strip null values from JSON-LD hash --- lib/jekyll-seo-tag/json_ld_drop.rb | 4 ++++ spec/jekyll_seo_tag/json_ld_drop_spec.rb | 2 +- spec/jekyll_seo_tag_integration_spec.rb | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/jekyll-seo-tag/json_ld_drop.rb b/lib/jekyll-seo-tag/json_ld_drop.rb index 3c0120e7..a8122ea0 100644 --- a/lib/jekyll-seo-tag/json_ld_drop.rb +++ b/lib/jekyll-seo-tag/json_ld_drop.rb @@ -73,6 +73,10 @@ def main_entity alias_method :mainEntityOfPage, :main_entity private :main_entity + def to_json + to_h.reject { |_k, v| v.nil? }.to_json + end + private attr_reader :page_drop diff --git a/spec/jekyll_seo_tag/json_ld_drop_spec.rb b/spec/jekyll_seo_tag/json_ld_drop_spec.rb index ea6a6010..1c1b28b5 100644 --- a/spec/jekyll_seo_tag/json_ld_drop_spec.rb +++ b/spec/jekyll_seo_tag/json_ld_drop_spec.rb @@ -157,7 +157,7 @@ let(:metadata) { {} } it "does not return null values as json" do - expect(subject.to_json).to_not match(/:null/) + expect(subject.to_json).to_not match(%r!:null!) end end end diff --git a/spec/jekyll_seo_tag_integration_spec.rb b/spec/jekyll_seo_tag_integration_spec.rb index 9a13ac74..bfd8bb4b 100755 --- a/spec/jekyll_seo_tag_integration_spec.rb +++ b/spec/jekyll_seo_tag_integration_spec.rb @@ -340,7 +340,7 @@ end it "removes null values from JSON-LD" do - expect(output).to_not match(/:null/) + expect(output).to_not match(%r!:null!) end end end