distinguish between schema_ref and type_schema
This commit is contained in:
parent
6cb497822b
commit
5e5918d2ae
@ -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
|
// special case where break draft-7 and allow overriding of properties when a $ref is used
|
||||||
attr = schema.find("default");
|
attr = schema.find("default");
|
||||||
if (attr != schema.end()) {
|
if (attr != schema.end()) {
|
||||||
// copy the referenced schema and modify the default value
|
// copy the referenced schema depending on the underlying type and modify the default value
|
||||||
sch = std::make_shared<schema_ref>(*dynamic_cast<schema_ref *>(sch.get()));
|
if (auto *ref_sch = dynamic_cast<schema_ref *>(sch.get())) {
|
||||||
|
sch = std::make_shared<schema_ref>(*ref_sch);
|
||||||
sch->set_default_value(attr.value());
|
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);
|
schema.erase(attr);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -5,7 +5,7 @@ using nlohmann::json;
|
|||||||
using nlohmann::json_uri;
|
using nlohmann::json_uri;
|
||||||
using nlohmann::json_schema::json_validator;
|
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#",
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
"properties": {
|
"properties": {
|
||||||
@ -13,6 +13,26 @@ static const json quad_schema = R"(
|
|||||||
"$ref": "#/definitions/length",
|
"$ref": "#/definitions/length",
|
||||||
"default": 20
|
"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": {
|
"height": {
|
||||||
"$ref": "#/definitions/length"
|
"$ref": "#/definitions/length"
|
||||||
},
|
},
|
||||||
@ -20,13 +40,17 @@ static const json quad_schema = R"(
|
|||||||
"$ref": "default_schema#/definitions/defaultLength"
|
"$ref": "default_schema#/definitions/defaultLength"
|
||||||
},
|
},
|
||||||
"time": {
|
"time": {
|
||||||
"$ref": "#/properties/width"
|
"$ref": "#/definitions/time"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"definitions": {
|
||||||
"length": {
|
"length": {
|
||||||
"$ref": "default_schema#/definitions/defaultLength",
|
"$ref": "default_schema#/definitions/defaultLength",
|
||||||
"default": 10
|
"default": 10
|
||||||
|
},
|
||||||
|
"time": {
|
||||||
|
"type": "integer",
|
||||||
|
"default": 15
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})"_json;
|
})"_json;
|
||||||
@ -52,6 +76,21 @@ int main(void)
|
|||||||
|
|
||||||
validator.set_root_schema(quad_schema);
|
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;
|
json empty_rectangle = R"({})"_json;
|
||||||
|
|
||||||
@ -59,7 +98,7 @@ int main(void)
|
|||||||
const auto actual = empty_rectangle.patch(default_patch);
|
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
|
// 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) {
|
if (actual != expected) {
|
||||||
std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl;
|
std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user