diff --git a/common/json-schema.cpp b/common/json-schema.cpp index da7e091234..8d0ebae8c1 100644 --- a/common/json-schema.cpp +++ b/common/json-schema.cpp @@ -104,7 +104,7 @@ class common_schema_builder { if (ref.compare(0, 2, "#/") != 0) { fail(path, "unsupported $ref " + ref + ", only references into the same document are supported"); } - if (doc_.refs.find(ref) == doc_.refs.end() && refs_.find(ref) == refs_.end()) { + if (refs_.find(ref) == refs_.end()) { // reserve the key first, so that a cycle back to this $ref stops here refs_[ref] = nullptr; refs_[ref] = build_node(resolve_ref(ref, path), ref); @@ -343,10 +343,6 @@ common_schema_document common_schema_from_json(const common_json & schema) { return doc; } -common_schema_ptr common_schema_from_json(const common_json & schema, common_schema_document & doc) { - return common_schema_builder(schema, doc).build(); -} - static common_schema::value_type json_type(const common_json & value) { if (value.is_null()) { return common_schema::TYPE_NULL; diff --git a/common/json-schema.h b/common/json-schema.h index a5029081a2..cee229e6ca 100644 --- a/common/json-schema.h +++ b/common/json-schema.h @@ -196,8 +196,3 @@ using common_schema_document_ptr = std::shared_ptr // Throws std::runtime_error when the schema falls outside the supported subset. common_schema_document common_schema_from_json(const common_json & schema); - -// Builds a schema that belongs to a document built earlier, e.g. one property of it. -// A $ref it cannot resolve on its own is looked up in doc.refs, the targets it resolves itself are added there. -// doc is unchanged when the schema is rejected. -common_schema_ptr common_schema_from_json(const common_json & schema, common_schema_document & doc); diff --git a/tests/test-json-schema.cpp b/tests/test-json-schema.cpp index b740cc4a6e..3e92010637 100644 --- a/tests/test-json-schema.cpp +++ b/tests/test-json-schema.cpp @@ -367,29 +367,6 @@ static void test_ref(testing & t) { t.assert_true("target", r.target == doc.refs.at("#/$defs/t").get()); as(t, r.target, "target"); }); - - t.test("a schema parsed into a document shares its refs", [](testing & t) { - auto doc = parse(R"({"properties": {"a": {"$ref": "#/$defs/t"}}, "$defs": {"t": {"type": "boolean"}}})"); - auto node = common_schema_from_json(common_json::parse(R"({"items": {"$ref": "#/$defs/t"}})"), doc); - const auto & a = as(t, node.get(), "node"); - const auto & r = as(t, a.items.get(), "items"); - t.assert_true("shared target", r.target == doc.refs.at("#/$defs/t").get()); - t.assert_equal("refs", (size_t) 1, doc.refs.size()); - - auto added = common_schema_from_json(common_json::parse(R"({"$ref": "#/$defs/u", "$defs": {"u": {"type": "null"}}})"), doc); - as(t, as(t, added.get(), "added").target, "target"); - t.assert_equal("refs", (size_t) 2, doc.refs.size()); - }); - - t.test("a rejected schema leaves the document unchanged", [](testing & t) { - common_schema_document doc; - try { - common_schema_from_json(common_json::parse(R"({"allOf": [{"$ref": "#/$defs/t"}, {"type": "x"}], "$defs": {"t": {"type": "null"}}})"), doc); - t.assert_true("rejected", false); - } catch (const std::runtime_error &) { - t.assert_true("no refs", doc.refs.empty()); - } - }); } static void test_may_be_string(testing & t) {