From 01c8645fe22090d682f2c79cdae90e97fb00c92e Mon Sep 17 00:00:00 2001 From: KarlaCarvajal Date: Mon, 2 Dec 2024 09:10:24 -0500 Subject: [PATCH] fix(dotnet-sdk): refactor task signature to not build schema output when there is not return type (#1170) --- .../Helper/LHMappingHelperTest.cs | 19 +++-------- .../LHTaskSignatureTest.cs | 33 +++++++++++++++++++ .../LittleHorse.Sdk/Helper/LHMappingHelper.cs | 13 +------- .../LittleHorse.Sdk/Worker/LHTaskSignature.cs | 9 ++--- sdk-dotnet/Littlehorse.sln | 13 ++++++++ 5 files changed, 56 insertions(+), 31 deletions(-) diff --git a/sdk-dotnet/LittleHorse.Sdk.Tests/Helper/LHMappingHelperTest.cs b/sdk-dotnet/LittleHorse.Sdk.Tests/Helper/LHMappingHelperTest.cs index e9cea017c..98fdc081b 100644 --- a/sdk-dotnet/LittleHorse.Sdk.Tests/Helper/LHMappingHelperTest.cs +++ b/sdk-dotnet/LittleHorse.Sdk.Tests/Helper/LHMappingHelperTest.cs @@ -3,7 +3,6 @@ using LittleHorse.Common.Proto; using LittleHorse.Sdk.Helper; using Google.Protobuf.WellKnownTypes; -using LittleHorse.Sdk.Exceptions; using LittleHorse.Sdk.Tests; using Xunit; using Type = System.Type; @@ -70,16 +69,6 @@ public void LHHelper_WithSystemBytesVariableType_ShouldReturnLHVariableBytesType Assert.True(result == VariableType.Bytes); } - [Fact] - public void LHHelper_WithSystemVoidVariableType_ShouldReturnLHVariableJsonObjType() - { - var type = typeof(void); - - var result = LHMappingHelper.MapDotNetTypeToLHVariableType(type); - - Assert.True(result == VariableType.JsonObj); - } - [Fact] public void LHHelper_WithSystemArrayObjectVariableType_ShouldReturnLHVariableJsonArrType() { @@ -94,15 +83,15 @@ public void LHHelper_WithSystemArrayObjectVariableType_ShouldReturnLHVariableJso } [Fact] - public void LHHelper_WithoutSystemVariableType_ShouldThrowException() + public void LHHelper_WithNotAllowedSystemVariableTypes_ShouldReturnLHJsonObj() { - var test_not_allowed_types = new List() { typeof(decimal), typeof(char) }; + var test_not_allowed_types = new List() { typeof(decimal), typeof(char), typeof(void) }; foreach (var type in test_not_allowed_types) { - var exception = Assert.Throws(() => LHMappingHelper.MapDotNetTypeToLHVariableType(type)); + var result = LHMappingHelper.MapDotNetTypeToLHVariableType(type); - Assert.Equal($"Unaccepted variable type.", exception.Message); + Assert.Equal(VariableType.JsonObj, result); } } diff --git a/sdk-dotnet/LittleHorse.Sdk.Tests/LHTaskSignatureTest.cs b/sdk-dotnet/LittleHorse.Sdk.Tests/LHTaskSignatureTest.cs index 2fc89b565..5600a1218 100644 --- a/sdk-dotnet/LittleHorse.Sdk.Tests/LHTaskSignatureTest.cs +++ b/sdk-dotnet/LittleHorse.Sdk.Tests/LHTaskSignatureTest.cs @@ -9,6 +9,7 @@ public class LHTaskSignatureTest { const string TASK_DEF_NAME_ADD = "add-task-worker"; + const string TASK_DEF_NAME_INFORM = "inform-task-worker"; const string TASK_DEF_NAME_UPDATE = "update-task-worker"; const string TASK_DEF_NAME_DELETE = "delete-task-worker"; const string TASK_DEF_NAME_GET = "get-task-worker"; @@ -63,6 +64,32 @@ public void TaskSignature_WithLHTaskMethodAndLHTypeAttributes_ShouldBuildLHSigna Assert.False(taskSignature.HasWorkerContextAtEnd); } + [Fact] + public void TaskSignature_WithoutReturnTypeInLHTaskMethod_ShouldBuildLHSignatureWithoutSchemaOutput() + { + int number_of_method_params = 1; + + var taskSignature = new LHTaskSignature(TASK_DEF_NAME_INFORM, new TestWorker()); + + var expectedLHMethodParam = new LHMethodParam + { + Type = VariableType.Str, + Name = "name", + IsMasked = TRUE_IS_MASKET + }; + + Assert.True(taskSignature.LhMethodParams.Count == number_of_method_params); + foreach (var actualLHMethodParam in taskSignature.LhMethodParams) + { + Assert.Equal(expectedLHMethodParam.Name, actualLHMethodParam.Name); + Assert.Equal(expectedLHMethodParam.Type, actualLHMethodParam.Type); + Assert.Equal(expectedLHMethodParam.IsMasked, actualLHMethodParam.IsMasked); + } + + Assert.Null(taskSignature.TaskDefOutputSchema); + Assert.False(taskSignature.HasWorkerContextAtEnd); + } + [Fact] public void TaskSignature_WithLHTaskMethodAndLHTypeAttributes_ShouldBuildSignatureWithOutputResult() { @@ -202,6 +229,12 @@ public string Add([LHType(masked: TRUE_IS_MASKET)] string name) return $"Output value: {name}"; } + [LHTaskMethod(TASK_DEF_NAME_INFORM)] + public void Inform([LHType(masked: TRUE_IS_MASKET)] string name) + { + var test_variable = "test_variable" + name; + } + [LHTaskMethod(TASK_DEF_NAME_UPDATE)] [LHType(masked: TRUE_IS_MASKET, name: "result")] public string Update(int value) diff --git a/sdk-dotnet/LittleHorse.Sdk/Helper/LHMappingHelper.cs b/sdk-dotnet/LittleHorse.Sdk/Helper/LHMappingHelper.cs index 39da40464..9705fc8a4 100644 --- a/sdk-dotnet/LittleHorse.Sdk/Helper/LHMappingHelper.cs +++ b/sdk-dotnet/LittleHorse.Sdk/Helper/LHMappingHelper.cs @@ -1,5 +1,4 @@ using System.Collections; -using System.Net; using Google.Protobuf; using Google.Protobuf.WellKnownTypes; using LittleHorse.Common.Proto; @@ -45,17 +44,7 @@ public static VariableType MapDotNetTypeToLHVariableType(Type type) return VariableType.JsonArr; } - if (!type.Namespace!.StartsWith("System")) - { - return VariableType.JsonObj; - } - - if (type.IsAssignableFrom(typeof(void))) - { - return VariableType.JsonObj; - } - - throw new Exception("Unaccepted variable type."); + return VariableType.JsonObj; } public static DateTime? MapDateTimeFromProtoTimeStamp(Timestamp protoTimestamp) diff --git a/sdk-dotnet/LittleHorse.Sdk/Worker/LHTaskSignature.cs b/sdk-dotnet/LittleHorse.Sdk/Worker/LHTaskSignature.cs index ba3ead229..a9a6f7f01 100644 --- a/sdk-dotnet/LittleHorse.Sdk/Worker/LHTaskSignature.cs +++ b/sdk-dotnet/LittleHorse.Sdk/Worker/LHTaskSignature.cs @@ -50,9 +50,10 @@ public LHTaskSignature(string taskDefName, T executable) var methodParams = TaskMethod.GetParameters(); - CreateInputVarsSignature(methodParams); + BuildInputVarsSignature(methodParams); - CreateOutputSchemaSignature(); + if (!TaskMethod.ReturnType.IsAssignableFrom(typeof(void))) + BuildOutputSchemaSignature(); } private bool IsValidLHTaskWorkerValue(Attribute? lhtaskWorkerAttribute, string taskDefName) @@ -65,7 +66,7 @@ private bool IsValidLHTaskWorkerValue(Attribute? lhtaskWorkerAttribute, string t return false; } - private void CreateInputVarsSignature(ParameterInfo[] methodParams) + private void BuildInputVarsSignature(ParameterInfo[] methodParams) { for (int i = 0; i < methodParams.Length; i++) { @@ -107,7 +108,7 @@ private void CreateInputVarsSignature(ParameterInfo[] methodParams) } } - private void CreateOutputSchemaSignature() + private void BuildOutputSchemaSignature() { var returnType = LHMappingHelper.MapDotNetTypeToLHVariableType(TaskMethod.ReturnType); var maskedValue = false; diff --git a/sdk-dotnet/Littlehorse.sln b/sdk-dotnet/Littlehorse.sln index 0b488ec5f..23959b58f 100644 --- a/sdk-dotnet/Littlehorse.sln +++ b/sdk-dotnet/Littlehorse.sln @@ -11,6 +11,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicExample", "Examples\Ba EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaskedFieldsExample", "Examples\MaskedFieldsExample\MaskedFieldsExample.csproj", "{AA9767C3-9F03-4414-A4C2-A0C3761A975C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LittleHorse.Sdk.Tests", "LittleHorse.Sdk.Tests\LittleHorse.Sdk.Tests.csproj", "{AC276D3F-ABAB-4E2D-B721-099681083390}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExceptionsHandlerExample", "Examples\ExceptionsHandlerExample\ExceptionsHandlerExample.csproj", "{6B7A8034-21C0-465D-8708-4F47A1780972}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -32,9 +36,18 @@ Global {AA9767C3-9F03-4414-A4C2-A0C3761A975C}.Debug|Any CPU.Build.0 = Debug|Any CPU {AA9767C3-9F03-4414-A4C2-A0C3761A975C}.Release|Any CPU.ActiveCfg = Release|Any CPU {AA9767C3-9F03-4414-A4C2-A0C3761A975C}.Release|Any CPU.Build.0 = Release|Any CPU + {AC276D3F-ABAB-4E2D-B721-099681083390}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AC276D3F-ABAB-4E2D-B721-099681083390}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AC276D3F-ABAB-4E2D-B721-099681083390}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AC276D3F-ABAB-4E2D-B721-099681083390}.Release|Any CPU.Build.0 = Release|Any CPU + {6B7A8034-21C0-465D-8708-4F47A1780972}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6B7A8034-21C0-465D-8708-4F47A1780972}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6B7A8034-21C0-465D-8708-4F47A1780972}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6B7A8034-21C0-465D-8708-4F47A1780972}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {88CB23B3-1174-45F5-9F72-46AB57BAB661} = {EEC3BB78-BFA5-4525-A965-4EAA3455BBAE} {AA9767C3-9F03-4414-A4C2-A0C3761A975C} = {EEC3BB78-BFA5-4525-A965-4EAA3455BBAE} + {6B7A8034-21C0-465D-8708-4F47A1780972} = {EEC3BB78-BFA5-4525-A965-4EAA3455BBAE} EndGlobalSection EndGlobal