From a863983e7d888e802615cdebc74ae29cc88583f5 Mon Sep 17 00:00:00 2001 From: Paul Guelpa Date: Mon, 15 Sep 2014 15:10:06 -0400 Subject: [PATCH 1/2] Deep merge objects when dereferencing --- Gemfile.lock | 6 ++++-- lib/prmd.rb | 1 + lib/prmd/schema.rb | 7 ++----- prmd.gemspec | 1 + test/schema_test.rb | 11 +++++++++++ 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ac627ef..271774e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,14 +2,16 @@ PATH remote: . specs: prmd (0.6.2) + deep_merge (~> 1.0) erubis (~> 2.7) - json_schema (~> 0.1) + json_schema (~> 0.1, >= 0.1.8) GEM remote: https://rubygems.org/ specs: + deep_merge (1.0.1) erubis (2.7.0) - json_schema (0.1.4) + json_schema (0.2.0) minitest (5.3.2) rake (10.2.2) diff --git a/lib/prmd.rb b/lib/prmd.rb index eb04478..c6ddc22 100644 --- a/lib/prmd.rb +++ b/lib/prmd.rb @@ -1,4 +1,5 @@ require "cgi" +require 'deep_merge' require "erubis" require "json" require "yaml" diff --git a/lib/prmd/schema.rb b/lib/prmd/schema.rb index 4dd626f..cd33f09 100644 --- a/lib/prmd/schema.rb +++ b/lib/prmd/schema.rb @@ -48,7 +48,7 @@ def dereference(reference) dereferenced_key, dereferenced_value = dereference(datum) [ [key, dereferenced_key].compact.last, - [dereferenced_value, value].inject({}) { |composite, element| composite.merge(element) } + [dereferenced_value, value].inject({}) { |composite, element| composite.deep_merge!(element) } ] rescue => error $stderr.puts("Failed to dereference `#{key}`") @@ -60,10 +60,7 @@ def schema_value_example(value) if value.has_key?('example') value['example'] elsif value.has_key?('anyOf') - idRef = value['anyOf'].detect do |ref| - ref['$ref'] && ref['$ref'].split('/').last == 'id' - end - ref = idRef || value['anyOf'].first + ref = value['anyOf'].detect {|ref| ref['$ref'].split('/').last == 'id'} || value['anyOf'].first schema_example(ref) elsif value.has_key?('properties') # nested properties schema_example(value) diff --git a/prmd.gemspec b/prmd.gemspec index c0cb85c..e07af55 100644 --- a/prmd.gemspec +++ b/prmd.gemspec @@ -18,6 +18,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] + spec.add_dependency "deep_merge", "~> 1.0" spec.add_dependency "erubis", "~> 2.7" spec.add_dependency "json_schema", "~> 0.1", ">= 0.1.8" diff --git a/test/schema_test.rb b/test/schema_test.rb index e68acb5..f9a4700 100644 --- a/test/schema_test.rb +++ b/test/schema_test.rb @@ -23,6 +23,17 @@ def test_dereference_with_nested_ref assert_equal(value, user_input_schema['definitions']['user']['definitions']['id']) end + def test_dereference_with_overridable_values + key, value = user_input_schema.dereference({ + '$ref' => '#/definitions/user/properties', + 'id' => { + 'example' => 'my-custom-override-id' + } + }) + assert_equal(value['id']['$ref'], '#/definitions/user/definitions/id') + assert_equal(value['id']['example'], 'my-custom-override-id') + end + def test_dereference_with_local_context key, value = user_input_schema.dereference({ '$ref' => '#/definitions/user/properties/id', From f51a657714faa5f306ce5c41d1d4ce0d0c47b0b3 Mon Sep 17 00:00:00 2001 From: Paul Guelpa Date: Mon, 15 Sep 2014 16:53:35 -0400 Subject: [PATCH 2/2] Fix failing render_test spec --- lib/prmd/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/prmd/schema.rb b/lib/prmd/schema.rb index cd33f09..f3d0383 100644 --- a/lib/prmd/schema.rb +++ b/lib/prmd/schema.rb @@ -60,7 +60,7 @@ def schema_value_example(value) if value.has_key?('example') value['example'] elsif value.has_key?('anyOf') - ref = value['anyOf'].detect {|ref| ref['$ref'].split('/').last == 'id'} || value['anyOf'].first + ref = value['anyOf'].detect {|ref| ref.has_key?('$ref') && ref['$ref'].split('/').last == 'id'} || value['anyOf'].first schema_example(ref) elsif value.has_key?('properties') # nested properties schema_example(value)