Skip to content

Commit

Permalink
Make sure Observable method is declared progressive
Browse files Browse the repository at this point in the history
As discussed in the review, this extra declaration is a requirement to
make sure that people really want the method to be progressive and not
return a IObservable derived type by accident.

Code-Sharp#238
  • Loading branch information
Johan 't Hart committed Mar 5, 2021
1 parent f1f54e7 commit 9c5f914
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ private static Type GetRelevantInterceptorType(MethodInfo method)

if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(IObservable<>))
{
MethodInfoValidation.ValidateProgressiveObservableMethod(method);

genericArgument = returnType.GetGenericArguments()[0];
interceptorType = typeof(ObservableCalleeProxyInterceptor<>);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void ValidateTupleReturnType(MethodInfo method)

IList<string> transformNames = attribute.TransformNames;

List<string> tupleNames =
List<string> tupleNames =
transformNames.Take(tupleLength).ToList();

ValidateTupleReturnType(method, tupleNames);
Expand All @@ -47,7 +47,7 @@ private static void ValidateTupleReturnTypeWithOutRefParameters(MethodInfo metho
method.GetParameters().Where(x => x.IsOut || x.ParameterType.IsByRef)
.Select(x => x.Name);

ICollection<string> intersection =
ICollection<string> intersection =
tupleNames.Intersect(outOrRefNames).ToList();

if (intersection.Count > 0)
Expand All @@ -74,6 +74,14 @@ public static void ValidateSyncMethod(MethodInfo method)
ValidateTupleReturnType(method);
}

internal static void ValidateProgressiveObservableMethod(MethodInfo method)
{
if (!method.IsDefined(typeof(WampProgressiveResultProcedureAttribute)))
{
ThrowHelper.ObservableMethodNotDeclaredProgressive(method);
}
}

public static void ValidateAsyncMethod(MethodInfo method)
{
ParameterInfo[] parameters = method.GetParameters();
Expand Down Expand Up @@ -162,6 +170,12 @@ public static void ProgressiveParameterTypeMismatch(MethodInfo method, Type retu
($"Method {method.Name} of type {method.DeclaringType.FullName} is declared as a progressive WAMP procedure, but its last (or second to last) parameter is not a IProgress of its return type. Expected: IProgress<{returnType.FullName}>");
}

public static void ObservableMethodNotDeclaredProgressive(MethodInfo method)
{
throw new ArgumentException
($"Method {method.Name} of type {method.DeclaringType.FullName} is returning an IObservable and therefore is required to be declared as a progressive WAMP procedure, but it is not. Please use the [WampProgressiveResultProcedure] attribute.");
}

public static void ProgressiveParameterTupleMismatch(MethodInfo method)
{
throw new ArgumentException
Expand Down Expand Up @@ -199,4 +213,4 @@ public static void CancellationTokenMustBeLastParameter(MethodInfo method)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected IWampRpcOperation CreateRpcMethod(Func<object> instanceProvider, ICall

if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(IObservable<>))
{
MethodInfoValidation.ValidateProgressiveObservableMethod(method);
return CreateProgressiveObservableOperation(instanceProvider, method, procedureUri);
}
else if (!typeof (Task).IsAssignableFrom(method.ReturnType))
Expand Down

0 comments on commit 9c5f914

Please sign in to comment.