コード例 #1
0
    public void IsBindingAllowed_IsFalse_WhenTypeMatches(bool initialValue)
    {
        // Arrange
        var provider = new ExcludeBindingMetadataProvider(typeof(int));

        var key = ModelMetadataIdentity.ForProperty(
            typeof(Person).GetProperty(nameof(Person.Age)),
            typeof(int),
            typeof(Person));

        var context = new BindingMetadataProviderContext(key, new ModelAttributes(new object[0], new object[0], null));

        context.BindingMetadata.IsBindingAllowed = initialValue;

        // Act
        provider.CreateBindingMetadata(context);

        // Assert
        Assert.False(context.BindingMetadata.IsBindingAllowed);
    }
コード例 #2
0
    public void CreateBindingDetails_OverrideBehaviorOnClass_OverrideWithNever()
    {
        // Arrange
        var propertyAttributes = new object[]
        {
            new BindNeverAttribute(),
        };

        var context = new BindingMetadataProviderContext(
            ModelMetadataIdentity.ForProperty(typeof(BindRequiredOnClass).GetProperty(nameof(BindRequiredOnClass.Property)), typeof(int), typeof(BindRequiredOnClass)),
            new ModelAttributes(new object[0], propertyAttributes, null));

        var provider = new DefaultBindingMetadataProvider();

        // Act
        provider.CreateBindingMetadata(context);

        // Assert
        Assert.False(context.BindingMetadata.IsBindingAllowed);
        Assert.False(context.BindingMetadata.IsBindingRequired);
    }
コード例 #3
0
    public void CreateBindingDetails_FindsBindRequired_OnParameter()
    {
        // Arrange
        var parameterAttributes = new object[]
        {
            new BindRequiredAttribute(),
        };

        var context = new BindingMetadataProviderContext(
            ModelMetadataIdentity.ForParameter(ParameterInfos.SampleParameterInfo),
            new ModelAttributes(Array.Empty <object>(), null, parameterAttributes));

        var provider = new DefaultBindingMetadataProvider();

        // Act
        provider.CreateBindingMetadata(context);

        // Assert
        Assert.True(context.BindingMetadata.IsBindingAllowed);
        Assert.True(context.BindingMetadata.IsBindingRequired);
    }
コード例 #4
0
    public void CreateBindingDetails_FindsBindRequired_OnProperty()
    {
        // Arrange
        var propertyAttributes = new object[]
        {
            new BindRequiredAttribute(),
        };

        var context = new BindingMetadataProviderContext(
            ModelMetadataIdentity.ForProperty(typeof(string).GetProperty(nameof(string.Length)), typeof(int), typeof(string)),
            new ModelAttributes(new object[0], propertyAttributes, null));

        var provider = new DefaultBindingMetadataProvider();

        // Act
        provider.CreateBindingMetadata(context);

        // Assert
        Assert.True(context.BindingMetadata.IsBindingAllowed);
        Assert.True(context.BindingMetadata.IsBindingRequired);
    }
コード例 #5
0
        public void CreateBindingDetails_OverrideBehaviorOnClass_OverrideWithNever()
        {
            // Arrange
            var propertyAttributes = new object[]
            {
                new BindNeverAttribute(),
            };

            var context = new BindingMetadataProviderContext(
                ModelMetadataIdentity.ForProperty(typeof(string), "Property", typeof(BindRequiredOnClass)),
                new ModelAttributes(propertyAttributes, typeAttributes: new object[0]));

            var provider = new DefaultBindingMetadataProvider(CreateMessageProvider());

            // Act
            provider.CreateBindingMetadata(context);

            // Assert
            Assert.False(context.BindingMetadata.IsBindingAllowed);
            Assert.False(context.BindingMetadata.IsBindingRequired);
        }
コード例 #6
0
        public void CreateBindingDetails_OverrideInheritedBehaviorOnClass_OverrideWithRequired()
        {
            // Arrange
            var propertyAttributes = new object[]
            {
                new BindRequiredAttribute()
            };

            var context = new BindingMetadataProviderContext(
                ModelMetadataIdentity.ForProperty(typeof(string), "Property", typeof(InheritedBindNeverOnClass)),
                new ModelAttributes(new object[0], propertyAttributes, null));

            var provider = new DefaultBindingMetadataProvider();

            // Act
            provider.CreateBindingMetadata(context);

            // Assert
            Assert.True(context.BindingMetadata.IsBindingAllowed);
            Assert.True(context.BindingMetadata.IsBindingRequired);
        }
コード例 #7
0
        public void CreateBindingDetails_FindsBindingBehaviorRequired_OnProperty()
        {
            // Arrange
            var propertyAttributes = new object[]
            {
                new BindingBehaviorAttribute(BindingBehavior.Required),
            };

            var context = new BindingMetadataProviderContext(
                ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string)),
                new ModelAttributes(propertyAttributes, typeAttributes: new object[0]));

            var provider = new DefaultBindingMetadataProvider(CreateMessageProvider());

            // Act
            provider.CreateBindingMetadata(context);

            // Assert
            Assert.True(context.BindingMetadata.IsBindingAllowed);
            Assert.True(context.BindingMetadata.IsBindingRequired);
        }
