Skip to content

Commit 63a6208

Browse files
committed
Fix incorrect JSON Patch output for root path
1 parent fd35c01 commit 63a6208

File tree

6 files changed

+16
-5
lines changed

6 files changed

+16
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ gem install json-diff # Or `gem 'json-diff'` in your Gemfile.
99
```ruby
1010
require 'json-diff'
1111
JsonDiff.diff(1, 2)
12-
#> [{:op => :replace, :path => "/", :value => 2}]
12+
#> [{:op => :replace, :path => "", :value => 2}]
1313
```
1414

1515
Outputs [RFC6902][]. Look at [hana][] for a JSON patch algorithm that can use this output.

lib/json-diff/diff.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module JsonDiff
22

33
def self.diff(before, after, opts = {})
4-
path = opts[:path] || '/'
4+
path = opts[:path] || ''
55
include_addition = (opts[:additions] == nil) ? true : opts[:additions]
66
include_moves = (opts[:moves] == nil) ? true : opts[:moves]
77
include_was = (opts[:include_was] == nil) ? false : opts[:include_was]

lib/json-diff/operation.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module JsonDiff
33
# Convert a list of strings or numbers to an RFC6901 JSON pointer.
44
# http://tools.ietf.org/html/rfc6901
55
def self.json_pointer(path)
6+
return "" if path == []
7+
68
escaped_path = path.map do |key|
79
if key.is_a?(String)
810
key.gsub('~', '~0')
@@ -17,7 +19,7 @@ def self.json_pointer(path)
1719

1820
# Add a key to a JSON pointer.
1921
def self.extend_json_pointer(pointer, key)
20-
if pointer == '/'
22+
if pointer == ''
2123
json_pointer([key])
2224
else
2325
pointer + json_pointer([key])

lib/json-diff/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JsonDiff
2-
VERSION = '0.3.0'
2+
VERSION = '0.3.1'
33
end

spec/json-diff/diff_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@
8484
])
8585
end
8686

87+
# Trans-type
88+
89+
it "should be able to diff two objects of mixed type" do
90+
diff = JsonDiff.diff(0, "0", include_was: true)
91+
expect(diff).to eql([
92+
{'op' => 'replace', 'path' => '', 'was' => 0, 'value' => "0"}
93+
])
94+
end
95+
8796
# Options
8897

8998
it "should be able to diff two integer arrays with original indices" do

spec/json-diff/operation_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe JsonDiff do
44
it "should convert the root to a JSON pointer" do
55
json_pointer = JsonDiff.json_pointer([])
6-
expect(json_pointer).to eql("/")
6+
expect(json_pointer).to eql("")
77
end
88

99
it "should convert a path to a JSON pointer" do

0 commit comments

Comments
 (0)