fix default is empty object
This commit is contained in:
parent
ef05be7efa
commit
561571179f
@ -33,7 +33,7 @@ using namespace nlohmann::json_schema;
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
static const json EmptyDefault{};
|
static const json EmptyDefault = nullptr;
|
||||||
|
|
||||||
class schema
|
class schema
|
||||||
{
|
{
|
||||||
@ -402,7 +402,7 @@ bool logical_combination<oneOf>::is_validate_complete(const json &instance, cons
|
|||||||
|
|
||||||
class type_schema : public schema
|
class type_schema : public schema
|
||||||
{
|
{
|
||||||
json defaultValue_{};
|
json defaultValue_ = EmptyDefault;
|
||||||
std::vector<std::shared_ptr<schema>> type_;
|
std::vector<std::shared_ptr<schema>> type_;
|
||||||
std::pair<bool, json> enum_, const_;
|
std::pair<bool, json> enum_, const_;
|
||||||
std::vector<std::shared_ptr<schema>> logic_;
|
std::vector<std::shared_ptr<schema>> logic_;
|
||||||
@ -882,7 +882,7 @@ class object : public schema
|
|||||||
const auto finding = instance.find(prop.first);
|
const auto finding = instance.find(prop.first);
|
||||||
if (instance.end() == finding) { // if the prop is not in the instance
|
if (instance.end() == finding) { // if the prop is not in the instance
|
||||||
const auto &defaultValue = prop.second->defaultValue(ptr, instance, e);
|
const auto &defaultValue = prop.second->defaultValue(ptr, instance, e);
|
||||||
if (!defaultValue.empty()) { // if default value is available
|
if (!defaultValue.is_null()) { // if default value is available
|
||||||
patch.add((ptr / prop.first), defaultValue);
|
patch.add((ptr / prop.first), defaultValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,10 +19,11 @@ static const json person_schema = R"(
|
|||||||
"minimum": 2,
|
"minimum": 2,
|
||||||
"maximum": 200
|
"maximum": 200
|
||||||
},
|
},
|
||||||
"address":{
|
"address": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties":{
|
"default": {},
|
||||||
"street":{
|
"properties": {
|
||||||
|
"street": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"default": "Abbey Road"
|
"default": "Abbey Road"
|
||||||
}
|
}
|
||||||
@ -40,17 +41,17 @@ static const json person_schema = R"(
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
json_validator validator{};
|
json_validator validator{};
|
||||||
|
validator.set_root_schema(person_schema);
|
||||||
|
|
||||||
|
{
|
||||||
// add address which is optional that should generate a diff containing a default street
|
// add address which is optional that should generate a diff containing a default street
|
||||||
json person_missing_address = R"({
|
json person_emtpy_address = R"({
|
||||||
"name": "Hans",
|
"name": "Hans",
|
||||||
"age": 69,
|
"age": 69,
|
||||||
"address": {}
|
"address": {}
|
||||||
})"_json;
|
})"_json;
|
||||||
|
|
||||||
validator.set_root_schema(person_schema);
|
const auto default_patch = validator.validate(person_emtpy_address);
|
||||||
|
|
||||||
const auto default_patch = validator.validate(person_missing_address);
|
|
||||||
|
|
||||||
if (!default_patch.is_array()) {
|
if (!default_patch.is_array()) {
|
||||||
std::cerr << "Patch with defaults is expected to be an array" << std::endl;
|
std::cerr << "Patch with defaults is expected to be an array" << std::endl;
|
||||||
@ -95,6 +96,59 @@ int main(void)
|
|||||||
std::cerr << "Patch with defaults contains wrong value" << std::endl;
|
std::cerr << "Patch with defaults contains wrong value" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// add address which is optional that should generate a diff containing a default street
|
||||||
|
json person_missing_address = R"({
|
||||||
|
"name": "Hans",
|
||||||
|
"age": 69
|
||||||
|
})"_json;
|
||||||
|
|
||||||
|
const auto default_patch = validator.validate(person_missing_address);
|
||||||
|
|
||||||
|
if (!default_patch.is_array()) {
|
||||||
|
std::cerr << "Patch with defaults is expected to be an array" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (default_patch.size() != 1) {
|
||||||
|
std::cerr << "Patch with defaults is expected to contain one opperation" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto &single_op = default_patch[0];
|
||||||
|
|
||||||
|
if (!single_op.contains("op")) {
|
||||||
|
std::cerr << "Patch with defaults is expected to contain opperation entry" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (single_op["op"].get<std::string>() != "add") {
|
||||||
|
std::cerr << "Patch with defaults is expected to contain add opperation" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!single_op.contains("path")) {
|
||||||
|
std::cerr << "Patch with defaults is expected to contain a path" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto &readPath = single_op["path"].get<std::string>();
|
||||||
|
if (readPath != "/address") {
|
||||||
|
std::cerr << "Patch with defaults contains wrong path. It is " << readPath << " and should be "
|
||||||
|
<< "/address" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!single_op.contains("value")) {
|
||||||
|
std::cerr << "Patch with defaults is expected to contain a value" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !single_op["value"].is_object() || !single_op["value"].empty()) {
|
||||||
|
std::cerr << "Patch with defaults contains wrong value" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user