distinguish between schema_ref and type_schema

This commit is contained in:
Martin KOCH 2021-12-13 08:58:23 +01:00 committed by Patrick Boettcher
parent 6cb497822b
commit 5e5918d2ae
2 changed files with 50 additions and 6 deletions

View File

@ -1280,9 +1280,14 @@ std::shared_ptr<schema> 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<schema_ref>(*dynamic_cast<schema_ref *>(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<schema_ref *>(sch.get())) {
sch = std::make_shared<schema_ref>(*ref_sch);
sch->set_default_value(attr.value());
} else if (auto *type_sch = dynamic_cast<type_schema *>(sch.get())) {
sch = std::make_shared<type_schema>(*type_sch);
sch->set_default_value(attr.value());
}
schema.erase(attr);
}
} else {

View File

@ -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;