Beispiel #1
0
        public void SetCollectionProperty_NonSettableProperty_NonNullValue_WithAddMethod(string propertyName)
        {
            object       value       = new SampleClassWithNonSettableCollectionProperties();
            IEdmProperty edmProperty = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32);

            DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            }, propertyName: edmProperty.Name);

            Assert.Equal(
                new[] { 1, 2, 3 },
                value.GetType().GetProperty(propertyName).GetValue(value, index: null) as IEnumerable <int>);
        }
Beispiel #2
0
        public void SetCollectionProperty_CanConvertNonStandardEdmTypes()
        {
            SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties();
            IEdmProperty edmProperty = GetMockEdmProperty("UnsignedArray", EdmPrimitiveTypeKind.Int32);

            DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            }, propertyName: edmProperty.Name);

            Assert.Equal(
                new uint[] { 1, 2, 3 },
                value.UnsignedArray);
        }
Beispiel #3
0
        public void SetCollectionProperty_NonSettableProperty_NonNullValue_NoAdd_Throws(string propertyName)
        {
            object       value        = new SampleClassWithNonSettableCollectionProperties();
            Type         propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType;
            IEdmProperty edmProperty  = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32);

            ExceptionAssert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            }, propertyName: edmProperty.Name),
                String.Format("The type '{0}' of the property '{1}' on type 'Microsoft.AspNet.OData.Test.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' does not have an Add method. " +
                              "Consider using a collection type that does have an Add method - for example IList<T> or ICollection<T>.", propertyType.FullName, propertyName));
        }
Beispiel #4
0
        public void SetCollectionProperty_NonSettableProperty_ArrayValue_FixedSize_Throws(string propertyName)
        {
            object       value        = new SampleClassWithNonSettableCollectionProperties();
            Type         propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType;
            IEdmProperty edmProperty  = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32);

            ExceptionAssert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            }, propertyName: edmProperty.Name),
                String.Format("The value of the property '{0}' on type 'Microsoft.AspNet.OData.Test.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' is an array. " +
                              "Consider adding a setter for the property.", propertyName));
        }
        private object BuildResourceInstance()
        {
            if (EdmObject == null)
            {
                return(null);
            }

            TypedEdmStructuredObject edmStructruredObject = EdmObject as TypedEdmStructuredObject;

            if (edmStructruredObject != null)
            {
                return(edmStructruredObject.Instance);
            }

            SelectExpandWrapper selectExpandWrapper = EdmObject as SelectExpandWrapper;

            if (selectExpandWrapper != null && selectExpandWrapper.UntypedInstance != null)
            {
                return(selectExpandWrapper.UntypedInstance);
            }

            Type clrType = EdmLibHelpers.GetClrType(StructuredType, EdmModel);

            if (clrType == null)
            {
                throw new InvalidOperationException(Error.Format(SRResources.MappingDoesNotContainResourceType, StructuredType.FullTypeName()));
            }

            object resource = Activator.CreateInstance(clrType);

            foreach (IEdmStructuralProperty property in StructuredType.StructuralProperties())
            {
                object value;
                if (EdmObject.TryGetPropertyValue(property.Name, out value) && value != null)
                {
                    string propertyName = EdmLibHelpers.GetClrPropertyName(property, EdmModel);

                    if (TypeHelper.IsCollection(value.GetType()))
                    {
                        DeserializationHelpers.SetCollectionProperty(resource, property, value, propertyName);
                    }
                    else
                    {
                        DeserializationHelpers.SetProperty(resource, propertyName, value);
                    }
                }
            }

            return(resource);
        }
        public void SetCollectionProperty_NonSettableProperty_NullValue_Throws(string propertyName)
        {
            object value = new SampleClassWithNonSettableCollectionProperties();

            value.GetType().GetProperty(propertyName).SetValue(value, null, null);
            IEdmProperty edmProperty = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32);

            ExceptionAssert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            }, propertyName: edmProperty.Name),
                String.Format("The property '{0}' on type 'Microsoft.AspNet.OData.Test.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' returned a null value. " +
                              "The input stream contains collection items which cannot be added if the instance is null.", propertyName));
        }
        public void SetCollectionProperty_OnNonCollection_ThrowsSerialization(string propertyName)
        {
            object       value        = new SampleClassWithDifferentCollectionProperties();
            Type         propertyType = typeof(SampleClassWithDifferentCollectionProperties).GetProperty(propertyName).PropertyType;
            IEdmProperty edmProperty  = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32);

            ExceptionAssert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            }, propertyName: edmProperty.Name),
                Error.Format(
                    "The type '{0}' of the property '{1}' on type 'Microsoft.AspNet.OData.Test.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithDifferentCollectionProperties' must be a collection.",
                    propertyType.FullName,
                    propertyName));
        }
        public override void SetValue(TEntityType entity, object value)
        {
            if (entity == null)
            {
                throw Error.ArgumentNull("entity");
            }

            if (_isCollection)
            {
                DeserializationHelpers.SetCollectionProperty(entity, _property.Name, edmPropertyType: null,
                                                             value: value, clearCollection: true);
            }
            else
            {
                _setter(entity, value);
            }
        }
        public void SetCollectionProperty_CanConvertEnumCollection()
        {
            SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties();
            IEdmProperty edmProperty = GetMockEdmProperty("FlagsEnum", EdmPrimitiveTypeKind.String);

            DeserializationHelpers.SetCollectionProperty(
                value,
                edmProperty,
                value: new List <FlagsEnum> {
                FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two | (FlagsEnum)123
            },
                propertyName: edmProperty.Name);

            Assert.Equal(
                new FlagsEnum[] { FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two | (FlagsEnum)123 },
                value.FlagsEnum);
        }
        public override void SetValue(TStructuralType instance, object value)
        {
            if (instance == null)
            {
                throw Error.ArgumentNull("instance");
            }

            if (_isCollection)
            {
                DeserializationHelpers.SetCollectionProperty(instance, _property.Name, edmPropertyType: null,
                                                             value: value, clearCollection: true);
            }
            else
            {
                _setter(instance, value);
            }
        }
