Use endpoint-specific JSON serialization settings, also fixing that enum values were converted to numbers instead of strings

This commit is contained in:
Sipke Schoorstra 2021-04-28 22:24:14 +02:00
parent 90bc936f42
commit 7a70b56ec2
6 changed files with 58 additions and 11 deletions

View file

@ -3,6 +3,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Metadata;
using Elsa.Server.Api.Services;
using Elsa.Services;
using Elsa.Services.Models;
using Microsoft.AspNetCore.Http;
@ -18,10 +19,12 @@ namespace Elsa.Server.Api.Endpoints.Activities
public class List : Controller
{
private readonly IActivityTypeService _activityTypeService;
private readonly IEndpointContentSerializerSettingsProvider _serializerSettingsProvider;
public List(IActivityTypeService activityTypeService)
public List(IActivityTypeService activityTypeService, IEndpointContentSerializerSettingsProvider serializerSettingsProvider)
{
_activityTypeService = activityTypeService;
_serializerSettingsProvider = serializerSettingsProvider;
}
[HttpGet]
@ -37,7 +40,7 @@ namespace Elsa.Server.Api.Endpoints.Activities
var activityTypes = await _activityTypeService.GetActivityTypesAsync(cancellationToken);
var tasks = activityTypes.Where(x => x.IsBrowsable).Select(x => DescribeActivity(x, cancellationToken)).ToList();
var descriptors = await Task.WhenAll(tasks);
return Json(descriptors);
return Json(descriptors, _serializerSettingsProvider.GetSettings());
}
private async Task<ActivityDescriptor> DescribeActivity(ActivityType activityType, CancellationToken cancellationToken) => await _activityTypeService.DescribeActivityType(activityType, cancellationToken);

View file

@ -6,6 +6,7 @@ using Elsa.Activities.Webhooks.Persistence;
using Elsa.Models;
using Elsa.Persistence.Specifications;
using Elsa.Serialization;
using Elsa.Server.Api.Services;
using Elsa.Server.Api.Swagger.Examples;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@ -21,12 +22,12 @@ namespace Elsa.Server.Api.Endpoints.WebhookDefinitions
public class Get : Controller
{
private readonly IWebhookDefinitionStore _webhookDefinitionStore;
private readonly IContentSerializer _serializer;
private readonly IEndpointContentSerializerSettingsProvider _serializerSettingsProvider;
public Get(IWebhookDefinitionStore webhookDefinitionStore, IContentSerializer serializer)
public Get(IWebhookDefinitionStore webhookDefinitionStore, IEndpointContentSerializerSettingsProvider serializerSettingsProvider)
{
_webhookDefinitionStore = webhookDefinitionStore;
_serializer = serializer;
_serializerSettingsProvider = serializerSettingsProvider;
}
[HttpGet]
@ -42,7 +43,7 @@ namespace Elsa.Server.Api.Endpoints.WebhookDefinitions
public async Task<IActionResult> Handle(string webhookDefinitionId, CancellationToken cancellationToken = default)
{
var webhookDefinition = await _webhookDefinitionStore.FindAsync(new EntityIdSpecification<WebhookDefinition>(webhookDefinitionId), cancellationToken);
return webhookDefinition == null ? (IActionResult) NotFound() : Json(webhookDefinition, _serializer.GetSettings());
return webhookDefinition == null ? (IActionResult) NotFound() : Json(webhookDefinition, _serializerSettingsProvider.GetSettings());
}
}
}

View file

@ -8,6 +8,7 @@ using Elsa.Models;
using Elsa.Persistence.Specifications;
using Elsa.Serialization;
using Elsa.Server.Api.Models;
using Elsa.Server.Api.Services;
using Elsa.Server.Api.Swagger.Examples;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@ -23,12 +24,12 @@ namespace Elsa.Server.Api.Endpoints.WebhookDefinitions
public class List : Controller
{
private readonly IWebhookDefinitionStore _webhookDefinitionStore;
private readonly IContentSerializer _serializer;
private readonly IEndpointContentSerializerSettingsProvider _serializerSettingsProvider;
public List(IWebhookDefinitionStore webhookDefinitionStore, IContentSerializer serializer)
public List(IWebhookDefinitionStore webhookDefinitionStore, IEndpointContentSerializerSettingsProvider serializerSettingsProvider)
{
_webhookDefinitionStore = webhookDefinitionStore;
_serializer = serializer;
_serializerSettingsProvider = serializerSettingsProvider;
}
[HttpGet]
@ -45,7 +46,7 @@ namespace Elsa.Server.Api.Endpoints.WebhookDefinitions
var specification = Specification<WebhookDefinition>.Identity;
var items = await _webhookDefinitionStore.FindManyAsync(specification, cancellationToken: cancellationToken);
return Json(items, _serializer.GetSettings());
return Json(items, _serializerSettingsProvider.GetSettings());
}
}
}

View file

@ -19,7 +19,6 @@ namespace Microsoft.Extensions.DependencyInjection
{
setupNewtonsoftJson ??= _ => { };
services.AddControllers().AddNewtonsoftJson(setupNewtonsoftJson);
services.AddRouting(options => options.LowercaseUrls = true);
services.AddVersionedApiExplorer(o =>
@ -39,6 +38,7 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddSingleton<ConnectionConverter>();
services.AddSingleton<ActivityBlueprintConverter>();
services.AddSingleton<IWorkflowBlueprintMapper, WorkflowBlueprintMapper>();
services.AddSingleton<IEndpointContentSerializerSettingsProvider, EndpointContentSerializerSettingsProvider>();
services.AddAutoMapperProfile<AutoMapperProfile>();
return services;
}

View file

@ -0,0 +1,33 @@
using Elsa.Serialization.Converters;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NodaTime;
using NodaTime.Serialization.JsonNet;
namespace Elsa.Server.Api.Services
{
public class EndpointContentSerializerSettingsProvider : IEndpointContentSerializerSettingsProvider
{
private readonly JsonSerializerSettings _serializerSettings;
public EndpointContentSerializerSettingsProvider()
{
_serializerSettings = new JsonSerializerSettings();
_serializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
_serializerSettings.NullValueHandling = NullValueHandling.Ignore;
_serializerSettings.TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple;
_serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy
{
ProcessDictionaryKeys = false,
ProcessExtensionDataNames = true,
OverrideSpecifiedNames = false
}
};
_serializerSettings.Converters.Add(new FlagEnumConverter(new DefaultNamingStrategy()));
}
public JsonSerializerSettings GetSettings() => _serializerSettings;
}
}

View file

@ -0,0 +1,9 @@
using Newtonsoft.Json;
namespace Elsa.Server.Api.Services
{
public interface IEndpointContentSerializerSettingsProvider
{
JsonSerializerSettings GetSettings();
}
}