Skip to content

Commit

Permalink
Replaced get_element methods with JSONPointer package
Browse files Browse the repository at this point in the history
  • Loading branch information
YongHee-Kim committed Jun 26, 2024
1 parent efc6bd0 commit 064ab5b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 48 deletions.
48 changes: 4 additions & 44 deletions src/schema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

# Transform escaped characters in JPaths back to their original value.
function unescape_jpath(raw::String)
ret = replace(replace(raw, "~0" => "~"), "~1" => "/")
m = match(r"%([0-9A-F]{2})", ret)
if m !== nothing
for c in m.captures
ret = replace(ret, "%$(c)" => Char(parse(UInt8, "0x$(c)")))
end
end
return ret
end

function type_to_dict(x)
return Dict(name => getfield(x, name) for name in fieldnames(typeof(x)))
end
Expand All @@ -35,36 +23,6 @@ function update_id(uri::URIs.URI, s::String)
return URIs.URI(; els...)
end

function get_element(schema, path::AbstractString)
for element in split(path, "/"; keepempty = false)
schema = _recurse_get_element(schema, unescape_jpath(String(element)))
end
return schema
end

function _recurse_get_element(schema::Any, ::String)
return error(
"unmanaged type in ref resolution $(typeof(schema)): $(schema).",
)
end

function _recurse_get_element(schema::AbstractDict, element::String)
if !haskey(schema, element)
error("missing property '$(element)' in $(schema).")
end
return schema[element]
end

function _recurse_get_element(schema::AbstractVector, element::String)
index = tryparse(Int, element) # Remember that `index` is 0-indexed!
if index === nothing
error("expected integer array index instead of '$(element)'.")
elseif index >= length(schema)
error("item index $(index) is larger than array $(schema).")
end
return schema[index+1]
end

function get_remote_schema(uri::URIs.URI)
io = IOBuffer()
r = Downloads.request(string(uri); output = io, throw = false)
Expand All @@ -89,7 +47,8 @@ function find_ref(
if path == "" || path == "#" # This path refers to the root schema.
return id_map[string(uri)]
elseif startswith(path, "#/") # This path is a JPointer.
return get_element(id_map[string(uri)], path[3:end])
p = JSONPointer.Pointer(path; shift_index=true)
return get_pointer(id_map[string(uri)], p)
end
uri = update_id(uri, path)
els = type_to_dict(uri)
Expand All @@ -114,7 +73,8 @@ function find_ref(
).data
end
end
return get_element(id_map[string(uri2)], uri.fragment)
p = JSONPointer.Pointer(uri.fragment; shift_index = true)
return get_pointer(id_map[string(uri2)], p)
end

# Recursively find all "$ref" fields and resolve their path.
Expand Down
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ end

@testset "errors" begin
@test_throws(
ErrorException("missing property 'Foo' in $(Dict{String,Any}())."),
KeyError("Foo"),
JSONSchema.Schema("""{
"type": "object",
"properties": {"version": {"\$ref": "#/definitions/Foo"}},
Expand All @@ -274,23 +274,23 @@ end
)

@test_throws(
ErrorException("unmanaged type in ref resolution $(Int64): 1."),
ArgumentError("JSON pointer does not match the data-structure. I tried (and failed) to index 1 with the key: Foo"),
JSONSchema.Schema("""{
"type": "object",
"properties": {"version": {"\$ref": "#/definitions/Foo"}},
"definitions": 1
}""")
)
@test_throws(
ErrorException("expected integer array index instead of 'Foo'."),
ArgumentError("JSON pointer does not match the data-structure. I tried (and failed) to index Any[1, 2] with the key: Foo"),
JSONSchema.Schema("""{
"type": "object",
"properties": {"version": {"\$ref": "#/definitions/Foo"}},
"definitions": [1, 2]
}""")
)
@test_throws(
ErrorException("item index 3 is larger than array $(Any[1, 2])."),
BoundsError,
JSONSchema.Schema("""{
"type": "object",
"properties": {"version": {"\$ref": "#/definitions/3"}},
Expand Down

0 comments on commit 064ab5b

Please sign in to comment.