Gets body model binders working per controller and figures out how the application model works

This commit is contained in:
Shannon
2020-05-12 12:29:03 +10:00
parent 39fed867c1
commit 3add4746fe
5 changed files with 43 additions and 114 deletions

View File

@@ -5,23 +5,9 @@ using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Web.Common.Attributes;
using Umbraco.Web.Common.ModelBinding;
namespace Umbraco.Web.Common.ApplicationModels
{
public class NewtonsoftJsonModelBinderConvention : IActionModelConvention
{
public void Apply(ActionModel action)
{
foreach(var p in action.Parameters)
{
if (p.BindingInfo?.BindingSource == BindingSource.Body)
{
p.BindingInfo.BinderType = typeof(UmbracoJsonModelBinder);
}
}
}
}
/// <summary>
/// A custom application model provider for Umbraco controllers
@@ -34,6 +20,10 @@ namespace Umbraco.Web.Common.ApplicationModels
/// This is nearly a copy of aspnetcore's ApiBehaviorApplicationModelProvider which supplies a convention for the
/// [ApiController] attribute, however that convention is too strict for our purposes so we will have our own.
/// </para>
/// <para>
/// See https://shazwazza.com/post/custom-body-model-binding-per-controller-in-asp-net-core/
/// and https://github.com/dotnet/aspnetcore/issues/21724
/// </para>
/// </remarks>
public class UmbracoApiBehaviorApplicationModelProvider : IApplicationModelProvider
{
@@ -50,7 +40,9 @@ namespace Umbraco.Web.Common.ApplicationModels
new ConsumesConstraintForFormFileParameterConvention(), // If an controller accepts files, it must accept multipart/form-data.
new InferParameterBindingInfoConvention(modelMetadataProvider), // no need for [FromBody] everywhere, A complex type parameter is assigned to FromBody
new NewtonsoftJsonModelBinderConvention()
// This ensures that all parameters of type BindingSource.Body (based on the above InferParameterBindingInfoConvention) are bound
// using our own UmbracoJsonModelBinder
new UmbracoJsonModelBinderConvention()
};
// TODO: Need to determine exactly how this affects errors

View File

@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Umbraco.Web.Common.ModelBinding;
using System.Linq;
namespace Umbraco.Web.Common.ApplicationModels
{
/// <summary>
/// Applies the <see cref="UmbracoJsonModelBinder"/> body model binder to any parameter binding source of type <see cref="BindingSource.Body"/>
/// </summary>
/// <remarks>
/// For this to work Microsoft's own <see cref="InferParameterBindingInfoConvention"/> convention must be executed before this one
/// </remarks>
public class UmbracoJsonModelBinderConvention : IActionModelConvention
{
public void Apply(ActionModel action)
{
foreach (var p in action.Parameters.Where(p => p.BindingInfo?.BindingSource == BindingSource.Body))
{
p.BindingInfo.BinderType = typeof(UmbracoJsonModelBinder);
}
}
}
}

View File

@@ -34,9 +34,7 @@ namespace Umbraco.Extensions
public static IServiceCollection AddUmbracoWebComponents(this IServiceCollection services)
{
services.TryAddSingleton<UmbracoJsonModelBinder>();
services.TryAddSingleton<UmbracoJsonModelBinderProvider>();
services.TryAddSingleton<UmbracoJsonModelBinderFactory>();
//services.ConfigureOptions<UmbracoMvcConfigureOptions>();
services.ConfigureOptions<UmbracoMvcConfigureOptions>();
services.TryAddEnumerable(ServiceDescriptor.Transient<IApplicationModelProvider, UmbracoApiBehaviorApplicationModelProvider>());
// TODO: We need to avoid this, surely there's a way? See ContainerTests.BuildServiceProvider_Before_Host_Is_Configured
@@ -118,7 +116,7 @@ namespace Umbraco.Extensions
}
/// <summary>
/// Options for configuring MVC
/// Options for globally configuring MVC for Umbraco
/// </summary>
/// <remarks>
/// We generally don't want to change the global MVC settings since we want to be unobtrusive as possible but some
@@ -126,22 +124,16 @@ namespace Umbraco.Extensions
/// </remarks>
private class UmbracoMvcConfigureOptions : IConfigureOptions<MvcOptions>
{
private readonly IHttpRequestStreamReaderFactory _readerFactory;
private readonly ILoggerFactory _logger;
private readonly ArrayPool<char> _arrayPool;
private readonly ObjectPoolProvider _objectPoolProvider;
public UmbracoMvcConfigureOptions(IHttpRequestStreamReaderFactory readerFactory, ILoggerFactory logger, ArrayPool<char> arrayPool, ObjectPoolProvider objectPoolProvider)
{
_readerFactory = readerFactory;
_logger = logger;
_arrayPool = arrayPool;
_objectPoolProvider = objectPoolProvider;
// TODO: we can inject params with DI here
public UmbracoMvcConfigureOptions()
{
}
// TODO: we can configure global mvc options here if we need to
public void Configure(MvcOptions options)
{
options.ModelBinderProviders.Insert(0, new UmbracoJsonModelBinderProvider(_readerFactory, _logger, _arrayPool, _objectPoolProvider));
}
}

View File

@@ -34,12 +34,10 @@ namespace Umbraco.Web.Common.Install
private readonly ILogger _logger;
private readonly IProfilingLogger _proflog;
public InstallApiController(UmbracoJsonModelBinderFactory modelBinderFactory, DatabaseBuilder databaseBuilder, IProfilingLogger proflog,
public InstallApiController(DatabaseBuilder databaseBuilder, IProfilingLogger proflog,
InstallHelper installHelper, InstallStepCollection installSteps, InstallStatusTracker installStatusTracker,
IUmbracoApplicationLifetime umbracoApplicationLifetime)
{
ModelBinderFactory = modelBinderFactory;
_databaseBuilder = databaseBuilder ?? throw new ArgumentNullException(nameof(databaseBuilder));
_proflog = proflog ?? throw new ArgumentNullException(nameof(proflog));
_installSteps = installSteps;

View File

@@ -5,53 +5,13 @@ using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Options;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Web.Common.Controllers;
namespace Umbraco.Web.Common.ModelBinding
{
public class UmbracoJsonModelBinderFactory : ModelBinderFactory
{
public UmbracoJsonModelBinderFactory(
UmbracoJsonModelBinderProvider umbracoJsonModelBinderProvider,
IModelMetadataProvider metadataProvider,
IOptions<MvcOptions> options,
IServiceProvider serviceProvider)
: base(metadataProvider, GetOptions(options, umbracoJsonModelBinderProvider), serviceProvider)
{
}
private static IOptions<MvcOptions> GetOptions(IOptions<MvcOptions> options, UmbracoJsonModelBinderProvider umbracoJsonModelBinderProvider)
{
// copy to new collection
var providers = options.Value.ModelBinderProviders.ToList();
// remove the default
providers.RemoveType<BodyModelBinderProvider>();
// prepend our own
providers.Insert(0, umbracoJsonModelBinderProvider);
var newOptions = new MvcOptions();
foreach (var p in providers)
newOptions.ModelBinderProviders.Add(p);
return new CustomOptions(newOptions);
}
private class CustomOptions : IOptions<MvcOptions>
{
public CustomOptions(MvcOptions options)
{
Value = options;
}
public MvcOptions Value { get; }
}
}
/// <summary>
/// A custom body model binder that only uses a <see cref="NewtonsoftJsonInputFormatter"/> to bind body action parameters
/// </summary>
public class UmbracoJsonModelBinder : BodyModelBinder, IModelBinder
{
public UmbracoJsonModelBinder(ArrayPool<char> arrayPool, ObjectPoolProvider objectPoolProvider, IHttpRequestStreamReaderFactory readerFactory, ILoggerFactory loggerFactory)
@@ -59,7 +19,7 @@ namespace Umbraco.Web.Common.ModelBinding
{
}
internal static IInputFormatter[] GetNewtonsoftJsonFormatter(ILoggerFactory logger, ArrayPool<char> arrayPool, ObjectPoolProvider objectPoolProvider)
private static IInputFormatter[] GetNewtonsoftJsonFormatter(ILoggerFactory logger, ArrayPool<char> arrayPool, ObjectPoolProvider objectPoolProvider)
{
var jsonOptions = new MvcNewtonsoftJsonOptions
{
@@ -76,42 +36,5 @@ namespace Umbraco.Web.Common.ModelBinding
jsonOptions)
};
}
Task IModelBinder.BindModelAsync(ModelBindingContext bindingContext)
{
return BindModelAsync(bindingContext);
}
}
/// <summary>
/// A model binder for Umbraco models that require Newtonsoft Json serializers
/// </summary>
public class UmbracoJsonModelBinderProvider : BodyModelBinderProvider, IModelBinderProvider
{
public UmbracoJsonModelBinderProvider(IHttpRequestStreamReaderFactory readerFactory, ILoggerFactory logger, ArrayPool<char> arrayPool, ObjectPoolProvider objectPoolProvider)
: base(UmbracoJsonModelBinder.GetNewtonsoftJsonFormatter(logger, arrayPool, objectPoolProvider), readerFactory)
{
}
/// <summary>
/// Returns the model binder if it's for Umbraco models
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
IModelBinder IModelBinderProvider.GetBinder(ModelBinderProviderContext context)
{
// Must be 'Body' (json) binding
if (context.BindingInfo.BindingSource != BindingSource.Body)
return null;
if (context.Metadata?.UnderlyingOrModelType.Assembly == typeof(UmbracoJsonModelBinderProvider).Assembly // Umbraco.Web.Common
|| context.Metadata?.UnderlyingOrModelType.Assembly == typeof(IRuntimeState).Assembly // Umbraco.Core
|| context.Metadata?.UnderlyingOrModelType.Assembly == typeof(RuntimeState).Assembly) // Umbraco.Infrastructure
{
return GetBinder(context);
}
return null;
}
}
}