Exemplo n.º 1
0
        public void CopyConstructor_DoesDeepCopyOfOtherModels()
        {
            // Arrange
            var action = new ActionModel(typeof(TestController).GetMethod("Edit"),
                                         new List <object>());

            var parameter = new ParameterModel(action.ActionMethod.GetParameters()[0],
                                               new List <object>());

            parameter.Action = action;
            action.Parameters.Add(parameter);

            var route = new AttributeRouteModel(new HttpGetAttribute("api/Products"));

            action.AttributeRouteModel = route;

            var apiExplorer = action.ApiExplorer;

            apiExplorer.IsVisible = false;
            apiExplorer.GroupName = "group1";

            // Act
            var action2 = new ActionModel(action);

            // Assert
            Assert.NotSame(action, action2.Parameters[0]);
            Assert.NotSame(apiExplorer, action2.ApiExplorer);
            Assert.NotSame(route, action2.AttributeRouteModel);
        }
Exemplo n.º 2
0
        public void CopyConstructor_DoesDeepCopyOfOtherModels()
        {
            // Arrange
            var action = new ActionModel(typeof(TestController).GetMethod("Edit"),
                                         new List<object>());

            var parameter = new ParameterModel(action.ActionMethod.GetParameters()[0],
                                               new List<object>());
            parameter.Action = action;
            action.Parameters.Add(parameter);

            var route = new AttributeRouteModel(new HttpGetAttribute("api/Products"));
            action.AttributeRouteModel = route;

            var apiExplorer = action.ApiExplorer;
            apiExplorer.IsVisible = false;
            apiExplorer.GroupName = "group1";

            // Act
            var action2 = new ActionModel(action);

            // Assert
            Assert.NotSame(action, action2.Parameters[0]);
            Assert.NotSame(apiExplorer, action2.ApiExplorer);
            Assert.NotSame(route, action2.AttributeRouteModel);
        }
Exemplo n.º 3
0
 public ParameterModel([NotNull] ParameterModel other)
 {
     Action        = other.Action;
     Attributes    = new List <object>(other.Attributes);
     BindingInfo   = other.BindingInfo;
     ParameterInfo = other.ParameterInfo;
     ParameterName = other.ParameterName;
 }
Exemplo n.º 4
0
 public ParameterModel([NotNull] ParameterModel other)
 {
     Action        = other.Action;
     Attributes    = new List <object>(other.Attributes);
     BindingInfo   = other.BindingInfo == null ? null : new BindingInfo(other.BindingInfo);
     ParameterInfo = other.ParameterInfo;
     ParameterName = other.ParameterName;
     Properties    = new Dictionary <object, object>(other.Properties);
 }
