public void SetCollectionProperty_CollectionTypeCannotBeInstantiated_And_SettableProperty_Throws(string propertyName) { object value = new SampleClassWithSettableCollectionProperties(); Assert.Throws <SerializationException>( () => DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> { 1, 2, 3 }), String.Format("The property '{0}' on type 'System.Web.Http.OData.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithSettableCollectionProperties' returned a null value. " + "The input stream contains collection items which cannot be added if the instance is null.", propertyName)); }
public void SetCollectionProperty_CanConvertEnumCollection() { SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties(); DeserializationHelpers.SetCollectionProperty(value, "FlagsEnum", isDelta: false, value: new List <string> { "One", "Four, Two" }); Assert.Equal( new FlagsEnum[] { FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two }, value.FlagsEnum); }
public void SetCollectionProperty_CollectionTypeCanBeInstantiated_And_SettableProperty(string propertyName) { object value = new SampleClassWithSettableCollectionProperties(); DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> { 1, 2, 3 }); Assert.Equal( new[] { 1, 2, 3 }, value.GetType().GetProperty(propertyName).GetValue(value, index: null) as IEnumerable <int>); }
public void SetCollectionProperty_CanConvertNonStandardEdmTypes() { SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties(); DeserializationHelpers.SetCollectionProperty(value, "UnsignedArray", isDelta: false, value: new List <int> { 1, 2, 3 }); Assert.Equal( new uint[] { 1, 2, 3 }, value.UnsignedArray); }
public void SetCollectionProperty_NonSettableProperty_ArrayValue_FixedSize_Throws(string propertyName) { object value = new SampleClassWithNonSettableCollectionProperties(); Type propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType; Assert.Throws <SerializationException>( () => DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> { 1, 2, 3 }), String.Format("The value of the property '{0}' on type 'System.Web.Http.OData.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' is an array. " + "Consider adding a setter for the property.", propertyName)); }
public void SetCollectionProperty_NonSettableProperty_NonNullValue_NoAdd_Throws(string propertyName) { object value = new SampleClassWithNonSettableCollectionProperties(); Type propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType; Assert.Throws <SerializationException>( () => DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> { 1, 2, 3 }), String.Format("The type '{0}' of the property '{1}' on type 'System.Web.Http.OData.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)); }
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 }); Assert.Equal( new[] { 1, 2, 3 }, value.GetType().GetProperty(propertyName).GetValue(value, index: null) as IEnumerable <int>); }
public void SetCollectionProperty_CanConvertEnumCollection() { SampleClassWithDifferentCollectionProperties value = new SampleClassWithDifferentCollectionProperties(); IEdmProperty edmProperty = GetMockEdmProperty("FlagsEnum", EdmPrimitiveTypeKind.String); DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <string> { "One", "Four, Two" }); Assert.Equal( new FlagsEnum[] { FlagsEnum.One, FlagsEnum.Four | FlagsEnum.Two }, value.FlagsEnum); }
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 }); Assert.Equal( new uint[] { 1, 2, 3 }, value.UnsignedArray); }
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); Assert.Throws <SerializationException>( () => DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> { 1, 2, 3 }), String.Format("The property '{0}' on type 'System.Web.Http.OData.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; Assert.Throws <SerializationException>( () => DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> { 1, 2, 3 }), Error.Format( "The type '{0}' of the property '{1}' on type 'System.Web.Http.OData.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithDifferentCollectionProperties' must be a collection.", propertyType.FullName, propertyName)); }
/// <summary> /// Deserializes the given <paramref name="structuralProperty"/> into <paramref name="entityResource"/>. /// </summary> /// <param name="entityResource">The object into which the structural property should be read.</param> /// <param name="structuralProperty">The entry object containing the structural properties.</param> /// <param name="readContext">The deserializer context.</param> public virtual void ApplyStructuralProperty(object entityResource, ODataProperty structuralProperty, ODataDeserializerContext readContext) { if (entityResource == null) { throw Error.ArgumentNull("entityResource"); } if (structuralProperty == null) { throw Error.ArgumentNull("structuralProperty"); } DeserializationHelpers.ApplyProperty(structuralProperty, EntityType, entityResource, DeserializerProvider, readContext); }
private void ApplyFeedInNavigationProperty(IEdmNavigationProperty navigationProperty, object entityResource, ODataFeedWithEntries feed, ODataDeserializerContext readContext) { Contract.Assert(navigationProperty != null && navigationProperty.PropertyKind == EdmPropertyKind.Navigation, "navigationProperty != null && navigationProperty.TypeKind == ResourceTypeKind.EntityType"); Contract.Assert(entityResource != null, "entityResource != null"); if (readContext.IsDeltaOfT) { string message = Error.Format(SRResources.CannotPatchNavigationProperties, navigationProperty.Name, navigationProperty.DeclaringEntityType().FullName()); throw new ODataException(message); } ODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(navigationProperty.Type); if (deserializer == null) { throw new SerializationException(Error.Format(SRResources.TypeCannotBeDeserialized, navigationProperty.Type.FullName(), typeof(ODataMediaTypeFormatter))); } object value = deserializer.ReadInline(feed, navigationProperty.Type, readContext); DeserializationHelpers.SetCollectionProperty(entityResource, navigationProperty, value); }
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>); }
public void ApplyProperty_DoesNotIgnoreKeyProperty() { // Arrange ODataProperty property = new ODataProperty { Name = "Key1", Value = "Value1" }; EdmEntityType entityType = new EdmEntityType("namespace", "name"); entityType.AddKeys(new EdmStructuralProperty(entityType, "Key1", EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string)))); EdmEntityTypeReference entityTypeReference = new EdmEntityTypeReference(entityType, isNullable: false); ODataDeserializerProvider provider = new DefaultODataDeserializerProvider(); var resource = new Mock <IDelta>(MockBehavior.Strict); Type propertyType = typeof(string); resource.Setup(r => r.TryGetPropertyType("Key1", out propertyType)).Returns(true).Verifiable(); resource.Setup(r => r.TrySetPropertyValue("Key1", "Value1")).Returns(true).Verifiable(); // Act DeserializationHelpers.ApplyProperty(property, entityTypeReference, resource.Object, provider, new ODataDeserializerContext()); // Assert resource.Verify(); }