From 0a9aee5ddbda55a9e485a2af83c2e8b0953565cd Mon Sep 17 00:00:00 2001 From: Toshiya Komoda Date: Tue, 24 May 2016 21:03:19 +0900 Subject: [PATCH 1/2] Fix Error handling for the following two case. 4.1. add When the operation is applied, the target location MUST reference 4.2. remove The target location MUST exist for the operation to be successful. --- lib/json/patch.rb | 6 ++++-- test/json-patch_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/json/patch.rb b/lib/json/patch.rb index f08ee25..6523c9f 100644 --- a/lib/json/patch.rb +++ b/lib/json/patch.rb @@ -97,7 +97,7 @@ def add_operation(target_doc, path, value) end def add_object(target_doc, target_item, ref_token, value) - raise JSON::PatchError if target_item.nil? + raise JSON::PatchError if (target_item.nil? || !(Hash === target_item)) if ref_token.nil? target_doc.replace(value) else @@ -130,8 +130,10 @@ def remove_operation(target_doc, path) if Array === target_item target_item.delete_at ref_token.to_i if valid_index?(target_item, ref_token) - else + elsif Hash === target_item target_item.delete ref_token + else + raise JSON::PatchError end end diff --git a/test/json-patch_test.rb b/test/json-patch_test.rb index fd7591f..0fb3f80 100644 --- a/test/json-patch_test.rb +++ b/test/json-patch_test.rb @@ -121,6 +121,17 @@ end end + describe "the target location MUST reference" do + let(:target_document) { %q'{"foo":"bar","baz":"wat"}' } + let(:operation_document) { %q'[{ "op": "add", "path": "/baz/quux", "value": "qux" }]' } + + it "will raise exception if the target location is string (neither an array nor object)" do + assert_raises(JSON::PatchError) do + JSON.patch(target_document, operation_document) + end + end + end + =begin TODO When the operation is applied, the target location MUST reference one of: @@ -176,6 +187,18 @@ end end + describe "The target location MUST exist for the operation to be successful." do + let(:target_document) { %q'{"foo":"bar","baz":"qux"}' } + let(:operation_document) { %q'[{ "op": "remove", "path": "/foo/baz" }]' } + + it "will rails an exception if the member in the path does not exist" do + assert_raises(JSON::PatchError) do + JSON.patch(target_document, operation_document) + end + end + end + + end describe "Section 4.3: The replace operation" do From 010e085e2690fd0131ae8ddc75dece81a4ecb592 Mon Sep 17 00:00:00 2001 From: Toshiya Komoda Date: Sat, 28 May 2016 16:13:14 +0900 Subject: [PATCH 2/2] debug unit tests. 1. fix for the parallel execution in the latest test-unit. 2. specify rake version in Gemfile for ruby 1.9.2 because the latest rake requires > ruby 1.9.3. --- .travis.yml | 1 + Gemfile | 2 ++ test/ietf_test.rb | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cba1a94..1f6b966 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,3 +7,4 @@ rvm: - jruby-19mode before_install: - git submodule update --init --recursive + - gem update bundler diff --git a/Gemfile b/Gemfile index f94b712..03cf238 100644 --- a/Gemfile +++ b/Gemfile @@ -3,4 +3,6 @@ source 'https://rubygems.org' # Specify your gem's dependencies in json-patch.gemspec gem 'minitest' gem 'coveralls', require: false +gem 'rake', '~> 10.5.0' if RUBY_VERSION < '1.9.3' +gem 'rake' if RUBY_VERSION >= '1.9.3' gemspec diff --git a/test/ietf_test.rb b/test/ietf_test.rb index 9ea2fa5..fc0c806 100644 --- a/test/ietf_test.rb +++ b/test/ietf_test.rb @@ -34,7 +34,7 @@ describe "JSON::Patch.new" do it "#{comment || spec['error'] || index}" do - target_doc = spec['doc'] if spec['doc'] + target_doc = Marshal.load(Marshal.dump(spec['doc'])) if spec['doc'] operations_doc = spec['patch'] if spec['patch'] expected_doc = spec['expected'] if spec['expected']