aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--spec/util_jsonpointer_spec.lua38
-rw-r--r--teal-src/util/jsonpointer.tl2
-rw-r--r--util/jsonpointer.lua2
3 files changed, 40 insertions, 2 deletions
diff --git a/spec/util_jsonpointer_spec.lua b/spec/util_jsonpointer_spec.lua
new file mode 100644
index 00000000..ce07c7a1
--- /dev/null
+++ b/spec/util_jsonpointer_spec.lua
@@ -0,0 +1,38 @@
+describe("util.jsonpointer", function()
+ local json, jp;
+ setup(function()
+ json = require "util.json";
+ jp = require "util.jsonpointer";
+ end)
+ describe("resolve()", function()
+ local example;
+ setup(function()
+ example = json.decode([[{
+ "foo": ["bar", "baz"],
+ "": 0,
+ "a/b": 1,
+ "c%d": 2,
+ "e^f": 3,
+ "g|h": 4,
+ "i\\j": 5,
+ "k\"l": 6,
+ " ": 7,
+ "m~n": 8
+ }]])
+ end)
+ it("works", function()
+ assert.same(example, jp.resolve(example, ""));
+ assert.same({ "bar", "baz" }, jp.resolve(example, "/foo"));
+ assert.same("bar", jp.resolve(example, "/foo/0"));
+ assert.same(0, jp.resolve(example, "/"));
+ assert.same(1, jp.resolve(example, "/a~1b"));
+ assert.same(2, jp.resolve(example, "/c%d"));
+ assert.same(3, jp.resolve(example, "/e^f"));
+ assert.same(4, jp.resolve(example, "/g|h"));
+ assert.same(5, jp.resolve(example, "/i\\j"));
+ assert.same(6, jp.resolve(example, "/k\"l"));
+ assert.same(7, jp.resolve(example, "/ "));
+ assert.same(8, jp.resolve(example, "/m~0n"));
+ end)
+ end)
+end)
diff --git a/teal-src/util/jsonpointer.tl b/teal-src/util/jsonpointer.tl
index 7b9db760..c21e1fbf 100644
--- a/teal-src/util/jsonpointer.tl
+++ b/teal-src/util/jsonpointer.tl
@@ -24,7 +24,7 @@ local function resolve_json_pointer(ref : table, path : string) : any, ptr_error
elseif idx is integer then
local i = tonumber(token)
if token == "-" then i = #ref + 1 end
- new_ref = ref[i]
+ new_ref = ref[i+1]
else
return nil, "invalid-table"
end
diff --git a/util/jsonpointer.lua b/util/jsonpointer.lua
index 7c657266..dd5ac9d6 100644
--- a/util/jsonpointer.lua
+++ b/util/jsonpointer.lua
@@ -20,7 +20,7 @@ local function resolve_json_pointer(ref, path)
if token == "-" then
i = #ref + 1
end
- new_ref = ref[i]
+ new_ref = ref[i + 1]
else
return nil, "invalid-table"
end