コード例 #8
0
        private static BindingBehaviorAttribute FindBindingBehavior(BindingMetadataProviderContext context)
        {
            switch (context.Key.MetadataKind)
            {
            case ModelMetadataKind.Property:
                // BindingBehavior can fall back to attributes on the Container Type, but we should ignore
                // attributes on the Property Type.
                var matchingAttributes = context.PropertyAttributes.OfType <BindingBehaviorAttribute>();
                return(matchingAttributes.FirstOrDefault()
                       ?? context.Key.ContainerType.GetTypeInfo()
                       .GetCustomAttributes(typeof(BindingBehaviorAttribute), inherit: true)
                       .OfType <BindingBehaviorAttribute>()
                       .FirstOrDefault());

            case ModelMetadataKind.Parameter:
                return(context.ParameterAttributes.OfType <BindingBehaviorAttribute>().FirstOrDefault());

            default:
                return(null);
            }
        }
コード例 #9
0
    public void CreateBindingDetails_FindsBindingSource()
    {
        // Arrange
        var attributes = new object[]
        {
            new BindingSourceModelBinderAttribute(BindingSource.Body),
            new BindingSourceModelBinderAttribute(BindingSource.Query),
        };

        var context = new BindingMetadataProviderContext(
            ModelMetadataIdentity.ForType(typeof(string)),
            new ModelAttributes(attributes, null, null));

        var provider = new DefaultBindingMetadataProvider();

        // Act
        provider.CreateBindingMetadata(context);

        // Assert
        Assert.Equal(BindingSource.Body, context.BindingMetadata.BindingSource);
    }
コード例 #10
0
    /// <inheritdoc />
    public void CreateBindingMetadata(BindingMetadataProviderContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        // Types cannot be required; only properties can
        if (context.Key.MetadataKind != ModelMetadataKind.Property)
        {
            return;
        }

        if (context.BindingMetadata.IsBindingRequired)
        {
            // This value is already required, no need to look at attributes.
            return;
        }

        var dataMemberAttribute = context
                                  .PropertyAttributes !
                                  .OfType <DataMemberAttribute>()
                                  .FirstOrDefault();

        if (dataMemberAttribute == null || !dataMemberAttribute.IsRequired)
        {
            return;
        }

        // isDataContract == true iff the container type has at least one DataContractAttribute
        var containerType  = context.Key.ContainerType;
        var isDataContract = containerType !.IsDefined(typeof(DataContractAttribute));

        if (isDataContract)
        {
            // We don't need to add a validator, just to set IsRequired = true. The validation
            // system will do the right thing.
            context.BindingMetadata.IsBindingRequired = true;
        }
    }
コード例 #11
0
        public void CreateBindingDetails_UsesFirstAttribute()
        {
            // Arrange
            var propertyAttributes = new object[]
            {
                new BindingBehaviorAttribute(BindingBehavior.Required),
                new BindNeverAttribute(),
            };

            var context = new BindingMetadataProviderContext(
                ModelMetadataIdentity.ForProperty(typeof(int), "Length", typeof(string)),
                new ModelAttributes(new object[0], propertyAttributes, null));

            var provider = new DefaultBindingMetadataProvider();

            // Act
            provider.CreateBindingMetadata(context);

            // Assert
            Assert.True(context.BindingMetadata.IsBindingAllowed);
            Assert.True(context.BindingMetadata.IsBindingRequired);
        }
        public void IsBindingRequired_LeftAlone_ForNonPropertyMetadata(bool initialValue)
        {
            // Arrange
            var provider = new DataMemberRequiredBindingMetadataProvider();

            var attributes = new object[]
            {
                new DataMemberAttribute()
                {
                    IsRequired = true,
                }
            };

            var key     = ModelMetadataIdentity.ForType(typeof(ClassWithDataMemberIsRequiredTrue));
            var context = new BindingMetadataProviderContext(key, new ModelAttributes(attributes, new object[0]));

            context.BindingMetadata.IsBindingRequired = initialValue;

            // Act
            provider.CreateBindingMetadata(context);

            // Assert
            Assert.Equal(initialValue, context.BindingMetadata.IsBindingRequired);
        }
