diff --git a/src/schema.jl b/src/schema.jl index f5e2252..fa29f17 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -192,6 +192,16 @@ end build_id_map!(::AbstractDict, ::Any, ::URIs.URI) = nothing +_copy_schema(x) = deepcopy(x) + +function _copy_schema(schema::Vector) + return map(_copy_schema, schema) +end + +function _copy_schema(schema::Dict{K}) where {K} + return Dict{K,Any}(k => _copy_schema(v) for (k, v) in schema) +end + function build_id_map!( id_map::AbstractDict, schema::AbstractVector, @@ -257,7 +267,7 @@ struct Schema ) parent_dir = parentFileDirectory end - schema = deepcopy(schema) # Ensure we don't modify the user's data! + schema = _copy_schema(schema) # Ensure we don't modify the user's data! id_map = build_id_map(schema) resolve_refs!(schema, URIs.URI(), id_map, parent_dir) return new(schema) diff --git a/test/runtests.jl b/test/runtests.jl index 41ae5d1..4b6a33b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -228,6 +228,18 @@ end schema_2 = JSONSchema.Schema(false) @test typeof(schema_2) == Schema @test typeof(schema_2.data) == Bool + + schema_dict = Dict( + "properties" => + Dict("age" => Dict("\$ref" => "#/\$defs/positiveNumber")), + "\$defs" => Dict( + "positiveNumber" => Dict("type" => "number", "minimum" => 0), + ), + ) + schema = JSONSchema.Schema(schema_dict) + @test isvalid(schema, Dict("age" => 1)) + @test !isvalid(schema, Dict("age" => -1)) + @test schema_dict["properties"]["age"]["\$ref"] == "#/\$defs/positiveNumber" end @testset "Base.show" begin