From ca26ec4c86506ebdcb7df1441bb824c79e93c67e Mon Sep 17 00:00:00 2001 From: Patrick Boettcher
Date: Thu, 9 Apr 2020 11:01:25 +0200
Subject: [PATCH] fix #98: catch out-of-range-exception only
Leave other exception go through - real errors.
---
src/json-validator.cpp | 12 +++++++-----
test/CMakeLists.txt | 4 ++++
test/issue-98.cpp | 10 ++++++++++
3 files changed, 21 insertions(+), 5 deletions(-)
create mode 100644 test/issue-98.cpp
diff --git a/src/json-validator.cpp b/src/json-validator.cpp
index cd690cd..b82b19d 100644
--- a/src/json-validator.cpp
+++ b/src/json-validator.cpp
@@ -178,11 +178,13 @@ public:
// referencing an unknown keyword, turn it into schema
try {
- auto &subschema = file.unknown_keywords.at(uri.pointer());
- auto s = schema::make(subschema, this, {}, {{uri}});
- file.unknown_keywords.erase(uri.fragment());
- return s;
- } catch (...) {
+ 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
}
// get or create a schema_ref
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 3371cb9..0de2a05 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -39,6 +39,10 @@ add_executable(issue-25-default-values issue-25-default-values.cpp)
target_link_libraries(issue-25-default-values nlohmann_json_schema_validator)
add_test(NAME issue-25-default-values COMMAND issue-25-default-values)
+add_executable(issue-98 issue-98.cpp)
+target_link_libraries(issue-98 nlohmann_json_schema_validator)
+add_test(NAME issue-98-erase-exception-unknown-keywords COMMAND issue-98)
+
# Unit test for string format checks
add_executable("string-format-check-test" "string-format-check-test.cpp")
target_include_directories("string-format-check-test" PRIVATE "${PROJECT_SOURCE_DIR}/src/")
diff --git a/test/issue-98.cpp b/test/issue-98.cpp
new file mode 100644
index 0000000..7aafe47
--- /dev/null
+++ b/test/issue-98.cpp
@@ -0,0 +1,10 @@
+#include