From 940262ceae42848b8bdab1e07fef4c2385b701c3 Mon Sep 17 00:00:00 2001 From: Patrick Boettcher Date: Mon, 23 Mar 2020 22:43:42 +0100 Subject: [PATCH] fix #93 by returning default-values for refs and also return them for root-schemas --- src/json-validator.cpp | 4 +- test/issue-93/CMakeLists.txt | 7 ++++ test/issue-93/blueprints.schema.json | 11 ++++++ test/issue-93/components.schema.json | 13 +++++++ test/issue-93/issue-93.cpp | 55 +++++++++++++++++++++++++++ test/issue-93/types/color.schema.json | 4 ++ test/test-pipe-in.sh | 2 +- 7 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 test/issue-93/CMakeLists.txt create mode 100644 test/issue-93/blueprints.schema.json create mode 100644 test/issue-93/components.schema.json create mode 100644 test/issue-93/issue-93.cpp create mode 100644 test/issue-93/types/color.schema.json diff --git a/src/json-validator.cpp b/src/json-validator.cpp index c684578..cd690cd 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -75,7 +75,7 @@ class schema_ref : public schema const json &defaultValue(const json::json_pointer &ptr, const json &instance, error_handler &e) const override { if (target_) - target_->defaultValue(ptr, instance, e); + return target_->defaultValue(ptr, instance, e); else e.error(ptr, instance, "unresolved schema-reference " + id_); @@ -241,7 +241,7 @@ public: const json &defaultValue(const json::json_pointer &ptr, const json &instance, error_handler &e) const override { if (root_) - root_->defaultValue(ptr, instance, e); + return root_->defaultValue(ptr, instance, e); else e.error(ptr, "", "no root schema has yet been set for validating an instance"); diff --git a/test/issue-93/CMakeLists.txt b/test/issue-93/CMakeLists.txt new file mode 100644 index 0000000..eaccc41 --- /dev/null +++ b/test/issue-93/CMakeLists.txt @@ -0,0 +1,7 @@ + +add_executable(issue-93 issue-93.cpp) +target_link_libraries(issue-93 nlohmann_json_schema_validator) + +add_test(NAME issue-93 + COMMAND issue-93 + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/test/issue-93/blueprints.schema.json b/test/issue-93/blueprints.schema.json new file mode 100644 index 0000000..c35c0f4 --- /dev/null +++ b/test/issue-93/blueprints.schema.json @@ -0,0 +1,11 @@ +{ + "type":"array", + "items": { + "type":"object", + "properties": { + "renderable": { + "$ref":"/components.schema.json#/Renderable" + } + } + } +} diff --git a/test/issue-93/components.schema.json b/test/issue-93/components.schema.json new file mode 100644 index 0000000..3aaeea9 --- /dev/null +++ b/test/issue-93/components.schema.json @@ -0,0 +1,13 @@ +{ + "Renderable": { + "type":"object", + "properties": { + "fg":{ + "$ref":"/types/color.schema.json" + }, + "bg":{ + "$ref":"/types/color.schema.json" + } + } + } +} diff --git a/test/issue-93/issue-93.cpp b/test/issue-93/issue-93.cpp new file mode 100644 index 0000000..87ba599 --- /dev/null +++ b/test/issue-93/issue-93.cpp @@ -0,0 +1,55 @@ +#include + +#include +#include + +using nlohmann::json; +using nlohmann::json_uri; +using nlohmann::json_schema::json_validator; + +static const auto expected_patch = R"( +[{"op":"add","path":"/0/renderable/bg","value":"Black"}] +)"_json; + +static const auto instance = R"( +[ + { + "name":"player", + "renderable": { + "fg":"White" + } + } +] +)"_json; + +static void loader(const json_uri &uri, json &schema) +{ + std::string filename = "./" + uri.path(); + std::ifstream lf(filename); + if (!lf.good()) + throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename); + try { + lf >> schema; + } catch (const std::exception &e) { + throw e; + } +} + +int main(void) +{ + json_validator validator(loader); + + std::fstream f("blueprints.schema.json"); + + json schema; + f >> schema; + + validator.set_root_schema(schema); + + auto missing_default_patch = validator.validate(instance); + + std::cerr << missing_default_patch << "\n"; + std::cerr << expected_patch << "\n"; + + return missing_default_patch != expected_patch; +} diff --git a/test/issue-93/types/color.schema.json b/test/issue-93/types/color.schema.json new file mode 100644 index 0000000..11d3092 --- /dev/null +++ b/test/issue-93/types/color.schema.json @@ -0,0 +1,4 @@ +{ + "type":"string", + "default":"Black" +} diff --git a/test/test-pipe-in.sh b/test/test-pipe-in.sh index ac57a77..6071277 100755 --- a/test/test-pipe-in.sh +++ b/test/test-pipe-in.sh @@ -1,7 +1,7 @@ #!/bin/bash # all argument are considered as a program to call (with its arguments), -# the last argument is read from via '<' +# the last argument is read from stdin via '<' set -e