Apply improved JSON serialization settings to workflow definition API endpoints

This commit is contained in:
Sipke Schoorstra 2021-10-08 15:03:25 +02:00
parent 23592c099d
commit 0be4d3e156
8 changed files with 36 additions and 16 deletions

View file

@ -248,7 +248,6 @@ export class ElsaWorkflowDefinitionEditorScreen {
let workflowDefinition = this.workflowDefinition;
const isNew = typeof workflowDefinition.definitionId === 'undefined' && typeof this.workflowDefinitionId === 'undefined';
debugger;
const request: SaveWorkflowDefinitionRequest = {
workflowDefinitionId: workflowDefinition.definitionId || this.workflowDefinitionId,
contextOptions: workflowDefinition.contextOptions,

View file

@ -41,13 +41,7 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
public async Task<IActionResult> Handle(string workflowDefinitionId, VersionOptions versionOptions, CancellationToken cancellationToken = default)
{
var workflowDefinition = await _workflowDefinitionStore.FindAsync(new WorkflowDefinitionIdSpecification(workflowDefinitionId, versionOptions), cancellationToken);
// Here we don't want to use the `PreserveReferencesHandling` setting because the model will be used by the designer "as-is" and will not resolve $id references.
// Fixes #1605.
var settings = DefaultContentSerializer.CreateDefaultJsonSerializationSettings();
settings.PreserveReferencesHandling = PreserveReferencesHandling.None;
return workflowDefinition == null ? NotFound() : Json(workflowDefinition, settings);
return workflowDefinition == null ? NotFound() : Json(workflowDefinition, SerializationHelper.GetSettingsForWorkflowDefinition());
}
}
}

View file

@ -7,6 +7,7 @@ using Elsa.Serialization;
using Elsa.Server.Api.Swagger.Examples;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.Filters;
@ -40,7 +41,7 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
public async Task<IActionResult> Handle(string versionId, CancellationToken cancellationToken = default)
{
var workflowDefinition = await _workflowDefinitionStore.FindAsync(new WorkflowDefinitionVersionIdSpecification(versionId), cancellationToken);
return workflowDefinition == null ? NotFound() : Json(workflowDefinition, _serializer.GetSettings());
return workflowDefinition == null ? NotFound() : Json(workflowDefinition, SerializationHelper.GetSettingsForWorkflowDefinition());
}
}
}

View file

@ -14,6 +14,7 @@ using Elsa.Server.Api.Swagger.Examples;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.Filters;
@ -63,7 +64,7 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
var summaries = _mapper.Map<IList<WorkflowDefinitionSummaryModel>>(items);
var pagedList = new PagedList<WorkflowDefinitionSummaryModel>(summaries, page, pageSize, totalCount);
return Json(pagedList, _serializer.GetSettings());
return Json(pagedList, SerializationHelper.GetSettingsForWorkflowDefinition());
}
private Specification<WorkflowDefinition> GetSpecification(string? ids, VersionOptions version)

View file

@ -6,6 +6,7 @@ using Elsa.Server.Api.Swagger.Examples;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.Filters;
@ -40,14 +41,14 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
{
// Create a draft.
var draft = await _workflowPublisher.GetDraftAsync(workflowDefinitionId, cancellationToken);
if (draft == null)
return NotFound();
// Publish the draft.
var published = await _workflowPublisher.PublishAsync(draft, cancellationToken);
return AcceptedAtAction("Handle", "GetByVersionId", new { versionId = published.Id, apiVersion = apiVersion.ToString() }, published);
var publishedWorkflowDefinition = await _workflowPublisher.PublishAsync(draft, cancellationToken);
return AcceptedAtAction("Handle", "GetByVersionId", new { versionId = publishedWorkflowDefinition.Id, apiVersion = apiVersion.ToString() }, publishedWorkflowDefinition);
}
}
}

View file

@ -6,6 +6,7 @@ using Elsa.Server.Api.Swagger.Examples;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.Filters;

View file

@ -7,6 +7,7 @@ using Elsa.Server.Api.Swagger.Examples;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.Filters;
@ -20,6 +21,7 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
{
private readonly IWorkflowPublisher _workflowPublisher;
private readonly ITenantAccessor _tenantAccessor;
public Save(IWorkflowPublisher workflowPublisher, ITenantAccessor tenantAccessor)
{
_workflowPublisher = workflowPublisher;
@ -40,6 +42,7 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
{
var workflowDefinitionId = request.WorkflowDefinitionId;
var workflowDefinition = !string.IsNullOrWhiteSpace(workflowDefinitionId) ? await _workflowPublisher.GetDraftAsync(workflowDefinitionId, cancellationToken) : default;
var isNew = workflowDefinition == null;
if (workflowDefinition == null)
{
@ -69,7 +72,9 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
else
workflowDefinition = await _workflowPublisher.SaveDraftAsync(workflowDefinition, cancellationToken);
return CreatedAtAction("Handle", "GetByVersionId", new { versionId = workflowDefinition.Id, apiVersion = apiVersion.ToString() }, workflowDefinition);
return isNew
? CreatedAtAction("Handle", "GetByVersionId", new { versionId = workflowDefinition.Id, apiVersion = apiVersion.ToString() }, workflowDefinition)
: Ok(workflowDefinition);
}
private IEnumerable<ConnectionDefinition> FilterInvalidConnections(SaveWorkflowDefinitionRequest request)

View file

@ -0,0 +1,18 @@
using Elsa.Serialization;
using Newtonsoft.Json;
namespace Elsa.Server.Api
{
public static class SerializationHelper
{
public static JsonSerializerSettings GetSettingsForWorkflowDefinition()
{
// Here we don't want to use the `PreserveReferencesHandling` setting because the model will be used by the designer "as-is" and will not resolve $id references.
// Fixes #1605.
var settings = DefaultContentSerializer.CreateDefaultJsonSerializationSettings();
settings.PreserveReferencesHandling = PreserveReferencesHandling.None;
return settings;
}
}
}