Merge remote-tracking branch 'origin/v11/dev' into v11/dev

This commit is contained in:
Bjarke Berg
2022-09-19 12:36:52 +02:00
4 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
namespace Umbraco.Extensions;
internal static class TypeExtensions
{
public static bool IsRenderingModel(this Type type)
=> typeof(ContentModel).IsAssignableFrom(type) || typeof(IPublishedContent).IsAssignableFrom(type);
}

View File

@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Web.Common.Filters;
using Umbraco.Cms.Web.Common.ModelBinders;
using Umbraco.Cms.Web.Common.Validators;
namespace Umbraco.Cms.Web.Common.Mvc;
@@ -18,6 +19,8 @@ public class UmbracoMvcConfigureOptions : IConfigureOptions<MvcOptions>
public void Configure(MvcOptions options)
{
options.ModelBinderProviders.Insert(0, new ContentModelBinderProvider());
options.ModelValidatorProviders.Insert(0, new BypassRenderingModelValidatorProvider());
options.ModelMetadataDetailsProviders.Add(new BypassRenderingModelValidationMetadataProvider());
options.Filters.Insert(0, new EnsurePartialViewMacroViewContextFilterAttribute());
}
}

View File

@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Common.Validators;
/// <summary>
/// Ensures we bypass object graph validation for rendering models.
/// </summary>
internal class BypassRenderingModelValidationMetadataProvider : IValidationMetadataProvider
{
public void CreateValidationMetadata(ValidationMetadataProviderContext context)
{
if (context.Key.ModelType.IsRenderingModel())
{
context.ValidationMetadata.ValidateChildren = false;
}
}
}

View File

@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Common.Validators;
/// <summary>
/// Ensures we bypass property validation for rendering models.
/// </summary>
internal class BypassRenderingModelValidatorProvider : IModelValidatorProvider
{
public void CreateValidators(ModelValidatorProviderContext context)
{
if (context.ModelMetadata.ModelType.IsRenderingModel())
{
context.Results.Clear();
context.Results.Add(new ValidatorItem
{
Validator = null,
IsReusable = true
});
}
}
}