From 52ad9a4fc6384a25803486b2c60223d6c1fa04ab Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Mon, 13 Feb 2017 15:47:57 +0100 Subject: [PATCH] Fixed Tapeti trying to publish return types of type Task --- Tapeti.Flow/Default/FlowBindingMiddleware.cs | 10 +++++----- Tapeti/Default/PublishResultBinding.cs | 6 +++--- Tapeti/Helpers/TaskTypeHelper.cs | 21 +++++++++++++------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Tapeti.Flow/Default/FlowBindingMiddleware.cs b/Tapeti.Flow/Default/FlowBindingMiddleware.cs index 66f1e12..85a64d3 100644 --- a/Tapeti.Flow/Default/FlowBindingMiddleware.cs +++ b/Tapeti.Flow/Default/FlowBindingMiddleware.cs @@ -54,11 +54,11 @@ namespace Tapeti.Flow.Default private static void RegisterYieldPointResult(IBindingContext context) { - bool isTask; - if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(typeof(IYieldPoint), out isTask)) + bool isTaskOf; + if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(typeof(IYieldPoint), out isTaskOf)) return; - if (isTask) + if (isTaskOf) { context.Result.SetHandler(async (messageContext, value) => { @@ -92,8 +92,8 @@ namespace Tapeti.Flow.Default if (request?.Response == null) return; - bool isTask; - if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t == request.Response || t == typeof(IYieldPoint), out isTask)) + bool isTaskOf; + if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t == request.Response || t == typeof(IYieldPoint), out isTaskOf)) throw new ResponseExpectedException($"Response of class {request.Response.FullName} expected in controller {context.Method.DeclaringType?.FullName}, method {context.Method.Name}"); } } diff --git a/Tapeti/Default/PublishResultBinding.cs b/Tapeti/Default/PublishResultBinding.cs index 5a6638e..7e5c29d 100644 --- a/Tapeti/Default/PublishResultBinding.cs +++ b/Tapeti/Default/PublishResultBinding.cs @@ -18,13 +18,13 @@ namespace Tapeti.Default return; - bool isTask; + bool isTaskOf; Type actualType; - if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t.IsClass, out isTask, out actualType)) + if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t.IsClass, out isTaskOf, out actualType)) return; - if (isTask) + if (isTaskOf) { var handler = GetType().GetMethod("PublishGenericTaskResult", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(actualType); diff --git a/Tapeti/Helpers/TaskTypeHelper.cs b/Tapeti/Helpers/TaskTypeHelper.cs index 131f583..b5e1a77 100644 --- a/Tapeti/Helpers/TaskTypeHelper.cs +++ b/Tapeti/Helpers/TaskTypeHelper.cs @@ -5,11 +5,18 @@ namespace Tapeti.Helpers { public static class TaskTypeHelper { - public static bool IsTypeOrTaskOf(this Type type, Func predicate, out bool isTask, out Type actualType) + public static bool IsTypeOrTaskOf(this Type type, Func predicate, out bool isTaskOf, out Type actualType) { + if (type == typeof(Task)) + { + isTaskOf = false; + actualType = type; + return false; + } + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>)) { - isTask = true; + isTaskOf = true; var genericArguments = type.GetGenericArguments(); if (genericArguments.Length == 1 && predicate(genericArguments[0])) @@ -19,21 +26,21 @@ namespace Tapeti.Helpers } } - isTask = false; + isTaskOf = false; actualType = type; return predicate(type); } - public static bool IsTypeOrTaskOf(this Type type, Func predicate, out bool isTask) + public static bool IsTypeOrTaskOf(this Type type, Func predicate, out bool isTaskOf) { Type actualType; - return IsTypeOrTaskOf(type, predicate, out isTask, out actualType); + return IsTypeOrTaskOf(type, predicate, out isTaskOf, out actualType); } - public static bool IsTypeOrTaskOf(this Type type, Type compareTo, out bool isTask) + public static bool IsTypeOrTaskOf(this Type type, Type compareTo, out bool isTaskOf) { - return IsTypeOrTaskOf(type, t => t == compareTo, out isTask); + return IsTypeOrTaskOf(type, t => t == compareTo, out isTaskOf); } } }