* Run code cleanup * Run dotnet format * Start manual cleanup in Web.Common * Finish up manual cleanup * Fix tests * Fix up InMemoryModelFactory.cs * Inject proper macroRenderer * Update src/Umbraco.Web.Common/Filters/JsonDateTimeFormatAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Web.Common/Filters/ValidateUmbracoFormRouteStringAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix based on review Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
59 lines
1.9 KiB
C#
59 lines
1.9 KiB
C#
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Umbraco.Cms.Web.Common.DependencyInjection;
|
|
using Umbraco.Cms.Web.Common.ModelBinders;
|
|
|
|
namespace Umbraco.Cms.Web.Common.ApplicationModels;
|
|
|
|
/// <summary>
|
|
/// Applies the <see cref="UmbracoJsonModelBinder" /> body model binder to any complex parameter and those with a
|
|
/// binding source of type <see cref="BindingSource.Body" />
|
|
/// </summary>
|
|
public class UmbracoJsonModelBinderConvention : IActionModelConvention
|
|
{
|
|
private readonly IModelMetadataProvider _modelMetadataProvider;
|
|
|
|
public UmbracoJsonModelBinderConvention()
|
|
: this(StaticServiceProvider.Instance.GetRequiredService<IModelMetadataProvider>())
|
|
{
|
|
}
|
|
|
|
public UmbracoJsonModelBinderConvention(IModelMetadataProvider modelMetadataProvider) =>
|
|
_modelMetadataProvider = modelMetadataProvider;
|
|
|
|
/// <inheritdoc />
|
|
public void Apply(ActionModel action)
|
|
{
|
|
foreach (ParameterModel p in action.Parameters)
|
|
{
|
|
if (p.BindingInfo == null)
|
|
{
|
|
if (IsComplexTypeParameter(p))
|
|
{
|
|
p.BindingInfo = new BindingInfo
|
|
{
|
|
BindingSource = BindingSource.Body,
|
|
BinderType = typeof(UmbracoJsonModelBinder),
|
|
};
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
if (p.BindingInfo.BindingSource == BindingSource.Body)
|
|
{
|
|
p.BindingInfo.BinderType = typeof(UmbracoJsonModelBinder);
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool IsComplexTypeParameter(ParameterModel parameter)
|
|
{
|
|
// No need for information from attributes on the parameter. Just use its type.
|
|
ModelMetadata metadata = _modelMetadataProvider.GetMetadataForType(parameter.ParameterInfo.ParameterType);
|
|
|
|
return metadata.IsComplexType;
|
|
}
|
|
}
|