コード例 #13
0
        public void CreateBindingMetadata(BindingMetadataProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // BinderModelName
            foreach (var binderModelNameAttribute in context.Attributes.OfType <IModelNameProvider>())
            {
                if (binderModelNameAttribute?.Name != null)
                {
                    context.BindingMetadata.BinderModelName = binderModelNameAttribute.Name;
                    break;
                }
            }

            // BinderType
            foreach (var binderTypeAttribute in context.Attributes.OfType <IBinderTypeProviderMetadata>())
            {
                if (binderTypeAttribute.BinderType != null)
                {
                    context.BindingMetadata.BinderType = binderTypeAttribute.BinderType;
                    break;
                }
            }

            // BindingSource
            foreach (var bindingSourceAttribute in context.Attributes.OfType <IBindingSourceMetadata>())
            {
                if (bindingSourceAttribute.BindingSource != null)
                {
                    context.BindingMetadata.BindingSource = bindingSourceAttribute.BindingSource;
                    break;
                }
            }

            // PropertyFilterProvider
            var propertyFilterProviders = context.Attributes.OfType <IPropertyFilterProvider>().ToArray();

            if (propertyFilterProviders.Length == 0)
            {
                context.BindingMetadata.PropertyFilterProvider = null;
            }
            else if (propertyFilterProviders.Length == 1)
            {
                context.BindingMetadata.PropertyFilterProvider = propertyFilterProviders[0];
            }
            else
            {
                var composite = new CompositePropertyFilterProvider(propertyFilterProviders);
                context.BindingMetadata.PropertyFilterProvider = composite;
            }

            if (context.Key.MetadataKind == ModelMetadataKind.Property)
            {
                // BindingBehavior can fall back to attributes on the Container Type, but we should ignore
                // attributes on the Property Type.
                var bindingBehavior = context.PropertyAttributes.OfType <BindingBehaviorAttribute>().FirstOrDefault();
                if (bindingBehavior == null)
                {
                    bindingBehavior =
                        context.Key.ContainerType.GetTypeInfo()
                        .GetCustomAttributes(typeof(BindingBehaviorAttribute), inherit: true)
                        .OfType <BindingBehaviorAttribute>()
                        .FirstOrDefault();
                }

                if (bindingBehavior != null)
                {
                    context.BindingMetadata.IsBindingAllowed  = bindingBehavior.Behavior != BindingBehavior.Never;
                    context.BindingMetadata.IsBindingRequired = bindingBehavior.Behavior == BindingBehavior.Required;
                }
            }
        }
コード例 #14
0
    public void CreateBindingMetadata(BindingMetadataProviderContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        // BinderModelName
        foreach (var binderModelNameAttribute in context.Attributes.OfType <IModelNameProvider>())
        {
            if (binderModelNameAttribute.Name != null)
            {
                context.BindingMetadata.BinderModelName = binderModelNameAttribute.Name;
                break;
            }
        }

        // BinderType
        foreach (var binderTypeAttribute in context.Attributes.OfType <IBinderTypeProviderMetadata>())
        {
            if (binderTypeAttribute.BinderType != null)
            {
                context.BindingMetadata.BinderType = binderTypeAttribute.BinderType;
                break;
            }
        }

        // BindingSource
        foreach (var bindingSourceAttribute in context.Attributes.OfType <IBindingSourceMetadata>())
        {
            if (bindingSourceAttribute.BindingSource != null)
            {
                context.BindingMetadata.BindingSource = bindingSourceAttribute.BindingSource;
                break;
            }
        }

        // PropertyFilterProvider
        var propertyFilterProviders = context.Attributes.OfType <IPropertyFilterProvider>().ToArray();

        if (propertyFilterProviders.Length == 0)
        {
            context.BindingMetadata.PropertyFilterProvider = null;
        }
        else if (propertyFilterProviders.Length == 1)
        {
            context.BindingMetadata.PropertyFilterProvider = propertyFilterProviders[0];
        }
        else
        {
            var composite = new CompositePropertyFilterProvider(propertyFilterProviders);
            context.BindingMetadata.PropertyFilterProvider = composite;
        }

        var bindingBehavior = FindBindingBehavior(context);

        if (bindingBehavior != null)
        {
            context.BindingMetadata.IsBindingAllowed  = bindingBehavior.Behavior != BindingBehavior.Never;
            context.BindingMetadata.IsBindingRequired = bindingBehavior.Behavior == BindingBehavior.Required;
        }

        if (GetBoundConstructor(context.Key.ModelType) is ConstructorInfo constructorInfo)
        {
            context.BindingMetadata.BoundConstructor = constructorInfo;
        }
    }
コード例 #15
0
 public void CreateBindingMetadata(BindingMetadataProviderContext context)
 {
 }
コード例 #16
0
 public void CreateBindingMetadata(BindingMetadataProviderContext context)
 {
     throw new System.NotImplementedException("Not used in tests");
 }
コード例 #17
0
 public void GetBindingMetadata(BindingMetadataProviderContext context)
 {
     // Don't bother with ModelBindingMessageProvider copy constructor. No other provider can change the
     // delegates.
     context.BindingMetadata.ModelBindingMessageProvider = _messageProvider;
 }