From fe845e65e766bf249f9b04412e8b50d041626c06 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 25 Jan 2023 11:46:07 +0100 Subject: [PATCH] Fix JsonObject type handling --- .../Elsa.Expressions/Models/JsonLiteral.cs | 23 +++++++++++++++++++ .../Implementations/TypeDescriber.cs | 4 ++-- .../VariableTypeDefinitionProvider.cs | 11 ++++++--- .../Elsa.Workflows.Core/Models/Input.cs | 21 +++++++++++++++++ .../Elsa.Workflows.Core/Models/JsonObject.cs | 19 --------------- .../DefaultExpressionSyntaxProvider.cs | 2 +- 6 files changed, 55 insertions(+), 25 deletions(-) delete mode 100644 src/modules/Elsa.Workflows.Core/Models/JsonObject.cs diff --git a/src/modules/Elsa.Expressions/Models/JsonLiteral.cs b/src/modules/Elsa.Expressions/Models/JsonLiteral.cs index e38cf5836..60b5e0979 100644 --- a/src/modules/Elsa.Expressions/Models/JsonLiteral.cs +++ b/src/modules/Elsa.Expressions/Models/JsonLiteral.cs @@ -1,30 +1,53 @@ using System.Text.Json; +using System.Text.Json.Serialization; namespace Elsa.Expressions.Models; +/// +/// Represents a literal JSON expression. +/// public class JsonLiteral : MemoryBlockReference { + /// + [JsonConstructor] public JsonLiteral() { } + /// public JsonLiteral(string? value) { Value = value; } + /// + /// The literal JSON string value. + /// public string? Value { get; } + + /// public override MemoryBlock Declare() => new(); + /// + /// Serializes the value into a JSON string in the form of a + /// + /// + /// + /// public static JsonLiteral From(T value) => new JsonLiteral(value); } +/// +/// Represents a JSON string for the specified type T +/// public class JsonLiteral : JsonLiteral { + /// public JsonLiteral() { } + /// public JsonLiteral(T value) : base(JsonSerializer.Serialize(value!)) { } diff --git a/src/modules/Elsa.JavaScript/Implementations/TypeDescriber.cs b/src/modules/Elsa.JavaScript/Implementations/TypeDescriber.cs index 88d5f98aa..de6e0d845 100644 --- a/src/modules/Elsa.JavaScript/Implementations/TypeDescriber.cs +++ b/src/modules/Elsa.JavaScript/Implementations/TypeDescriber.cs @@ -26,8 +26,8 @@ public class TypeDescriber : ITypeDescriber { DeclarationKeyword = GetDeclarationKeyword(type), Name = type.Name, - Properties = GetPropertyDefinitions(type).ToList(), - Methods = GetMethodDefinitions(type).ToList() + Properties = GetPropertyDefinitions(type).DistinctBy(x => x.Name).ToList(), + Methods = GetMethodDefinitions(type).DistinctBy(x => x.Name).ToList() }; return typeDefinition; diff --git a/src/modules/Elsa.JavaScript/Providers/VariableTypeDefinitionProvider.cs b/src/modules/Elsa.JavaScript/Providers/VariableTypeDefinitionProvider.cs index 88af27577..d6b34973c 100644 --- a/src/modules/Elsa.JavaScript/Providers/VariableTypeDefinitionProvider.cs +++ b/src/modules/Elsa.JavaScript/Providers/VariableTypeDefinitionProvider.cs @@ -20,16 +20,21 @@ internal class VariableTypeDefinitionProvider : TypeDefinitionProvider protected override IEnumerable GetTypeDefinitions(TypeDefinitionContext context) { - var excludedTypes = new[] { typeof(ExpandoObject), typeof(IDictionary) }; + var excludedTypes = new Func[] + { + type => type == typeof(ExpandoObject), + type => typeof(IDictionary).IsAssignableFrom(type), + type => type == typeof(object) + }; var variableTypeQuery = from variable in context.Variables let variableType = variable.GetVariableType() - where (variableType.IsClass || variableType.IsInterface) && !variableType.IsPrimitive && !excludedTypes.Any(x => x.IsAssignableFrom(variableType)) + where (variableType.IsClass || variableType.IsInterface) && !variableType.IsPrimitive && !excludedTypes.Any(x => x(variableType)) select variableType; var variableTypes = variableTypeQuery.Distinct(); - + foreach (var variableType in variableTypes) { yield return _typeDescriber.DescribeType(variableType); diff --git a/src/modules/Elsa.Workflows.Core/Models/Input.cs b/src/modules/Elsa.Workflows.Core/Models/Input.cs index 03c0ea727..720d1566d 100644 --- a/src/modules/Elsa.Workflows.Core/Models/Input.cs +++ b/src/modules/Elsa.Workflows.Core/Models/Input.cs @@ -6,8 +6,12 @@ using Elsa.Workflows.Core.Expressions; namespace Elsa.Workflows.Core.Models; +/// +/// A base type for the type. +/// public abstract class Input : Argument { + /// protected Input(IExpression expression, MemoryBlockReference memoryBlockReference, Type type) : base(memoryBlockReference) { Expression = expression; @@ -19,60 +23,77 @@ public abstract class Input : Argument [JsonPropertyName("typeName")] public Type Type { get; set; } } +/// +/// Represents activity input that is evaluated at runtime. +/// public class Input : Input { + /// public Input(T literal, string? id = default) : this(new Literal(literal) { Id = id! }) { } + /// public Input(Func @delegate, string? id = default) : this(new DelegateBlockReference(() => @delegate()){ Id = id!}) { } + /// public Input(Func> @delegate) : this(new DelegateBlockReference(@delegate)) { } + /// public Input(Func> @delegate) : this(new DelegateBlockReference(@delegate)) { } + /// public Input(Func @delegate) : this(new DelegateBlockReference(@delegate)) { } + /// public Input(Variable variable) : base(new VariableExpression(variable), variable, typeof(T)) { } + /// public Input(Output output) : base(new OutputExpression(output), output.MemoryBlockReference(), typeof(T)) { } + /// public Input(Literal literal) : base(new LiteralExpression(literal.Value), literal, typeof(T)) { } + /// public Input(Literal literal) : base(new LiteralExpression(literal.Value), literal, typeof(T)) { } + /// public Input(JsonLiteral literal) : base(new JsonExpression(literal.Value), literal, typeof(T)) { } + /// public Input(JsonLiteral literal) : base(new JsonExpression(literal.Value), literal, typeof(T)) { } + /// public Input(DelegateBlockReference delegateBlockReference) : base(new DelegateExpression(delegateBlockReference), delegateBlockReference, typeof(T)) { } + /// public Input(ElsaExpression expression) : this(new ElsaExpressionBlockReference(expression)) { } + /// public Input(IExpression expression, MemoryBlockReference memoryBlockReference) : base(expression, memoryBlockReference, typeof(T)) { } diff --git a/src/modules/Elsa.Workflows.Core/Models/JsonObject.cs b/src/modules/Elsa.Workflows.Core/Models/JsonObject.cs deleted file mode 100644 index 3309d895a..000000000 --- a/src/modules/Elsa.Workflows.Core/Models/JsonObject.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Elsa.Expressions.Models; - -namespace Elsa.Workflows.Core.Models; - -public class JsonObject : MemoryBlockReference -{ - public JsonObject() - { - } - - public JsonObject(string? defaultValue) - { - DefaultValue = defaultValue; - } - - public string? Name { get; set; } - public string? DefaultValue { get; } - public override MemoryBlock Declare() => new(DefaultValue); -} \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Management/Providers/DefaultExpressionSyntaxProvider.cs b/src/modules/Elsa.Workflows.Management/Providers/DefaultExpressionSyntaxProvider.cs index 911f36392..f2f819105 100644 --- a/src/modules/Elsa.Workflows.Management/Providers/DefaultExpressionSyntaxProvider.cs +++ b/src/modules/Elsa.Workflows.Management/Providers/DefaultExpressionSyntaxProvider.cs @@ -34,7 +34,7 @@ public class DefaultExpressionSyntaxProvider : IExpressionSyntaxProvider private ExpressionSyntaxDescriptor CreateJsonDescriptor() => CreateDescriptor( "Json", CreateJsonExpression, - context => new JsonObject(context.GetExpression().Value), + context => new JsonLiteral(context.GetExpression().Value), expression => expression.Value); private ExpressionSyntaxDescriptor CreateDelegateDescriptor() => CreateDescriptor(