From 0d92a829ff1f4e968471a592765264937c379c1c Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Wed, 1 Oct 2014 13:05:16 -0700 Subject: [PATCH] Correct minor problem in `CachedDataAnnotationsModelMetadata.ComputeDisplayFormatString()` - only affects an extreme corner case: user sets `metadata.EditFormatString` then reads `metadata.DisplayFormatString` - an extreme case because `EditFormatString` is normally set only when `DisplayFormatString` is set and, if set, it's to the same value - happened to see this while updating `CachedDataAnnotationsModelMetadata` for this PR nit: an -> a in an adjacent XML comment in `CachedDataAnnotationsModelMetadata` --- .../CachedDataAnnotationsModelMetadata.cs | 10 ++--- .../CachedDataAnnotationsModelMetadataTest.cs | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs index 78decd573f..c9622d8608 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Metadata/CachedDataAnnotationsModelMetadata.cs @@ -81,19 +81,19 @@ namespace Microsoft.AspNet.Mvc.ModelBinding } /// - /// Calculate based on presence of an + /// Calculate based on presence of a /// and its value. /// /// /// Calculated value. - /// if an exists. + /// if a exists. /// null otherwise. /// protected override string ComputeDisplayFormatString() { return PrototypeCache.DisplayFormat != null ? PrototypeCache.DisplayFormat.DataFormatString - : base.ComputeEditFormatString(); + : base.ComputeDisplayFormatString(); } protected override string ComputeDisplayName() @@ -114,13 +114,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding } /// - /// Calculate based on presence of an + /// Calculate based on presence of a /// and its and /// values. /// /// /// Calculated value. - /// if an exists and + /// if a exists and /// its is true; null otherwise. /// /// diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/CachedDataAnnotationsModelMetadataTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/CachedDataAnnotationsModelMetadataTest.cs index 31443607b0..d042fbdc0b 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/CachedDataAnnotationsModelMetadataTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Metadata/CachedDataAnnotationsModelMetadataTest.cs @@ -321,6 +321,44 @@ namespace Microsoft.AspNet.Mvc.ModelBinding Assert.Equal(expected, result); } + [Fact] + private void EditFormatString_DoesNotAffectDisplayFormat() + { + // Arrange + var provider = new DataAnnotationsModelMetadataProvider(); + var metadata = new CachedDataAnnotationsModelMetadata( + provider, + containerType: null, + modelType: typeof(object), + propertyName: null, + attributes: Enumerable.Empty()); + + // Act + metadata.EditFormatString = "custom format"; + + // Assert + Assert.Null(metadata.DisplayFormatString); + } + + [Fact] + private void DisplayFormatString_DoesNotAffectEditFormat() + { + // Arrange + var provider = new DataAnnotationsModelMetadataProvider(); + var metadata = new CachedDataAnnotationsModelMetadata( + provider, + containerType: null, + modelType: typeof(object), + propertyName: null, + attributes: Enumerable.Empty()); + + // Act + metadata.DisplayFormatString = "custom format"; + + // Assert + Assert.Null(metadata.EditFormatString); + } + private class DataTypeWithCustomDisplayFormat : DataTypeAttribute { public DataTypeWithCustomDisplayFormat() : base("Custom datatype")