Add Variable available in dropdown when configuring a activity input (#5918)

* Sample to provide a ExpressionDescriptorProvider

* fix variable accessor in VariableExpressionHandler

* remove Variable Expression to use default one

* Change the Variable Expression Provider to handle Variable Expression correctly

* need to check on UI Side and Server side is valid JSON is sent. For example, if a use change from a literal value to a Variable value, the value is sent to the API and result in an exception, we cannot handle invalid json  without using Try/Catch

---------

Co-authored-by: Jérémie DEVILLARD <jdevillard@users.noreply.github.com>
Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com>
This commit is contained in:
jdevillard 2024-08-30 12:01:44 +02:00 committed by GitHub
parent 684717d180
commit 3083a400d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 9 deletions

View file

@ -1,3 +1,4 @@
using Elsa.EntityFrameworkCore.Extensions;
using Elsa.EntityFrameworkCore.Modules.Management;
using Elsa.EntityFrameworkCore.Modules.Runtime;
using Elsa.Extensions;
@ -7,8 +8,8 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddElsa(elsa =>
{
elsa.UseWorkflowManagement(management => management.UseEntityFrameworkCore());
elsa.UseWorkflowRuntime(runtime => runtime.UseEntityFrameworkCore());
elsa.UseWorkflowManagement(management => management.UseEntityFrameworkCore(ef => ef.UseSqlite()));
elsa.UseWorkflowRuntime(runtime => runtime.UseEntityFrameworkCore(ef=>ef.UseSqlite()));
elsa.UseWorkflowsApi();
elsa.UseHttp();
@ -26,12 +27,12 @@ builder.Services.AddElsa(elsa =>
});
elsa.UseDefaultAuthentication();
elsa.AddActivity<VehicleActivity>();
elsa.AddActivity<VehicleActivity>();
});
builder.Services.AddSingleton<VehicleUIHandler>();
builder.Services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()));
var app = builder.Build();
// Configure the HTTP request pipeline.
@ -41,4 +42,5 @@ app.UseAuthentication();
app.UseAuthorization();
app.UseWorkflowsApi();
app.Map("/test",c=> c.UseWorkflows());
app.Run();
app.Run();

View file

@ -49,13 +49,23 @@ public class DefaultExpressionDescriptorProvider : IExpressionDescriptorProvider
return CreateDescriptor<VariableExpressionHandler>(
"Variable",
"Variable",
isBrowsable: false,
isBrowsable: true,
memoryBlockReferenceFactory: () => new Variable(),
deserialize: context =>
{
var valueElement = context.JsonElement.TryGetProperty("value", out var v) ? v : default;
var value = valueElement.Deserialize(context.MemoryBlockType, context.Options);
return new Expression("Variable", value);
var valueElement = context.JsonElement.TryGetProperty("value", out var v) ? v : default;
var valueString = valueElement.GetValue()?.ToString();
try
{
var value = JsonSerializer.Deserialize(valueString, context.MemoryBlockType, context.Options);
return new Expression("Variable", value);
}
catch (Exception)
{
return new Expression("Variable", null);
}
}
);
}