protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName) { Func <IEnumerable <Attribute>, ModelMetadata> metadataFactory = (attr) => base.CreateMetadata(attr, containerType, modelAccessor, modelType, propertyName); var conventionType = containerType ?? modelType; Type defaultResourceType = DefaultResourceType; MetadataConventionsAttribute conventionAttribute = conventionType.GetAttributeOnTypeOrAssembly <MetadataConventionsAttribute>(); if (conventionAttribute != null && conventionAttribute.ResourceType != null) { defaultResourceType = conventionAttribute.ResourceType; } else if (RequireConventionAttribute) { return(metadataFactory(attributes)); } ApplyConventionsToValidationAttributes(attributes, containerType, propertyName, defaultResourceType); var foundDisplayAttribute = attributes.FirstOrDefault(a => typeof(DisplayAttribute) == a.GetType()) as DisplayAttribute; if (foundDisplayAttribute.CanSupplyDisplayName()) { return(metadataFactory(attributes)); } // Our displayAttribute is lacking. Time to get busy. DisplayAttribute displayAttribute = foundDisplayAttribute.Copy() ?? new DisplayAttribute(); var rewrittenAttributes = attributes.Replace(foundDisplayAttribute, displayAttribute); // ensure resource type. displayAttribute.ResourceType = displayAttribute.ResourceType ?? defaultResourceType; if (displayAttribute.ResourceType != null) { // ensure resource name string displayAttributeName = GetDisplayAttributeName(containerType, propertyName, displayAttribute); if (displayAttributeName != null) { displayAttribute.Name = displayAttributeName; } if (!displayAttribute.ResourceType.PropertyExists(displayAttribute.Name)) { displayAttribute.ResourceType = null; } } var metadata = metadataFactory(rewrittenAttributes); if (metadata.DisplayName == null || metadata.DisplayName == metadata.PropertyName) { metadata.DisplayName = metadata.PropertyName.SplitUpperCaseToString(); } return(metadata); }
public void First_WithNoMatchingAttribute_ReturnsNull() { // arrange var attributeProvider = new Mock<ICustomAttributeProvider>(); var attribute = new MetadataConventionsAttribute { ResourceType = typeof(TestResources) }; attributeProvider.Setup(a => a.GetCustomAttributes(typeof(MetadataConventionsAttribute), true)).Returns(new[] { attribute }); // act var retrievedAttribute = attributeProvider.Object.First<DisplayAttribute>(); // assert Assert.Null(retrievedAttribute); }
public void First_WithAttributeReturnedByAttributeProvider_ReturnsAttribute() { // arrange var attributeProvider = new Mock<ICustomAttributeProvider>(); var attribute = new MetadataConventionsAttribute { ResourceType = typeof(TestResources) }; attributeProvider.Setup(a => a.GetCustomAttributes(typeof(MetadataConventionsAttribute), true)).Returns(new[] { attribute }); // act var retrievedAttribute = attributeProvider.Object.First<MetadataConventionsAttribute>(); // assert Assert.Equal(typeof(TestResources), retrievedAttribute.ResourceType); }