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

Deep merge dereferenced keys #163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions lib/prmd.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "cgi"
require 'deep_merge'
require "erubis"
require "json"
require "yaml"
Expand Down
7 changes: 2 additions & 5 deletions lib/prmd/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}`")
Expand All @@ -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.has_key?('$ref') && ref['$ref'].split('/').last == 'id'} || value['anyOf'].first
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't see how this was affected by my change, but there was a test that was failing for me locally. I also wasn't sure how this was working before, unless somewhere we're creating hash objects that have an empty string as the default value.

schema_example(ref)
elsif value.has_key?('properties') # nested properties
schema_example(value)
Expand Down
1 change: 1 addition & 0 deletions prmd.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
11 changes: 11 additions & 0 deletions test/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down