diff --git a/src/json-validator.cpp b/src/json-validator.cpp index c67862d..1a801bd 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -1280,9 +1280,14 @@ std::shared_ptr schema::make(json &schema, // special case where break draft-7 and allow overriding of properties when a $ref is used attr = schema.find("default"); if (attr != schema.end()) { - // copy the referenced schema and modify the default value - sch = std::make_shared(*dynamic_cast(sch.get())); - sch->set_default_value(attr.value()); + // copy the referenced schema depending on the underlying type and modify the default value + if (auto *ref_sch = dynamic_cast(sch.get())) { + sch = std::make_shared(*ref_sch); + sch->set_default_value(attr.value()); + } else if (auto *type_sch = dynamic_cast(sch.get())) { + sch = std::make_shared(*type_sch); + sch->set_default_value(attr.value()); + } schema.erase(attr); } } else { diff --git a/test/issue-189-default-values.cpp b/test/issue-189-default-values.cpp index d0b32ad..e7232d3 100644 --- a/test/issue-189-default-values.cpp +++ b/test/issue-189-default-values.cpp @@ -5,7 +5,7 @@ using nlohmann::json; using nlohmann::json_uri; using nlohmann::json_schema::json_validator; -static const json quad_schema = R"( +static const json rectangle_schema = R"( { "$schema": "http://json-schema.org/draft-07/schema#", "properties": { @@ -13,6 +13,26 @@ static const json quad_schema = R"( "$ref": "#/definitions/length", "default": 20 }, + "height": { + "$ref": "#/definitions/length" + } + }, + "definitions": { + "length": { + "type": "integer", + "default": 10 + } + } +})"_json; + +static const json quad_schema = R"( +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "properties": { + "width": { + "$ref": "#/properties/height", + "default": 20 + }, "height": { "$ref": "#/definitions/length" }, @@ -20,13 +40,17 @@ static const json quad_schema = R"( "$ref": "default_schema#/definitions/defaultLength" }, "time": { - "$ref": "#/properties/width" + "$ref": "#/definitions/time" } }, "definitions": { "length": { "$ref": "default_schema#/definitions/defaultLength", "default": 10 + }, + "time": { + "type": "integer", + "default": 15 } } })"_json; @@ -52,6 +76,21 @@ int main(void) validator.set_root_schema(quad_schema); + { + json empty_quad = R"({})"_json; + + const auto default_patch = validator.validate(empty_quad); + const auto actual = empty_quad.patch(default_patch); + + const auto expected = R"({"height":10,"width":20,"depth":5,"time":15})"_json; + if (actual != expected) { + std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl; + return 1; + } + } + + validator.set_root_schema(rectangle_schema); + { json empty_rectangle = R"({})"_json; @@ -59,7 +98,7 @@ int main(void) 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,"depth":5,"time":20})"_json; + 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;