Skip to content

Commit

Permalink
Fix serialization of Boolean activity input (elsa-workflows#4922)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
sfmskywalker committed Feb 9, 2024
1 parent 796438c commit 2448d5e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/modules/Elsa.Workflows.Core/Activities/While.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Elsa.Workflows.Behaviors;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Models;
using Elsa.Workflows.UIHints;
using JetBrains.Annotations;

namespace Elsa.Workflows.Activities;
Expand Down Expand Up @@ -69,7 +70,7 @@ public While(Func<bool> condition, IActivity? body = default, [CallerFilePath] s
/// <summary>
/// The condition to evaluate.
/// </summary>
[Input(AutoEvaluate = false)]
[Input(AutoEvaluate = false, UIHint = InputUIHints.SingleLine)]
public Input<bool> Condition { get; set; } = new(false);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
using Elsa.Expressions.Contracts;
Expand All @@ -24,6 +25,7 @@ public InputJsonConverter(IExpressionDescriptorRegistry expressionDescriptorRegi
public override bool CanConvert(Type typeToConvert) => typeof(Input).IsAssignableFrom(typeToConvert);

/// <inheritdoc />
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public override Input<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (!JsonDocument.TryParseValue(ref reader, out var doc))
Expand All @@ -42,11 +44,17 @@ public override Input<T> Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
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)
Expand Down

0 comments on commit 2448d5e

Please sign in to comment.