/// <inheritdoc /> public sealed override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext) { if (item == null) { return(null); } if (edmType == null) { throw Error.ArgumentNull("edmType"); } if (!edmType.IsCollection()) { throw new SerializationException( Error.Format(SRResources.TypeCannotBeDeserialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter))); } IEdmCollectionTypeReference collectionType = edmType.AsCollection(); IEdmTypeReference elementType = collectionType.ElementType(); ODataCollectionValue collection = item as ODataCollectionValue; if (collection == null) { throw Error.Argument("item", SRResources.ArgumentMustBeOfType, typeof(ODataCollectionValue).Name); } // Recursion guard to avoid stack overflows RuntimeHelpers.EnsureSufficientExecutionStack(); IEnumerable result = ReadCollectionValue(collection, elementType, readContext); if (result != null) { if (readContext.IsUntyped && elementType.IsComplex()) { EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(collectionType); foreach (EdmComplexObject complexObject in result) { complexCollection.Add(complexObject); } return(complexCollection); } else if (readContext.IsUntyped && elementType.IsEnum()) { EdmEnumObjectCollection enumCollection = new EdmEnumObjectCollection(collectionType); foreach (EdmEnumObject enumObject in result) { enumCollection.Add(enumObject); } return(enumCollection); } else { Type elementClrType = EdmLibHelpers.GetClrType(elementType, readContext.Model); IEnumerable castedResult = _castMethodInfo.MakeGenericMethod(elementClrType).Invoke(null, new object[] { result }) as IEnumerable; return(castedResult); } } return(null); }
public void Can_DeserializePayload_WithEnumCollections_InUntypedMode(string actionName, IEdmAction expectedAction, ODataPath path) { // Arrange const string Body = @"{ ""Colors"": [ ""Red"", ""Green""] }"; ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(Body)); message.SetHeader("Content-Type", "application/json"); ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), _model); ODataDeserializerContext context = new ODataDeserializerContext { Path = path, Model = _model, ResourceType = typeof(ODataUntypedActionParameters) }; // Act ODataUntypedActionParameters payload = _deserializer.Read(reader, typeof(ODataUntypedActionParameters), context) as ODataUntypedActionParameters; // Assert Assert.Same(expectedAction, payload.Action); Assert.NotNull(payload); Assert.True(payload.ContainsKey("Colors")); EdmEnumObjectCollection colors = payload["Colors"] as EdmEnumObjectCollection; EdmEnumObject color = colors[0] as EdmEnumObject; Assert.NotNull(color); Assert.Equal("Red", color.Value); }
private static EdmEntityObjectCollection GetCustomers() { if (_untypedSimpleOpenCustormers != null) { return(_untypedSimpleOpenCustormers); } IEdmModel edmModel = OpenEntityTypeTests.GetUntypedEdmModel(); IEdmEntityType customerType = edmModel.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "UntypedSimpleOpenCustomer"); EdmEntityObject customer = new EdmEntityObject(customerType); customer.TrySetPropertyValue("CustomerId", 1); //Add Numbers primitive collection property customer.TrySetPropertyValue("DeclaredNumbers", new[] { 1, 2 }); //Add Color, Colors enum(collection) property IEdmEnumType colorType = edmModel.SchemaElements.OfType <IEdmEnumType>().First(c => c.Name == "Color"); EdmEnumObject color = new EdmEnumObject(colorType, "Red"); EdmEnumObject color2 = new EdmEnumObject(colorType, "0"); EdmEnumObject color3 = new EdmEnumObject(colorType, "Red"); customer.TrySetPropertyValue("Color", color); List <IEdmEnumObject> colorList = new List <IEdmEnumObject>(); colorList.Add(color); colorList.Add(color2); colorList.Add(color3); IEdmCollectionTypeReference enumCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(colorType.ToEdmTypeReference(false))); EdmEnumObjectCollection colors = new EdmEnumObjectCollection(enumCollectionType, colorList); customer.TrySetPropertyValue("Colors", colors); customer.TrySetPropertyValue("DeclaredColors", colors); //Add Addresses complex(collection) property EdmComplexType addressType = edmModel.SchemaElements.OfType <IEdmComplexType>().First(c => c.Name == "Address") as EdmComplexType; EdmComplexObject address = new EdmComplexObject(addressType); address.TrySetPropertyValue("Street", "No1"); EdmComplexObject address2 = new EdmComplexObject(addressType); address2.TrySetPropertyValue("Street", "No2"); List <IEdmComplexObject> addressList = new List <IEdmComplexObject>(); addressList.Add(address); addressList.Add(address2); IEdmCollectionTypeReference complexCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(addressType.ToEdmTypeReference(false))); EdmComplexObjectCollection addresses = new EdmComplexObjectCollection(complexCollectionType, addressList); customer.TrySetPropertyValue("DeclaredAddresses", addresses); EdmEntityObjectCollection customers = new EdmEntityObjectCollection(new EdmCollectionTypeReference(new EdmCollectionType(customerType.ToEdmTypeReference(false)))); customers.Add(customer); _untypedSimpleOpenCustormers = customers; return(_untypedSimpleOpenCustormers); }
public void GetEdmType_Returns_EdmTypeInitializedByCtor() { // Arrange IEdmTypeReference elementType = new EdmEnumTypeReference(new EdmEnumType("NS", "Enum"), isNullable: false); IEdmCollectionTypeReference collectionType = new EdmCollectionTypeReference(new EdmCollectionType(elementType)); // Act var edmObject = new EdmEnumObjectCollection(collectionType); // Assert Assert.Same(collectionType, edmObject.GetEdmType()); }
public static bool TryCreateInstance(Type collectionType, IEdmCollectionTypeReference edmCollectionType, Type elementType, out IEnumerable instance) { Contract.Assert(collectionType != null); if (collectionType == typeof(EdmComplexObjectCollection)) { instance = new EdmComplexObjectCollection(edmCollectionType); return(true); } else if (collectionType == typeof(EdmEntityObjectCollection)) { instance = new EdmEntityObjectCollection(edmCollectionType); return(true); } else if (collectionType == typeof(EdmEnumObjectCollection)) { instance = new EdmEnumObjectCollection(edmCollectionType); return(true); } else if (TypeHelper.IsGenericType(collectionType)) { Type genericDefinition = collectionType.GetGenericTypeDefinition(); if (genericDefinition == typeof(IEnumerable <>) || genericDefinition == typeof(ICollection <>) || genericDefinition == typeof(IList <>)) { instance = Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType)) as IEnumerable; return(true); } } if (collectionType.IsArray) { // We dont know the size of the collection in advance. So, create a list and later call ToArray. instance = Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType)) as IEnumerable; return(true); } if (collectionType.GetConstructor(Type.EmptyTypes) != null && !collectionType.IsAbstract) { instance = Activator.CreateInstance(collectionType) as IEnumerable; return(true); } instance = null; return(false); }
public static IEdmObject ConvertToEdmObject(this IEnumerable enumerable, IEdmCollectionTypeReference collectionType) { Contract.Assert(enumerable != null); Contract.Assert(collectionType != null); IEdmTypeReference elementType = collectionType.ElementType(); if (elementType.IsEntity()) { EdmEntityObjectCollection entityCollection = new EdmEntityObjectCollection(collectionType); foreach (EdmEntityObject entityObject in enumerable) { entityCollection.Add(entityObject); } return(entityCollection); } else if (elementType.IsComplex()) { EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(collectionType); foreach (EdmComplexObject complexObject in enumerable) { complexCollection.Add(complexObject); } return(complexCollection); } else if (elementType.IsEnum()) { EdmEnumObjectCollection enumCollection = new EdmEnumObjectCollection(collectionType); foreach (EdmEnumObject enumObject in enumerable) { enumCollection.Add(enumObject); } return(enumCollection); } return(null); }
public bool EnumCollectionFunction(int key, [FromODataUri] EdmEnumObjectCollection colors) { Assert.NotNull(colors); IList <IEdmEnumObject> results = colors.ToList(); Assert.Equal(2, results.Count); // #1 EdmEnumObject color = results[0] as EdmEnumObject; Assert.NotNull(color); Assert.Equal("NS.Color", color.GetEdmType().FullName()); Assert.Equal("Red", color.Value); // #2 EdmEnumObject color2 = results[1] as EdmEnumObject; Assert.NotNull(color2); Assert.Equal("NS.Color", color2.GetEdmType().FullName()); Assert.Equal("Green", color2.Value); return(true); }
public bool EnumCollectionFunctionOnAttribute(int key, [FromODataUri] EdmEnumObjectCollection colors) { return(EnumCollectionFunction(key, colors)); }
public static bool TryCreateInstance(Type collectionType, IEdmCollectionTypeReference edmCollectionType, Type elementType, out IEnumerable instance) { Contract.Assert(collectionType != null); if (collectionType == typeof(EdmComplexObjectCollection)) { instance = new EdmComplexObjectCollection(edmCollectionType); return true; } else if (collectionType == typeof(EdmEntityObjectCollection)) { instance = new EdmEntityObjectCollection(edmCollectionType); return true; } else if (collectionType == typeof(EdmEnumObjectCollection)) { instance = new EdmEnumObjectCollection(edmCollectionType); return true; } else if (collectionType.IsGenericType) { Type genericDefinition = collectionType.GetGenericTypeDefinition(); if (genericDefinition == typeof(IEnumerable<>) || genericDefinition == typeof(ICollection<>) || genericDefinition == typeof(IList<>)) { instance = Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType)) as IEnumerable; return true; } } if (collectionType.IsArray) { // We dont know the size of the collection in advance. So, create a list and later call ToArray. instance = Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType)) as IEnumerable; return true; } if (collectionType.GetConstructor(Type.EmptyTypes) != null && !collectionType.IsAbstract) { instance = Activator.CreateInstance(collectionType) as IEnumerable; return true; } instance = null; return false; }
public IActionResult PostUntypedSimpleOpenCustomer(EdmEntityObject customer) { // Verify there is a string dynamic property in OpenEntityType object nameValue; customer.TryGetPropertyValue("Name", out nameValue); Type nameType; customer.TryGetPropertyType("Name", out nameType); Assert.NotNull(nameValue); Assert.Equal(typeof(String), nameType); Assert.Equal("FirstName 6", nameValue); // Verify there is a collection of double dynamic property in OpenEntityType object doubleListValue; customer.TryGetPropertyValue("DoubleList", out doubleListValue); Type doubleListType; customer.TryGetPropertyType("DoubleList", out doubleListType); Assert.NotNull(doubleListValue); Assert.Equal(typeof(List <Double>), doubleListType); // Verify there is a collection of complex type dynamic property in OpenEntityType object addressesValue; customer.TryGetPropertyValue("Addresses", out addressesValue); Assert.NotNull(addressesValue); // Verify there is a complex type dynamic property in OpenEntityType object addressValue; customer.TryGetPropertyValue("Address", out addressValue); Type addressType; customer.TryGetPropertyType("Address", out addressType); Assert.NotNull(addressValue); Assert.Equal(typeof(EdmComplexObject), addressType); // Verify there is a collection of enum type dynamic property in OpenEntityType object favoriteColorsValue; customer.TryGetPropertyValue("FavoriteColors", out favoriteColorsValue); EdmEnumObjectCollection favoriteColors = favoriteColorsValue as EdmEnumObjectCollection; Assert.NotNull(favoriteColorsValue); Assert.NotNull(favoriteColors); Assert.Equal(typeof(EdmEnumObject), favoriteColors[0].GetType()); // Verify there is an enum type dynamic property in OpenEntityType object favoriteColorValue; customer.TryGetPropertyValue("FavoriteColor", out favoriteColorValue); Assert.NotNull(favoriteColorValue); Assert.Equal("Red", ((EdmEnumObject)favoriteColorValue).Value); Type favoriteColorType; customer.TryGetPropertyType("FavoriteColor", out favoriteColorType); Assert.Equal(typeof(EdmEnumObject), favoriteColorType); // Verify there is a string dynamic property in OpenComplexType EdmComplexObject address = addressValue as EdmComplexObject; object cityValue; address.TryGetPropertyValue("City", out cityValue); Type cityType; address.TryGetPropertyType("City", out cityType); Assert.NotNull(cityValue); Assert.Equal(typeof(String), cityType); Assert.Equal("City 6", cityValue); // It reads as ODataUntypedValue, and the RawValue is the string with the "" return(Ok(customer)); }
/// <inheritdoc /> public sealed override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext) { if (item == null) { return null; } if (edmType == null) { throw Error.ArgumentNull("edmType"); } if (!edmType.IsCollection()) { throw new SerializationException( Error.Format(SRResources.TypeCannotBeDeserialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter))); } IEdmCollectionTypeReference collectionType = edmType.AsCollection(); IEdmTypeReference elementType = collectionType.ElementType(); ODataCollectionValue collection = item as ODataCollectionValue; if (collection == null) { throw Error.Argument("item", SRResources.ArgumentMustBeOfType, typeof(ODataCollectionValue).Name); } // Recursion guard to avoid stack overflows RuntimeHelpers.EnsureSufficientExecutionStack(); IEnumerable result = ReadCollectionValue(collection, elementType, readContext); if (result != null) { if (readContext.IsUntyped && elementType.IsComplex()) { EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(collectionType); foreach (EdmComplexObject complexObject in result) { complexCollection.Add(complexObject); } return complexCollection; } else if (readContext.IsUntyped && elementType.IsEnum()) { EdmEnumObjectCollection enumCollection = new EdmEnumObjectCollection(collectionType); foreach (EdmEnumObject enumObject in result) { enumCollection.Add(enumObject); } return enumCollection; } else { Type elementClrType = EdmLibHelpers.GetClrType(elementType, readContext.Model); IEnumerable castedResult = _castMethodInfo.MakeGenericMethod(elementClrType).Invoke(null, new object[] { result }) as IEnumerable; return castedResult; } } return null; }
private static EdmEntityObjectCollection GetCustomers() { if (_untypedSimpleOpenCustormers != null) { return _untypedSimpleOpenCustormers; } EdmEntityType customerType = new EdmEntityType("NS", "UntypedSimpleOpenCustomer", null, false, true); customerType.AddKeys(customerType.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32)); EdmEntityObject customer = new EdmEntityObject(customerType); customer.TrySetPropertyValue("CustomerId", 1); //Add Numbers primitive collection property customer.TrySetPropertyValue("DeclaredNumbers", new[] { 1, 2 }); //Add Color, Colors enum(collection) property EdmEnumType colorType = new EdmEnumType("NS", "Color"); colorType.AddMember(new EdmEnumMember(colorType, "Red", new EdmIntegerConstant(0))); EdmEnumObject color = new EdmEnumObject(colorType, "Red"); EdmEnumObject color2 = new EdmEnumObject(colorType, "0"); EdmEnumObject color3 = new EdmEnumObject(colorType, "Red"); customer.TrySetPropertyValue("Color", color); List<IEdmEnumObject> colorList = new List<IEdmEnumObject>(); colorList.Add(color); colorList.Add(color2); colorList.Add(color3); IEdmCollectionTypeReference enumCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(colorType.ToEdmTypeReference(false))); EdmEnumObjectCollection colors = new EdmEnumObjectCollection(enumCollectionType, colorList); customer.TrySetPropertyValue("Colors", colors); customer.TrySetPropertyValue("DeclaredColors", colors); //Add Addresses complex(collection) property EdmComplexType addressType = new EdmComplexType("NS", "Address", null, false, true); addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String); EdmComplexObject address = new EdmComplexObject(addressType); address.TrySetPropertyValue("Street", "No1"); EdmComplexObject address2 = new EdmComplexObject(addressType); address2.TrySetPropertyValue("Street", "No2"); List<IEdmComplexObject> addressList = new List<IEdmComplexObject>(); addressList.Add(address); addressList.Add(address2); IEdmCollectionTypeReference complexCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(addressType.ToEdmTypeReference(false))); EdmComplexObjectCollection addresses = new EdmComplexObjectCollection(complexCollectionType, addressList); customer.TrySetPropertyValue("DeclaredAddresses", addresses); EdmEntityObjectCollection customers = new EdmEntityObjectCollection(new EdmCollectionTypeReference(new EdmCollectionType(customerType.ToEdmTypeReference(false)))); customers.Add(customer); _untypedSimpleOpenCustormers = customers; return _untypedSimpleOpenCustormers; }