Beispiel #11
0
        private object BuildEntityInstance()
        {
            if (EdmObject == null)
            {
                return(null);
            }

            TypedEdmEntityObject edmEntityObject = EdmObject as TypedEdmEntityObject;

            if (edmEntityObject != null)
            {
                return(edmEntityObject.Instance);
            }

            var  assemblyNames = Request.HttpContext.RequestServices.GetService <AssembliesResolver>();
            Type clrType       = EdmLibHelpers.GetClrType(EntityType, EdmModel, assemblyNames);

            if (clrType == null)
            {
                throw new InvalidOperationException(Error.Format(SRResources.MappingDoesNotContainEntityType, EntityType.FullName()));
            }

            object resource = Activator.CreateInstance(clrType);

            foreach (IEdmStructuralProperty property in EntityType.StructuralProperties())
            {
                object value;
                if (EdmObject.TryGetPropertyValue(property.Name, out value) && value != null)
                {
                    if (value.GetType().IsCollection())
                    {
                        DeserializationHelpers.SetCollectionProperty(resource, property, value, property.Name, assemblyNames);
                    }
                    else
                    {
                        DeserializationHelpers.SetProperty(resource, property.Name, value);
                    }
                }
            }

            return(resource);
        }
Beispiel #12
0
        public void SetCollectionProperty_CanConvertDataTime_ByDefault()
        {
            // Arrange
            SampleClassWithDifferentCollectionProperties source = new SampleClassWithDifferentCollectionProperties();
            IEdmProperty           edmProperty = GetMockEdmProperty("DateTimeList", EdmPrimitiveTypeKind.DateTimeOffset);
            DateTime               dt          = new DateTime(2014, 11, 15, 1, 2, 3);
            IList <DateTimeOffset> dtos        = new List <DateTimeOffset>
            {
                new DateTimeOffset(dt, TimeSpan.Zero),
                new DateTimeOffset(dt, new TimeSpan(+7, 0, 0)),
                new DateTimeOffset(dt, new TimeSpan(-8, 0, 0))
            };

            IEnumerable <DateTime> expects = dtos.Select(e => e.LocalDateTime);

            // Act
            DeserializationHelpers.SetCollectionProperty(source, edmProperty, dtos, edmProperty.Name, timeZoneInfo: null);

            // Assert
            Assert.Equal(expects, source.DateTimeList);
        }
        public void SetCollectionProperty_ClearsCollection_IfClearCollectionIsTrue(string propertyName)
        {
            // Arrange
            IEnumerable <int> value    = new int[] { 1, 2, 3 };
            object            resource = new SampleClassWithNonSettableCollectionProperties
            {
                ICollection = { 42 },
                IList       = { 42 },
                Collection  = { 42 },
                List        = { 42 },
                CustomCollectionWithNoEmptyCtor = { 42 },
                CustomCollection = { 42 }
            };

            // Act
            DeserializationHelpers.SetCollectionProperty(resource, propertyName, null, value, clearCollection: true);

            // Assert
            Assert.Equal(
                value,
                resource.GetType().GetProperty(propertyName).GetValue(resource, index: null) as IEnumerable <int>);
        }
Beispiel #14
0
        public void SetCollectionProperty_CanConvertDataTime_ByTimeZoneInfo()
        {
            // Arrange
            SampleClassWithDifferentCollectionProperties source = new SampleClassWithDifferentCollectionProperties();
            IEdmProperty edmProperty = GetMockEdmProperty("DateTimeList", EdmPrimitiveTypeKind.DateTimeOffset);

            TimeZoneInfo           tzi  = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            DateTime               dt   = new DateTime(2014, 11, 15, 1, 2, 3);
            IList <DateTimeOffset> dtos = new List <DateTimeOffset>
            {
                new DateTimeOffset(dt, TimeSpan.Zero),
                new DateTimeOffset(dt, new TimeSpan(+7, 0, 0)),
                new DateTimeOffset(dt, new TimeSpan(-8, 0, 0))
            };

            // Act
            DeserializationHelpers.SetCollectionProperty(source, edmProperty, dtos, edmProperty.Name, timeZoneInfo: tzi);

            // Assert
            Assert.Equal(new List <DateTime> {
                dt.AddHours(-8), dt.AddHours(-15), dt
            }, source.DateTimeList);
        }