From 03af1b5e1e8b24ee436e3c2d5beca390ed3840e0 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher
Date: Thu, 9 Apr 2020 15:34:38 +0200 Subject: [PATCH] fix #100: only look for "unknown keyword" if the uri contains a pointer. --- src/json-validator.cpp | 19 ++++++++++++------- test/issue-100/CMakeLists.txt | 6 ++++++ test/issue-100/instance.json | 4 ++++ test/issue-100/schema.json | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 test/issue-100/CMakeLists.txt create mode 100644 test/issue-100/instance.json create mode 100644 test/issue-100/schema.json diff --git a/src/json-validator.cpp b/src/json-validator.cpp index b82b19d..54a94c7 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -177,14 +177,19 @@ public: return schema->second; // referencing an unknown keyword, turn it into schema - try { - auto &subschema = file.unknown_keywords.at(uri.pointer()); // null is returned if not existing - auto s = schema::make(subschema, this, {}, {{uri}}); // A JSON Schema MUST be an object or a boolean. - if (s) { // nullptr if invalid schema, e.g. null - file.unknown_keywords.erase(uri.fragment()); - return s; + // + // an unknown keyword can only be referenced by a json-pointer, + // not by a plain name fragment + if (uri.pointer() != "") { + try { + auto &subschema = file.unknown_keywords.at(uri.pointer()); // null is returned if not existing + auto s = schema::make(subschema, this, {}, {{uri}}); // A JSON Schema MUST be an object or a boolean. + if (s) { // nullptr if invalid schema, e.g. null + file.unknown_keywords.erase(uri.fragment()); + return s; + } + } catch (nlohmann::detail::out_of_range &) { // at() did not find it } - } catch (nlohmann::detail::out_of_range &) { // at() did not find it } // get or create a schema_ref diff --git a/test/issue-100/CMakeLists.txt b/test/issue-100/CMakeLists.txt new file mode 100644 index 0000000..06a754f --- /dev/null +++ b/test/issue-100/CMakeLists.txt @@ -0,0 +1,6 @@ +add_test_simple_schema(Issue::100 + ${CMAKE_CURRENT_SOURCE_DIR}/schema.json + ${CMAKE_CURRENT_SOURCE_DIR}/instance.json) +set_tests_properties(Issue::100 + PROPERTIES + WILL_FAIL 1) diff --git a/test/issue-100/instance.json b/test/issue-100/instance.json new file mode 100644 index 0000000..0f8d8df --- /dev/null +++ b/test/issue-100/instance.json @@ -0,0 +1,4 @@ +{ + "Prop1": 1, + "Prop2": [] +} diff --git a/test/issue-100/schema.json b/test/issue-100/schema.json new file mode 100644 index 0000000..3230714 --- /dev/null +++ b/test/issue-100/schema.json @@ -0,0 +1,15 @@ +{ + "$id": "http://xxx.local/schemas/mySchema.json", + "$schema": "http://json-schema.org/draft-07/schema#", + + "type": "object", + "properties": { + "Prop1": { + "type": "integer", + "$comment": "Just a comment" + }, + "Prop2": { + "$ref": "#random_ref" + } + } +}