mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2025-01-03 05:22:24 +08:00
throw exception when deserialize json which not include required field
This commit is contained in:
parent
c7e1c2375b
commit
7b92550523
@ -447,7 +447,13 @@ oatpp::Void Deserializer::deserializeObject(Deserializer* deserializer, parser::
|
|||||||
skipValue(caret);
|
skipValue(caret);
|
||||||
polymorphs.emplace_back(field, label.toString()); // store polymorphs for later processing.
|
polymorphs.emplace_back(field, label.toString()); // store polymorphs for later processing.
|
||||||
} else {
|
} else {
|
||||||
field->set(static_cast<oatpp::BaseObject *>(object.get()), deserializer->deserialize(caret, field->type));
|
auto value = deserializer->deserialize(caret, field->type);
|
||||||
|
if(field->info.required && value == nullptr) {
|
||||||
|
throw std::runtime_error("[oatpp::parser::json::mapping::Deserializer::deserialize()]: "
|
||||||
|
"Error. " + std::string(type->nameQualifier) + "::"
|
||||||
|
+ std::string(field->name) + " is required!");
|
||||||
|
}
|
||||||
|
field->set(static_cast<oatpp::BaseObject *>(object.get()), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (deserializer->getConfig()->allowUnknownFields) {
|
} else if (deserializer->getConfig()->allowUnknownFields) {
|
||||||
@ -479,6 +485,11 @@ oatpp::Void Deserializer::deserializeObject(Deserializer* deserializer, parser::
|
|||||||
parser::Caret polyCaret(p.second);
|
parser::Caret polyCaret(p.second);
|
||||||
auto selectedType = p.first->info.typeSelector->selectType(static_cast<oatpp::BaseObject *>(object.get()));
|
auto selectedType = p.first->info.typeSelector->selectType(static_cast<oatpp::BaseObject *>(object.get()));
|
||||||
auto value = deserializer->deserialize(polyCaret, selectedType);
|
auto value = deserializer->deserialize(polyCaret, selectedType);
|
||||||
|
if(p.first->info.required && value == nullptr) {
|
||||||
|
throw std::runtime_error("[oatpp::parser::json::mapping::Deserializer::deserialize()]: "
|
||||||
|
"Error. " + std::string(type->nameQualifier) + "::"
|
||||||
|
+ std::string(p.first->name) + " is required!");
|
||||||
|
}
|
||||||
oatpp::Any any(value);
|
oatpp::Any any(value);
|
||||||
p.first->set(static_cast<oatpp::BaseObject *>(object.get()), oatpp::Void(any.getPtr(), p.first->type));
|
p.first->set(static_cast<oatpp::BaseObject *>(object.get()), oatpp::Void(any.getPtr(), p.first->type));
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,58 @@ class Test4 : public oatpp::DTO {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Test5 : public oatpp::DTO {
|
||||||
|
|
||||||
|
DTO_INIT(Test5, DTO)
|
||||||
|
|
||||||
|
DTO_FIELD_INFO(strF) {
|
||||||
|
info->required = true;
|
||||||
|
}
|
||||||
|
DTO_FIELD(String, strF);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Test6 : public oatpp::DTO {
|
||||||
|
|
||||||
|
DTO_INIT(Test6, DTO)
|
||||||
|
|
||||||
|
DTO_FIELD(String, strF);
|
||||||
|
};
|
||||||
|
|
||||||
|
class TestChild1 : public oatpp::DTO {
|
||||||
|
|
||||||
|
DTO_INIT(TestChild1, DTO)
|
||||||
|
|
||||||
|
DTO_FIELD_INFO(name) {
|
||||||
|
info->required = true;
|
||||||
|
}
|
||||||
|
DTO_FIELD(String, name);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Test7 : public oatpp::DTO {
|
||||||
|
|
||||||
|
DTO_INIT(Test7, DTO)
|
||||||
|
|
||||||
|
DTO_FIELD(String, strF);
|
||||||
|
|
||||||
|
DTO_FIELD(Object<TestChild1>, child);
|
||||||
|
};
|
||||||
|
|
||||||
|
class TestChild2 : public oatpp::DTO {
|
||||||
|
|
||||||
|
DTO_INIT(TestChild2, DTO)
|
||||||
|
|
||||||
|
DTO_FIELD(String, name);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Test8 : public oatpp::DTO {
|
||||||
|
|
||||||
|
DTO_INIT(Test8, DTO)
|
||||||
|
|
||||||
|
DTO_FIELD(String, strF);
|
||||||
|
|
||||||
|
DTO_FIELD(Object<TestChild2>, child);
|
||||||
|
};
|
||||||
|
|
||||||
class AnyDto : public oatpp::DTO {
|
class AnyDto : public oatpp::DTO {
|
||||||
|
|
||||||
DTO_INIT(AnyDto, DTO)
|
DTO_INIT(AnyDto, DTO)
|
||||||
@ -100,7 +152,7 @@ void DeserializerTest::onRun(){
|
|||||||
OATPP_ASSERT(obj1)
|
OATPP_ASSERT(obj1)
|
||||||
OATPP_ASSERT(!obj1->strF)
|
OATPP_ASSERT(!obj1->strF)
|
||||||
|
|
||||||
obj1 = mapper->readFromString<oatpp::Object<Test1>>("{\"strF\":\"value1\"}");
|
obj1 = mapper->readFromString<oatpp::Object<Test1>>(R"({"strF":"value1"})");
|
||||||
|
|
||||||
OATPP_ASSERT(obj1)
|
OATPP_ASSERT(obj1)
|
||||||
OATPP_ASSERT(obj1->strF)
|
OATPP_ASSERT(obj1->strF)
|
||||||
@ -185,6 +237,34 @@ void DeserializerTest::onRun(){
|
|||||||
OATPP_ASSERT(obj4->list->size() == 0)
|
OATPP_ASSERT(obj4->list->size() == 0)
|
||||||
OATPP_ASSERT(obj4->map->size() == 0)
|
OATPP_ASSERT(obj4->map->size() == 0)
|
||||||
|
|
||||||
|
data::mapping::type::DTOWrapper<Test5> obj5;
|
||||||
|
try {
|
||||||
|
obj5 = mapper->readFromString<oatpp::Object<Test5>>(R"({"strF":null})");
|
||||||
|
} catch (std::runtime_error& e) {
|
||||||
|
OATPP_LOGD(TAG, "Test5::strF is required!")
|
||||||
|
}
|
||||||
|
OATPP_ASSERT(obj5 == nullptr)
|
||||||
|
|
||||||
|
try {
|
||||||
|
auto obj6 = mapper->readFromString<oatpp::Object<Test6>>(R"({"strF":null})");
|
||||||
|
} catch (std::runtime_error& e) {
|
||||||
|
OATPP_ASSERT(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
data::mapping::type::DTOWrapper<Test7> obj7;
|
||||||
|
try {
|
||||||
|
obj7 = mapper->readFromString<oatpp::Object<Test7>>(R"({"strF":"value1", "child":{"name":null}})");
|
||||||
|
} catch (std::runtime_error& e) {
|
||||||
|
OATPP_LOGD(TAG, "TestChild1::name is required!")
|
||||||
|
}
|
||||||
|
OATPP_ASSERT(obj7 == nullptr)
|
||||||
|
|
||||||
|
try {
|
||||||
|
auto obj8 = mapper->readFromString<oatpp::Object<Test8>>(R"({"strF":"value1", "child":{"name":null}})");
|
||||||
|
} catch (std::runtime_error& e) {
|
||||||
|
OATPP_ASSERT(false)
|
||||||
|
}
|
||||||
|
|
||||||
OATPP_LOGD(TAG, "Any: String")
|
OATPP_LOGD(TAG, "Any: String")
|
||||||
{
|
{
|
||||||
auto dto = mapper->readFromString<oatpp::Object<AnyDto>>(R"({"any":"my_string"})");
|
auto dto = mapper->readFromString<oatpp::Object<AnyDto>>(R"({"any":"my_string"})");
|
||||||
|
Loading…
Reference in New Issue
Block a user