Exemplo n.º 5
0
 public ParameterModel([NotNull] ParameterModel other)
 {
     Action         = other.Action;
     Attributes     = new List <object>(other.Attributes);
     BinderMetadata = other.BinderMetadata;
     IsOptional     = other.IsOptional;
     ParameterInfo  = other.ParameterInfo;
     ParameterName  = other.ParameterName;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a <see cref="ParameterModel"/> for the given <see cref="ParameterInfo"/>.
        /// </summary>
        /// <param name="parameterInfo">The <see cref="ParameterInfo"/>.</param>
        /// <returns>A <see cref="ParameterModel"/> for the given <see cref="ParameterInfo"/>.</returns>
        protected virtual ParameterModel CreateParameterModel([NotNull] ParameterInfo parameterInfo)
        {
            // CoreCLR returns IEnumerable<Attribute> from GetCustomAttributes - the OfType<object>
            // is needed to so that the result of ToArray() is object
            var attributes     = parameterInfo.GetCustomAttributes(inherit: true).OfType <object>().ToArray();
            var parameterModel = new ParameterModel(parameterInfo, attributes);

            parameterModel.BinderMetadata = attributes.OfType <IBinderMetadata>().FirstOrDefault();

            parameterModel.ParameterName = parameterInfo.Name;

            return(parameterModel);
        }
Exemplo n.º 7
0
        public void CopyConstructor_CopiesAllProperties()
        {
            // Arrange
            var parameter = new ParameterModel(typeof(TestController).GetMethod("Edit").GetParameters()[0],
                                               new List <object>()
            {
                new FromBodyAttribute()
            });

            parameter.Action      = new ActionModel(typeof(TestController).GetMethod("Edit"), new List <object>());
            parameter.BindingInfo = new BindingInfo()
            {
                BindingSource = BindingSource.Body
            };

            parameter.ParameterName = "id";

            // Act
            var parameter2 = new ParameterModel(parameter);

            // Assert
            foreach (var property in typeof(ParameterModel).GetProperties())
            {
                var value1 = property.GetValue(parameter);
                var value2 = property.GetValue(parameter2);

                if (typeof(IEnumerable <object>).IsAssignableFrom(property.PropertyType))
                {
                    Assert.Equal <object>((IEnumerable <object>)value1, (IEnumerable <object>)value2);

                    // Ensure non-default value
                    Assert.NotEmpty((IEnumerable <object>)value1);
                }
                else if (property.PropertyType.IsValueType ||
                         Nullable.GetUnderlyingType(property.PropertyType) != null)
                {
                    Assert.Equal(value1, value2);

                    // Ensure non-default value
                    Assert.NotEqual(value1, Activator.CreateInstance(property.PropertyType));
                }
                else
                {
                    Assert.Same(value1, value2);

                    // Ensure non-default value
                    Assert.NotNull(value1);
                }
            }
        }
Exemplo n.º 8
0
        public ParameterModel(ParameterModel other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            Action        = other.Action;
            Attributes    = new List <object>(other.Attributes);
            BindingInfo   = other.BindingInfo == null ? null : new BindingInfo(other.BindingInfo);
            ParameterInfo = other.ParameterInfo;
            ParameterName = other.ParameterName;
            Properties    = new Dictionary <object, object>(other.Properties);
        }
Exemplo n.º 9
0
        public ParameterModel(ParameterModel other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            Action = other.Action;
            Attributes = new List<object>(other.Attributes);
            BindingInfo = other.BindingInfo == null ? null : new BindingInfo(other.BindingInfo);
            ParameterInfo = other.ParameterInfo;
            ParameterName = other.ParameterName;
            Properties = new Dictionary<object, object>(other.Properties);
        }
Exemplo n.º 10
0
        public void CopyConstructor_CopiesAllProperties()
        {
            // Arrange
            var parameter = new ParameterModel(typeof(TestController).GetMethod("Edit").GetParameters()[0],
                                               new List<object>() { new FromBodyAttribute() });

            parameter.Action = new ActionModel(typeof(TestController).GetMethod("Edit"), new List<object>());
            parameter.BindingInfo = new BindingInfo()
            {
                BindingSource = BindingSource.Body
            };

            parameter.ParameterName = "id";

            // Act
            var parameter2 = new ParameterModel(parameter);

            // Assert
            foreach (var property in typeof(ParameterModel).GetProperties())
            {
                var value1 = property.GetValue(parameter);
                var value2 = property.GetValue(parameter2);

                if (typeof(IEnumerable<object>).IsAssignableFrom(property.PropertyType))
                {
                    Assert.Equal<object>((IEnumerable<object>)value1, (IEnumerable<object>)value2);

                    // Ensure non-default value
                    Assert.NotEmpty((IEnumerable<object>)value1);
                }
                else if (property.PropertyType.IsValueType ||
                    Nullable.GetUnderlyingType(property.PropertyType) != null)
                {
                    Assert.Equal(value1, value2);

                    // Ensure non-default value
                    Assert.NotEqual(value1, Activator.CreateInstance(property.PropertyType));
                }
                else
                {
                    Assert.Same(value1, value2);

                    // Ensure non-default value
                    Assert.NotNull(value1);
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Creates a <see cref="ParameterModel"/> for the given <see cref="ParameterInfo"/>.
        /// </summary>
        /// <param name="parameterInfo">The <see cref="ParameterInfo"/>.</param>
        /// <returns>A <see cref="ParameterModel"/> for the given <see cref="ParameterInfo"/>.</returns>
        protected virtual ParameterModel CreateParameterModel(ParameterInfo parameterInfo)
        {
            if (parameterInfo == null)
            {
                throw new ArgumentNullException(nameof(parameterInfo));
            }

            // CoreCLR returns IEnumerable<Attribute> from GetCustomAttributes - the OfType<object>
            // is needed to so that the result of ToArray() is object
            var attributes     = parameterInfo.GetCustomAttributes(inherit: true).OfType <object>().ToArray();
            var parameterModel = new ParameterModel(parameterInfo, attributes);

            var bindingInfo = BindingInfo.GetBindingInfo(attributes);

            parameterModel.BindingInfo = bindingInfo;

            parameterModel.ParameterName = parameterInfo.Name;

            return(parameterModel);
        }
Exemplo n.º 12
0
 public void Apply(ParameterModel model)
 {
     model.BindingInfo = model.BindingInfo ?? new BindingInfo();
     model.BindingInfo.BindingSource = BindingSource.Custom;
     model.BindingInfo.BinderModelName = "CoolMetadata";
 }
        public void ApplyConventions_RunsInOrderOfDecreasingScope()
        {
            // Arrange
            var sequence = 0;

            var applicationConvention = new Mock<IApplicationModelConvention>();
            applicationConvention
                .Setup(c => c.Apply(It.IsAny<ApplicationModel>()))
                .Callback(() => { Assert.Equal(0, sequence++); });

            var controllerConvention = new Mock<IControllerModelConvention>();
            controllerConvention
                .Setup(c => c.Apply(It.IsAny<ControllerModel>()))
                .Callback(() => { Assert.Equal(1, sequence++); });

            var actionConvention = new Mock<IActionModelConvention>();
            actionConvention
                .Setup(c => c.Apply(It.IsAny<ActionModel>()))
                .Callback(() => { Assert.Equal(2, sequence++); });

            var parameterConvention = new Mock<IParameterModelConvention>();
            parameterConvention
                .Setup(c => c.Apply(It.IsAny<ParameterModel>()))
                .Callback(() => { Assert.Equal(3, sequence++); });

            var options = new TestOptionsManager<MvcOptions>();
            options.Value.Conventions.Add(applicationConvention.Object);

            var applicationModel = new ApplicationModel();

            var controller = new ControllerModel(typeof(ConventionsController).GetTypeInfo(),
                                                 new List<object>() { controllerConvention.Object });
            controller.Application = applicationModel;
            applicationModel.Controllers.Add(controller);

            var methodInfo = typeof(ConventionsController).GetMethod("Create");
            var actionModel = new ActionModel(methodInfo, new List<object>() { actionConvention.Object });
            actionModel.Controller = controller;
            controller.Actions.Add(actionModel);

            var parameterInfo = actionModel.ActionMethod.GetParameters().Single();
            var parameterModel = new ParameterModel(parameterInfo,
                                           new List<object>() { parameterConvention.Object });
            parameterModel.Action = actionModel;
            actionModel.Parameters.Add(parameterModel);

            // Act
            ApplicationModelConventions.ApplyConventions(applicationModel, options.Value.Conventions);

            // Assert
            Assert.Equal(4, sequence);
        }
 public void Apply(ParameterModel parameter)
 {
     parameter.BindingInfo = parameter.BindingInfo ?? new BindingInfo();
     parameter.BindingInfo.BinderModelName = "ChangedParameter";
 }
Exemplo n.º 15
0
        public void CopyConstructor_CopiesAllProperties()
        {
            // Arrange
            var parameter = new ParameterModel(typeof(TestController).GetMethod("Edit").GetParameters()[0],
                                               new List<object>() { new FromBodyAttribute() });

            parameter.Action = new ActionModel(typeof(TestController).GetMethod("Edit"), new List<object>());
            parameter.BindingInfo = new BindingInfo()
            {
                BindingSource = BindingSource.Body
            };

            parameter.ParameterName = "id";
            parameter.Properties.Add(new KeyValuePair<object, object>("test key", "test value"));

            // Act
            var parameter2 = new ParameterModel(parameter);

            // Assert
            foreach (var property in typeof(ParameterModel).GetProperties())
            {
                if (property.Name.Equals("BindingInfo"))
                {
                    // This test excludes other mutable objects on purpose because we deep copy them.
                    continue;
                }

                var value1 = property.GetValue(parameter);
                var value2 = property.GetValue(parameter2);

                if (typeof(IEnumerable<object>).IsAssignableFrom(property.PropertyType))
                {
                    Assert.Equal<object>((IEnumerable<object>)value1, (IEnumerable<object>)value2);

                    // Ensure non-default value
                    Assert.NotEmpty((IEnumerable<object>)value1);
                }
                else if (typeof(IDictionary<object, object>).IsAssignableFrom(property.PropertyType))
                {
                    Assert.Equal(value1, value2);

                    // Ensure non-default value
                    Assert.NotEmpty((IDictionary<object, object>)value1);
                }
                else if (property.PropertyType.IsValueType ||
                    Nullable.GetUnderlyingType(property.PropertyType) != null)
                {
                    Assert.Equal(value1, value2);

                    // Ensure non-default value
                    Assert.NotEqual(value1, Activator.CreateInstance(property.PropertyType));
                }
                else
                {
                    Assert.Same(value1, value2);

                    // Ensure non-default value
                    Assert.NotNull(value1);
                }
            }
        }
Exemplo n.º 16
0
        public void CopyConstructor_CopiesAllProperties()
        {
            // Arrange
            var parameter = new ParameterModel(typeof(TestController).GetMethod("Edit").GetParameters()[0],
                                               new List <object>()
            {
                new FromBodyAttribute()
            });

            parameter.Action      = new ActionModel(typeof(TestController).GetMethod("Edit"), new List <object>());
            parameter.BindingInfo = new BindingInfo()
            {
                BindingSource = BindingSource.Body
            };

            parameter.ParameterName = "id";
            parameter.Properties.Add(new KeyValuePair <object, object>("test key", "test value"));

            // Act
            var parameter2 = new ParameterModel(parameter);

            // Assert
            foreach (var property in typeof(ParameterModel).GetProperties())
            {
                if (property.Name.Equals("BindingInfo"))
                {
                    // This test excludes other mutable objects on purpose because we deep copy them.
                    continue;
                }

                var value1 = property.GetValue(parameter);
                var value2 = property.GetValue(parameter2);

                if (typeof(IEnumerable <object>).IsAssignableFrom(property.PropertyType))
                {
                    Assert.Equal <object>((IEnumerable <object>)value1, (IEnumerable <object>)value2);

                    // Ensure non-default value
                    Assert.NotEmpty((IEnumerable <object>)value1);
                }
                else if (typeof(IDictionary <object, object>).IsAssignableFrom(property.PropertyType))
                {
                    Assert.Equal(value1, value2);

                    // Ensure non-default value
                    Assert.NotEmpty((IDictionary <object, object>)value1);
                }
                else if (property.PropertyType.GetTypeInfo().IsValueType ||
                         Nullable.GetUnderlyingType(property.PropertyType) != null)
                {
                    Assert.Equal(value1, value2);

                    // Ensure non-default value
                    Assert.NotEqual(value1, Activator.CreateInstance(property.PropertyType));
                }
                else
                {
                    Assert.Same(value1, value2);

                    // Ensure non-default value
                    Assert.NotNull(value1);
                }
            }
        }