Fixing 2340: ModelMetadata should recompute localizable properties.
This commit is contained in:
parent
0469103683
commit
67d0bf880a
|
|
@ -14,7 +14,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
/// An implementation of <see cref="IBindingMetadataProvider"/> and <see cref="IDisplayMetadataProvider"/> for
|
||||
/// the System.ComponentModel.DataAnnotations attribute classes.
|
||||
/// </summary>
|
||||
public class DataAnnotationsMetadataProvider :
|
||||
public class DataAnnotationsMetadataProvider :
|
||||
IBindingMetadataProvider,
|
||||
IDisplayMetadataProvider,
|
||||
IValidationMetadataProvider
|
||||
|
|
@ -72,7 +72,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
// Description
|
||||
if (displayAttribute != null)
|
||||
{
|
||||
displayMetadata.Description = displayAttribute.GetDescription();
|
||||
displayMetadata.Description = () => displayAttribute.GetDescription();
|
||||
}
|
||||
|
||||
// DisplayFormat
|
||||
|
|
@ -84,7 +84,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
// DisplayName
|
||||
if (displayAttribute != null)
|
||||
{
|
||||
displayMetadata.DisplayName = displayAttribute.GetName();
|
||||
displayMetadata.DisplayName = () => displayAttribute.GetName();
|
||||
}
|
||||
|
||||
if (displayFormatAttribute != null && displayFormatAttribute.ApplyFormatInEditMode)
|
||||
|
|
@ -216,8 +216,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
|
||||
foreach (var attribute in context.Attributes.OfType<ValidationAttribute>())
|
||||
{
|
||||
// If another provider has already added this attribute, do not repeat it.
|
||||
// This will prevent attributes like RemoteAttribute (which implement ValidationAttribute and
|
||||
// If another provider has already added this attribute, do not repeat it.
|
||||
// This will prevent attributes like RemoteAttribute (which implement ValidationAttribute and
|
||||
// IClientModelValidator) to be added to the ValidationMetadata twice.
|
||||
// This is to ensure we do not end up with duplication validation rules on the client side.
|
||||
if (!context.ValidationMetadata.ValidatorMetadata.Contains(attribute))
|
||||
|
|
|
|||
|
|
@ -179,7 +179,12 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
{
|
||||
get
|
||||
{
|
||||
return DisplayMetadata.Description;
|
||||
if (DisplayMetadata.Description == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return DisplayMetadata.Description();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -197,7 +202,12 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
{
|
||||
get
|
||||
{
|
||||
return DisplayMetadata.DisplayName;
|
||||
if (DisplayMetadata.DisplayName == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return DisplayMetadata.DisplayName();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
||||
|
|
@ -28,10 +29,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
public string DataTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a model description.
|
||||
/// See <see cref="ModelMetadata.Description"/>
|
||||
/// Gets or sets a delegate which is used to get a value for the
|
||||
/// model description. See <see cref="ModelMetadata.Description"/>.
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
public Func<string> Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a display format string for the model.
|
||||
|
|
@ -40,10 +41,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
public string DisplayFormatString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a display name for the model.
|
||||
/// See <see cref="ModelMetadata.DisplayName"/>
|
||||
/// Gets or sets a delegate delegate which is used to get a value for the
|
||||
/// display name of the model. See <see cref="ModelMetadata.DisplayName"/>.
|
||||
/// </summary>
|
||||
public string DisplayName { get; set; }
|
||||
public Func<string> DisplayName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets an edit format string for the model.
|
||||
|
|
|
|||
|
|
@ -20,8 +20,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
{
|
||||
{ new DataTypeAttribute(DataType.Duration), d => d.DataTypeName, DataType.Duration.ToString() },
|
||||
|
||||
{ new DisplayAttribute() { Description = "d" }, d => d.Description, "d" },
|
||||
{ new DisplayAttribute() { Name = "DN" }, d => d.DisplayName, "DN" },
|
||||
{ new DisplayAttribute() { Description = "d" }, d => d.Description(), "d" },
|
||||
{ new DisplayAttribute() { Name = "DN" }, d => d.DisplayName(), "DN" },
|
||||
{ new DisplayAttribute() { Order = 3 }, d => d.Order, 3 },
|
||||
|
||||
{ new DisplayColumnAttribute("Property"), d => d.SimpleDisplayProperty, "Property" },
|
||||
|
|
@ -175,7 +175,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
provider.GetDisplayMetadata(context);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("name from resources", context.DisplayMetadata.DisplayName);
|
||||
Assert.Equal("name from resources", context.DisplayMetadata.DisplayName());
|
||||
}
|
||||
|
||||
// This is IMPORTANT. Product code needs to use GetDescription() instead of .Description. It's easy to regress.
|
||||
|
|
@ -204,7 +204,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
provider.GetDisplayMetadata(context);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("description from resources", context.DisplayMetadata.Description);
|
||||
Assert.Equal("description from resources", context.DisplayMetadata.Description());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ using System.ComponentModel.DataAnnotations;
|
|||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -420,6 +422,58 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
|
|||
Assert.Equal("description", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisplayName_FromResources_GetsRecomputed()
|
||||
{
|
||||
// Arrange
|
||||
var display = new DisplayAttribute()
|
||||
{
|
||||
Name = nameof(TestResources.DisplayAttribute_CultureSensitiveName),
|
||||
ResourceType = typeof(TestResources),
|
||||
};
|
||||
|
||||
var provider = CreateProvider(new[] { display });
|
||||
var metadata = provider.GetMetadataForType(typeof(string));
|
||||
|
||||
// Act & Assert
|
||||
var cultures = new[] { "fr-FR", "en-US", "en-GB" };
|
||||
foreach (var culture in cultures)
|
||||
{
|
||||
using (new CultureReplacer(uiCulture: culture))
|
||||
{
|
||||
// Later iterations ensure value is recomputed.
|
||||
var result = metadata.DisplayName;
|
||||
Assert.Equal("name from resources" + culture, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Description_FromResources_GetsRecomputed()
|
||||
{
|
||||
// Arrange
|
||||
var display = new DisplayAttribute()
|
||||
{
|
||||
Description = nameof(TestResources.DisplayAttribute_CultureSensitiveDescription),
|
||||
ResourceType = typeof(TestResources),
|
||||
};
|
||||
|
||||
var provider = CreateProvider(new[] { display });
|
||||
var metadata = provider.GetMetadataForType(typeof(string));
|
||||
|
||||
// Act & Assert
|
||||
var cultures = new[] { "fr-FR", "en-US", "en-GB" };
|
||||
foreach (var culture in cultures)
|
||||
{
|
||||
using (new CultureReplacer(uiCulture: culture))
|
||||
{
|
||||
// Later iterations ensure value is recomputed.
|
||||
var result = metadata.Description;
|
||||
Assert.Equal("description from resources" + culture, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DataTypeName_Null_IfHtmlEncodeTrue()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Threading;
|
||||
using Microsoft.AspNet.Mvc.Core.Test;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||
|
|
@ -12,5 +13,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
|||
public static string DisplayAttribute_Description { get; } = Resources.DisplayAttribute_Description;
|
||||
|
||||
public static string DisplayAttribute_Name { get; } = Resources.DisplayAttribute_Name;
|
||||
|
||||
public static string DisplayAttribute_CultureSensitiveName =>
|
||||
Resources.DisplayAttribute_Name + Thread.CurrentThread.CurrentUICulture;
|
||||
|
||||
public static string DisplayAttribute_CultureSensitiveDescription =>
|
||||
Resources.DisplayAttribute_Description + Thread.CurrentThread.CurrentUICulture;
|
||||
}
|
||||
}
|
||||
|
|
@ -97,7 +97,7 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
var provider = new TestModelMetadataProvider();
|
||||
provider
|
||||
.ForType<DefaultTemplatesUtilities.ObjectTemplateModel>()
|
||||
.DisplayDetails(dd => dd.DisplayName = displayName);
|
||||
.DisplayDetails(dd => dd.DisplayName = () => displayName);
|
||||
|
||||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);
|
||||
var enumerableHelper = DefaultTemplatesUtilities.GetHtmlHelperForEnumerable(provider: provider);
|
||||
|
|
@ -124,7 +124,7 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
var provider = new TestModelMetadataProvider();
|
||||
provider
|
||||
.ForProperty<DefaultTemplatesUtilities.ObjectTemplateModel>("Property1")
|
||||
.DisplayDetails(dd => dd.DisplayName = displayName);
|
||||
.DisplayDetails(dd => dd.DisplayName = () => displayName);
|
||||
|
||||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);
|
||||
var enumerableHelper = DefaultTemplatesUtilities.GetHtmlHelperForEnumerable(provider: provider);
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
var provider = new TestModelMetadataProvider();
|
||||
provider
|
||||
.ForType<DefaultTemplatesUtilities.ObjectTemplateModel>()
|
||||
.DisplayDetails(dd => dd.DisplayName = string.Empty);
|
||||
.DisplayDetails(dd => dd.DisplayName = () => string.Empty);
|
||||
|
||||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);
|
||||
|
||||
|
|
@ -136,7 +136,7 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
var provider = new TestModelMetadataProvider();
|
||||
provider
|
||||
.ForType<DefaultTemplatesUtilities.ObjectTemplateModel>()
|
||||
.DisplayDetails(dd => dd.DisplayName = displayName);
|
||||
.DisplayDetails(dd => dd.DisplayName = () => displayName);
|
||||
|
||||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);
|
||||
|
||||
|
|
@ -158,7 +158,7 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
var provider = new TestModelMetadataProvider();
|
||||
provider
|
||||
.ForProperty<DefaultTemplatesUtilities.ObjectTemplateModel>("Property1")
|
||||
.DisplayDetails(dd => dd.DisplayName = string.Empty);
|
||||
.DisplayDetails(dd => dd.DisplayName = () => string.Empty);
|
||||
|
||||
var modelExplorer = provider
|
||||
.GetModelExplorerForType(typeof(DefaultTemplatesUtilities.ObjectTemplateModel), model: null)
|
||||
|
|
@ -188,7 +188,7 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
var provider = new TestModelMetadataProvider();
|
||||
provider
|
||||
.ForProperty<DefaultTemplatesUtilities.ObjectTemplateModel>("Property1")
|
||||
.DisplayDetails(dd => dd.DisplayName = displayName);
|
||||
.DisplayDetails(dd => dd.DisplayName = () => displayName);
|
||||
|
||||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue