diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 1d75fc7..8872315 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -551,9 +551,9 @@ public: sch.erase(attr); } - const auto defaultAttr = sch.find("default"); - if (defaultAttr != sch.end()) { - defaultValue_ = defaultAttr.value(); + const auto default_attr = sch.find("default"); + if (default_attr != sch.end()) { + defaultValue_ = default_attr.value(); } for (auto &key : known_keywords) @@ -630,6 +630,11 @@ public: sch.erase(attr); } } + + void set_default_value(const json &default_value) + { + defaultValue_ = default_value; + } }; class string : public schema @@ -1276,6 +1281,17 @@ std::shared_ptr schema::make(json &schema, // so this is the origial URI for this reference, the $ref-value has thus be resolved from it auto id = uris.back().derive(attr.value().get()); sch = root->get_or_create_ref(id); + + const auto default_attr = schema.find("default"); + if (default_attr != schema.end()) { + if (type_schema *type_sch = dynamic_cast(sch.get())) { + // copy the referenced schema and modify the default value + auto schema_copy = std::make_shared(*type_sch); + schema_copy->set_default_value(default_attr.value()); + sch = schema_copy; + } + } + schema.erase(attr); } else { sch = std::make_shared(schema, root, uris); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8b45790..fd1c309 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -69,3 +69,7 @@ add_test(NAME binary-validation COMMAND binary-validation) add_executable(issue-149-entry-selection issue-149-entry-selection.cpp) target_link_libraries(issue-149-entry-selection PRIVATE nlohmann_json_schema_validator) add_test(NAME issue-149-entry-selection COMMAND issue-149-entry-selection) + +add_executable(issue-189-default-values issue-189-default-values.cpp) +target_link_libraries(issue-189-default-values nlohmann_json_schema_validator) +add_test(NAME issue-189-default-values COMMAND issue-189-default-values) diff --git a/test/issue-189-default-values.cpp b/test/issue-189-default-values.cpp new file mode 100644 index 0000000..0fb7cc5 --- /dev/null +++ b/test/issue-189-default-values.cpp @@ -0,0 +1,50 @@ +#include +#include + +using nlohmann::json; +using nlohmann::json_schema::json_validator; + +static const json rectangle_schema = R"( +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "A rectangle", + "properties": { + "width": { + "$ref": "#/definitions/length", + "default": 20 + }, + "height": { + "$ref": "#/definitions/length" + } + }, + "definitions": { + "length": { + "type": "integer", + "default": 10 + } + }, + "additionalProperties": false, + "type": "object" +})"_json; + +int main(void) +{ + json_validator validator{}; + validator.set_root_schema(rectangle_schema); + + { + json empty_rectangle = R"({})"_json; + + const auto default_patch = validator.validate(empty_rectangle); + const auto actual = empty_rectangle.patch(default_patch); + + // height must be 10 according to the default specified in the length definition while width must be 10 overridden by the width element + const auto expected = R"({"height":10,"width":20})"_json; + if (actual != expected) { + std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl; + return 1; + } + } + + return 0; +}