Support override of default value when using referenced schemas (#189)
This commit is contained in:
parent
0d563960cd
commit
686f3b1cc5
@ -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> 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<std::string>());
|
||||
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<type_schema *>(sch.get())) {
|
||||
// copy the referenced schema and modify the default value
|
||||
auto schema_copy = std::make_shared<type_schema>(*type_sch);
|
||||
schema_copy->set_default_value(default_attr.value());
|
||||
sch = schema_copy;
|
||||
}
|
||||
}
|
||||
|
||||
schema.erase(attr);
|
||||
} else {
|
||||
sch = std::make_shared<type_schema>(schema, root, uris);
|
||||
|
||||
@ -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)
|
||||
|
||||
50
test/issue-189-default-values.cpp
Normal file
50
test/issue-189-default-values.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json-schema.hpp>
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user