Skip to content

Commit

Permalink
Merge pull request #692 from Orckestra/fix/token-conversion
Browse files Browse the repository at this point in the history
Expanding method info checks, so return value is within expectations, giving good log info if messed up.
  • Loading branch information
mawtex authored Oct 25, 2019
2 parents 97099e8 + cff7467 commit 2e33ce0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Composite/C1Console/Actions/FlowHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static FlowHandle Deserialize(string serializedFlowHandle)
Type flowTokenType = TypeManager.GetType(flowTokenTypeString);

MethodInfo methodInfo = flowTokenType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static);
if (methodInfo == null)
if (methodInfo == null || !(typeof(FlowToken).IsAssignableFrom(methodInfo.ReturnType)))
{
throw new InvalidOperationException(string.Format("The flow token '{0}' is missing a public static Deserialize method taking a string as parameter and returning an '{1}'", flowTokenType, typeof(FlowToken)));
}
Expand Down
4 changes: 2 additions & 2 deletions Composite/C1Console/Actions/FlowTokenSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Security;
Expand Down Expand Up @@ -76,7 +76,7 @@ public static FlowToken Deserialize(string serialziedFlowToken, bool includeHash
Type flowType = TypeManager.GetType(flowTokenTypeString);

MethodInfo methodInfo = flowType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static);
if (methodInfo == null)
if (methodInfo == null || !(typeof(FlowToken).IsAssignableFrom(methodInfo.ReturnType)))
{
throw new InvalidOperationException(string.Format("The flow token {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", flowType, typeof(FlowToken)));
}
Expand Down
4 changes: 2 additions & 2 deletions Composite/C1Console/Security/ActionTokenSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Security;
Expand Down Expand Up @@ -83,7 +83,7 @@ public static ActionToken Deserialize(string serialziedActionToken, bool include
Type actionType = TypeManager.GetType(actionTokenTypeString);

MethodInfo methodInfo = actionType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static);
if (methodInfo == null)
if (methodInfo == null || !(typeof(ActionToken).IsAssignableFrom(methodInfo.ReturnType)))
{
Log.LogWarning("ActionTokenSerializer", string.Format("The action token {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", actionType, typeof(ActionToken)));
throw new InvalidOperationException(string.Format("The action token {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", actionType, typeof(ActionToken)));
Expand Down
4 changes: 2 additions & 2 deletions Composite/C1Console/Security/EntityTokenSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using System.Security;
using Composite.Core.Serialization;
Expand Down Expand Up @@ -95,7 +95,7 @@ private static EntityToken DeserializeLegacy(string serializedEntityToken, bool
Type entityType = TypeManager.GetType(entityTokenTypeString);

MethodInfo methodInfo = entityType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static);
if (methodInfo == null)
if (methodInfo == null || !(typeof(EntityToken).IsAssignableFrom(methodInfo.ReturnType)))
{
throw new InvalidOperationException($"The entity token {entityType} is missing a public static Deserialize method taking a string as parameter and returning an {typeof(EntityToken)}");
}
Expand Down
8 changes: 8 additions & 0 deletions Composite/Core/Serialization/CompositeJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ public static T Deserialize<T>(string str, bool isSigned)
{
return Deserialize<T>(obj);
}

if (!(typeof(T).IsAssignableFrom(methodInfo.ReturnType)))
{
string typeName = str.GetValue(TypeKeyString);
Log.LogWarning("CompositeJsonSerializer", string.Format("The action {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", typeName, typeof(T)));
throw new InvalidOperationException(string.Format("The token {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", typeName, typeof(T)));
}

return (T)methodInfo.Invoke(null, new object[] { obj });
}

Expand Down

0 comments on commit 2e33ce0

Please sign in to comment.