Skip to content

Commit 5e5918d

Browse files
martin-kochpboettch
authored andcommitted
distinguish between schema_ref and type_schema
1 parent 6cb4978 commit 5e5918d

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/json-validator.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -1280,9 +1280,14 @@ std::shared_ptr<schema> schema::make(json &schema,
12801280
// special case where break draft-7 and allow overriding of properties when a $ref is used
12811281
attr = schema.find("default");
12821282
if (attr != schema.end()) {
1283-
// copy the referenced schema and modify the default value
1284-
sch = std::make_shared<schema_ref>(*dynamic_cast<schema_ref *>(sch.get()));
1285-
sch->set_default_value(attr.value());
1283+
// copy the referenced schema depending on the underlying type and modify the default value
1284+
if (auto *ref_sch = dynamic_cast<schema_ref *>(sch.get())) {
1285+
sch = std::make_shared<schema_ref>(*ref_sch);
1286+
sch->set_default_value(attr.value());
1287+
} else if (auto *type_sch = dynamic_cast<type_schema *>(sch.get())) {
1288+
sch = std::make_shared<type_schema>(*type_sch);
1289+
sch->set_default_value(attr.value());
1290+
}
12861291
schema.erase(attr);
12871292
}
12881293
} else {

test/issue-189-default-values.cpp

+42-3
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,52 @@ using nlohmann::json;
55
using nlohmann::json_uri;
66
using nlohmann::json_schema::json_validator;
77

8-
static const json quad_schema = R"(
8+
static const json rectangle_schema = R"(
99
{
1010
"$schema": "http://json-schema.org/draft-07/schema#",
1111
"properties": {
1212
"width": {
1313
"$ref": "#/definitions/length",
1414
"default": 20
1515
},
16+
"height": {
17+
"$ref": "#/definitions/length"
18+
}
19+
},
20+
"definitions": {
21+
"length": {
22+
"type": "integer",
23+
"default": 10
24+
}
25+
}
26+
})"_json;
27+
28+
static const json quad_schema = R"(
29+
{
30+
"$schema": "http://json-schema.org/draft-07/schema#",
31+
"properties": {
32+
"width": {
33+
"$ref": "#/properties/height",
34+
"default": 20
35+
},
1636
"height": {
1737
"$ref": "#/definitions/length"
1838
},
1939
"depth": {
2040
"$ref": "default_schema#/definitions/defaultLength"
2141
},
2242
"time": {
23-
"$ref": "#/properties/width"
43+
"$ref": "#/definitions/time"
2444
}
2545
},
2646
"definitions": {
2747
"length": {
2848
"$ref": "default_schema#/definitions/defaultLength",
2949
"default": 10
50+
},
51+
"time": {
52+
"type": "integer",
53+
"default": 15
3054
}
3155
}
3256
})"_json;
@@ -52,14 +76,29 @@ int main(void)
5276

5377
validator.set_root_schema(quad_schema);
5478

79+
{
80+
json empty_quad = R"({})"_json;
81+
82+
const auto default_patch = validator.validate(empty_quad);
83+
const auto actual = empty_quad.patch(default_patch);
84+
85+
const auto expected = R"({"height":10,"width":20,"depth":5,"time":15})"_json;
86+
if (actual != expected) {
87+
std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl;
88+
return 1;
89+
}
90+
}
91+
92+
validator.set_root_schema(rectangle_schema);
93+
5594
{
5695
json empty_rectangle = R"({})"_json;
5796

5897
const auto default_patch = validator.validate(empty_rectangle);
5998
const auto actual = empty_rectangle.patch(default_patch);
6099

61100
// height must be 10 according to the default specified in the length definition while width must be 10 overridden by the width element
62-
const auto expected = R"({"height":10,"width":20,"depth":5,"time":20})"_json;
101+
const auto expected = R"({"height":10,"width":20})"_json;
63102
if (actual != expected) {
64103
std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl;
65104
return 1;

0 commit comments

Comments
 (0)