From 2448d5eabd99dee142cd12ff918f3808498f3b01 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 9 Feb 2024 19:42:03 +0100 Subject: [PATCH] Fix serialization of Boolean activity input (#4922) * Improve InputJsonConverter handling and trimming suppression A new switch case block has been added for better handling of different types of JsonValueKind in InputJsonConverter. Also, a suppression message for the IL2026 trimming warning has been included to prevent potential runtime issues caused by code trimming, although justifications for this suppression are still pending. * Add UIHint to While activity's Condition input The Condition input in the While activity in Elsa.Workflows.Core has been updated to also include a UIHint. This aims to improve user input by specifically defining it as a SingleLine type. --- .../Elsa.Workflows.Core/Activities/While.cs | 3 ++- .../Converters/InputJsonConverter.cs | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/modules/Elsa.Workflows.Core/Activities/While.cs b/src/modules/Elsa.Workflows.Core/Activities/While.cs index ec2f85eeb..dc998be41 100644 --- a/src/modules/Elsa.Workflows.Core/Activities/While.cs +++ b/src/modules/Elsa.Workflows.Core/Activities/While.cs @@ -5,6 +5,7 @@ using Elsa.Workflows.Attributes; using Elsa.Workflows.Behaviors; using Elsa.Workflows.Contracts; using Elsa.Workflows.Models; +using Elsa.Workflows.UIHints; using JetBrains.Annotations; namespace Elsa.Workflows.Activities; @@ -69,7 +70,7 @@ public class While : Activity /// /// The condition to evaluate. /// - [Input(AutoEvaluate = false)] + [Input(AutoEvaluate = false, UIHint = InputUIHints.SingleLine)] public Input Condition { get; set; } = new(false); /// diff --git a/src/modules/Elsa.Workflows.Core/Serialization/Converters/InputJsonConverter.cs b/src/modules/Elsa.Workflows.Core/Serialization/Converters/InputJsonConverter.cs index abd643cd7..f7d194fca 100644 --- a/src/modules/Elsa.Workflows.Core/Serialization/Converters/InputJsonConverter.cs +++ b/src/modules/Elsa.Workflows.Core/Serialization/Converters/InputJsonConverter.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; using Elsa.Expressions.Contracts; @@ -24,6 +25,7 @@ public class InputJsonConverter : JsonConverter> public override bool CanConvert(Type typeToConvert) => typeof(Input).IsAssignableFrom(typeToConvert); /// + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "")] public override Input Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (!JsonDocument.TryParseValue(ref reader, out var doc)) @@ -42,11 +44,17 @@ public class InputJsonConverter : JsonConverter> var memoryBlockReference = expressionDescriptor?.MemoryBlockReferenceFactory(); var memoryBlockReferenceType = memoryBlockReference?.GetType(); var expressionValueElement = expressionElement.TryGetProperty("value", out var expressionElementValueValue) ? expressionElementValueValue : default; - var expressionValue = expressionValueElement.ValueKind == JsonValueKind.String - ? expressionValueElement.GetString() - : expressionValueElement.ValueKind != JsonValueKind.Undefined && memoryBlockReferenceType != null - ? expressionValueElement.Deserialize(memoryBlockReferenceType, options)! - : default; + + var expressionValue = expressionValueElement.ValueKind switch + { + JsonValueKind.String => expressionValueElement.GetString(), + JsonValueKind.False => false, + JsonValueKind.True => true, + JsonValueKind.Number => expressionValueElement.GetDouble(), + JsonValueKind.Undefined => default, + _ => memoryBlockReferenceType != null ? expressionValueElement.Deserialize(memoryBlockReferenceType, options)! : default + }; + var expression = new Expression(expressionTypeName, expressionValue); if (memoryBlockReference == null)