Fix problems with the serializer when using 'type' as an attribute

fixes #7372
This commit is contained in:
Gunnar Beutner 2014-10-25 09:14:56 +02:00
parent 4dc13d965e
commit 315c84eb4a
11 changed files with 25 additions and 32 deletions

View File

@ -131,7 +131,7 @@ DynamicObject::Ptr DynamicType::CreateObject(const Dictionary::Ptr& serializedUp
Object::Ptr object = type->Instantiate(); Object::Ptr object = type->Instantiate();
Deserialize(object, serializedUpdate, false, FAConfig); Deserialize(object, serializedUpdate, true, FAConfig);
return static_pointer_cast<DynamicObject>(object); return static_pointer_cast<DynamicObject>(object);
} }

View File

@ -149,6 +149,9 @@ static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary
{ {
const Type *type; const Type *type;
if (!object && safe_mode)
BOOST_THROW_EXCEPTION(std::runtime_error("Tried to instantiate object while safe mode is enabled."));
if (object) if (object)
type = object->GetReflectionType(); type = object->GetReflectionType();
else else
@ -157,14 +160,12 @@ static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary
if (!type) if (!type)
return object; return object;
Object::Ptr instance = object; Object::Ptr instance;
if (!instance) { if (object)
if (safe_mode && !type->IsSafe()) instance = object;
BOOST_THROW_EXCEPTION(std::runtime_error("Tried to instantiate type '" + type->GetName() + "' which is not marked as safe.")); else
instance = type->Instantiate(); instance = type->Instantiate();
}
ObjectLock olock(input); ObjectLock olock(input);
BOOST_FOREACH(const Dictionary::Pair& kv, input) { BOOST_FOREACH(const Dictionary::Pair& kv, input) {
@ -232,8 +233,8 @@ Value icinga::Deserialize(const Object::Ptr& object, const Value& value, bool sa
ASSERT(dict != NULL); ASSERT(dict != NULL);
if (!dict->Contains("type")) if ((safe_mode && !object) || !dict->Contains("type"))
return DeserializeDictionary(dict, safe_mode, attributeTypes); return DeserializeDictionary(dict, safe_mode, attributeTypes);
else
return DeserializeObject(object, dict, safe_mode, attributeTypes); return DeserializeObject(object, dict, safe_mode, attributeTypes);
} }

View File

@ -57,11 +57,6 @@ bool Type::IsAbstract(void) const
return ((GetAttributes() & TAAbstract) != 0); return ((GetAttributes() & TAAbstract) != 0);
} }
bool Type::IsSafe(void) const
{
return ((GetAttributes() & TASafe) != 0);
}
bool Type::IsAssignableFrom(const Type *other) const bool Type::IsAssignableFrom(const Type *other) const
{ {
for (const Type *t = other; t; t = t->GetBaseType()) { for (const Type *t = other; t; t = t->GetBaseType()) {

View File

@ -43,8 +43,7 @@ struct Field
enum TypeAttribute enum TypeAttribute
{ {
TAAbstract = 1, TAAbstract = 1
TASafe = 2
}; };
class I2_BASE_API Type class I2_BASE_API Type
@ -64,7 +63,6 @@ public:
bool IsAssignableFrom(const Type *other) const; bool IsAssignableFrom(const Type *other) const;
bool IsAbstract(void) const; bool IsAbstract(void) const;
bool IsSafe(void) const;
static void Register(const Type *type); static void Register(const Type *type);
static const Type *GetByName(const String& name); static const Type *GetByName(const String& name);

View File

@ -131,9 +131,8 @@ Value ApiEvents::CheckResultAPIHandler(const MessageOrigin& origin, const Dictio
if (!params) if (!params)
return Empty; return Empty;
Value crv = params->Get("cr"); CheckResult::Ptr cr = make_shared<CheckResult>();
Deserialize(cr, params->Get("cr"), true);
CheckResult::Ptr cr = Deserialize(crv, true);
Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin); Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
@ -1105,7 +1104,7 @@ Value ApiEvents::VarsChangedAPIHandler(const MessageOrigin& origin, const Dictio
if (origin.FromZone && !origin.FromZone->CanAccessObject(object)) if (origin.FromZone && !origin.FromZone->CanAccessObject(object))
return Empty; return Empty;
Dictionary::Ptr vars = Deserialize(params->Get("vars"), true); Dictionary::Ptr vars = params->Get("vars");
if (!vars) if (!vars)
return Empty; return Empty;
@ -1166,7 +1165,8 @@ Value ApiEvents::CommentAddedAPIHandler(const MessageOrigin& origin, const Dicti
if (origin.FromZone && !origin.FromZone->CanAccessObject(checkable)) if (origin.FromZone && !origin.FromZone->CanAccessObject(checkable))
return Empty; return Empty;
Comment::Ptr comment = Deserialize(params->Get("comment"), true); Comment::Ptr comment = make_shared<Comment>();
Deserialize(comment, params->Get("comment"), true);
checkable->AddComment(comment->GetEntryType(), comment->GetAuthor(), checkable->AddComment(comment->GetEntryType(), comment->GetAuthor(),
comment->GetText(), comment->GetExpireTime(), comment->GetId(), origin); comment->GetText(), comment->GetExpireTime(), comment->GetId(), origin);
@ -1281,7 +1281,8 @@ Value ApiEvents::DowntimeAddedAPIHandler(const MessageOrigin& origin, const Dict
if (origin.FromZone && !origin.FromZone->CanAccessObject(checkable)) if (origin.FromZone && !origin.FromZone->CanAccessObject(checkable))
return Empty; return Empty;
Downtime::Ptr downtime = Deserialize(params->Get("downtime"), true); Downtime::Ptr downtime = make_shared<Downtime>();
Deserialize(downtime, params->Get("downtime"), true);
checkable->AddDowntime(downtime->GetAuthor(), downtime->GetComment(), checkable->AddDowntime(downtime->GetAuthor(), downtime->GetComment(),
downtime->GetStartTime(), downtime->GetEndTime(), downtime->GetStartTime(), downtime->GetEndTime(),

View File

@ -57,7 +57,7 @@ enum StateType
}; };
}}} }}}
safe class CheckResult class CheckResult
{ {
[state] double schedule_start; [state] double schedule_start;
[state] double schedule_end; [state] double schedule_end;

View File

@ -35,7 +35,7 @@ enum CommentType
}; };
}}} }}}
safe class Comment class Comment
{ {
[state] String id; [state] String id;
[state] double entry_time; [state] double entry_time;

View File

@ -20,7 +20,7 @@
namespace icinga namespace icinga
{ {
safe class Downtime class Downtime
{ {
[state] String id; [state] String id;
[state] double entry_time; [state] double entry_time;

View File

@ -22,7 +22,7 @@
namespace icinga namespace icinga
{ {
safe class PerfdataValue class PerfdataValue
{ {
[state] String label; [state] String label;
[state] double value; [state] double value;

View File

@ -133,7 +133,6 @@ class { return T_CLASS; }
namespace { return T_NAMESPACE; } namespace { return T_NAMESPACE; }
code { return T_CODE; } code { return T_CODE; }
abstract { yylval->num = TAAbstract; return T_CLASS_ATTRIBUTE; } abstract { yylval->num = TAAbstract; return T_CLASS_ATTRIBUTE; }
safe { yylval->num = TASafe; return T_CLASS_ATTRIBUTE; }
config { yylval->num = FAConfig; return T_FIELD_ATTRIBUTE; } config { yylval->num = FAConfig; return T_FIELD_ATTRIBUTE; }
state { yylval->num = FAState; return T_FIELD_ATTRIBUTE; } state { yylval->num = FAState; return T_FIELD_ATTRIBUTE; }
enum { yylval->num = FAEnum; return T_FIELD_ATTRIBUTE; } enum { yylval->num = FAEnum; return T_FIELD_ATTRIBUTE; }

View File

@ -105,8 +105,7 @@ struct Field
enum TypeAttribute enum TypeAttribute
{ {
TAAbstract = 1, TAAbstract = 1
TASafe = 2
}; };
struct Klass struct Klass