Skip to content

Expanding $refs fails if Dict is strictly typed #81

Description

@piever

First of all, thanks for this package, I'm finding it very helpful.

I've encountered a small bug (easy to work around, but it did take me a little bit to figure out what was happening). If a dictionary containing a $ref is too strictly typed, the machinery in JSONSchema fails when trying to expand it to its definition.

julia> using JSONSchema

julia> d = Dict(
           "properties" => Dict("age" => Dict("\$ref" => "#/\$defs/positiveNumber")),
           "\$defs" => Dict("positiveNumber" => Dict("type" => "number", "minimum" => 0))
       );

julia> JSONSchema.Schema(d)
ERROR: MethodError: Cannot `convert` an object of type Dict{String, Any} to an object of type String

Full stacktrace
ERROR: MethodError: Cannot `convert` an object of type Dict{String, Any} to an object of type String
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  convert(::Type{String}, ::JSON.PtrString)
   @ JSON ~/.julia/packages/JSON/gMdcG/src/lazy.jl:428
  convert(::Type{String}, ::String)
   @ Base essentials.jl:461
  convert(::Type{T}, ::T) where T<:AbstractString
   @ Base strings/basic.jl:232
  ...

Stacktrace:
 [1] setindex!(h::Dict{String, String}, v0::Dict{String, Any}, key::String)
   @ Base ./dict.jl:355
 [2] resolve_refs!(schema::Dict{String, String}, uri::URIs.URI, id_map::Dict{String, Any}, parent_dir::String)
   @ JSONSchema ~/.julia/dev/JSONSchema/src/schema.jl:164
 [3] resolve_refs!(schema::Dict{String, Dict{String, String}}, uri::URIs.URI, id_map::Dict{String, Any}, parent_dir::String)
   @ JSONSchema ~/.julia/dev/JSONSchema/src/schema.jl:181
 [4] resolve_refs!(schema::Dict{String, Dict{String}}, uri::URIs.URI, id_map::Dict{String, Any}, parent_dir::String)
   @ JSONSchema ~/.julia/dev/JSONSchema/src/schema.jl:181
 [5] _
   @ ~/.julia/dev/JSONSchema/src/schema.jl:262 [inlined]
 [6] Schema(schema::Dict{String, Dict{String}})
   @ JSONSchema ~/.julia/dev/JSONSchema/src/schema.jl:249
 [7] top-level scope
   @ REPL[13]:1

Widening the container type fixes the issue:

julia> d = Dict(
           "properties" => Dict("age" => Dict{String, Any}("\$ref" => "#/\$defs/positiveNumber")),
           "\$defs" => Dict("positiveNumber" => Dict("type" => "number", "minimum" => 0))
       );

julia> JSONSchema.Schema(d)
A JSONSchema

but I imagine this could be done automatically by JSONSchema when copying the user-provided nested dict.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions