public void ReadInline_ThrowsArgumentNull_Item() { var deserializer = new ODataEntityDeserializer(_deserializerProvider); Assert.ThrowsArgumentNull( () => deserializer.ReadInline(item: null, edmType: _productEdmType, readContext: new ODataDeserializerContext()), "item"); }
public void ReadInline_Throws_ArgumentMustBeOfType() { var deserializer = new ODataEntityDeserializer(_deserializerProvider); Assert.ThrowsArgument( () => deserializer.ReadInline(item: 42, edmType: _productEdmType, readContext: new ODataDeserializerContext()), "item", "The argument must be of type 'ODataEntry'"); }
public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { if (messageReader == null) { throw Error.ArgumentNull("messageReader"); } IEdmAction action = GetAction(readContext); Contract.Assert(action != null); // Create the correct resource type; Dictionary <string, object> payload; if (type == typeof(ODataActionParameters)) { payload = new ODataActionParameters(); } else { payload = new ODataUntypedActionParameters(action); } ODataParameterReader reader = messageReader.CreateODataParameterReader(action); while (reader.Read()) { string parameterName = null; IEdmOperationParameter parameter = null; switch (reader.State) { case ODataParameterReaderState.Value: parameterName = reader.Name; parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName); // ODataLib protects against this but asserting just in case. Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName)); if (parameter.Type.IsPrimitive()) { payload[parameterName] = reader.Value; } else { ODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(parameter.Type); payload[parameterName] = deserializer.ReadInline(reader.Value, parameter.Type, readContext); } break; case ODataParameterReaderState.Collection: parameterName = reader.Name; parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName); // ODataLib protects against this but asserting just in case. Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName)); IEdmCollectionTypeReference collectionType = parameter.Type as IEdmCollectionTypeReference; Contract.Assert(collectionType != null); ODataCollectionValue value = ODataCollectionDeserializer.ReadCollection(reader.CreateCollectionReader()); ODataCollectionDeserializer collectionDeserializer = (ODataCollectionDeserializer)DeserializerProvider.GetEdmTypeDeserializer(collectionType); payload[parameterName] = collectionDeserializer.ReadInline(value, collectionType, readContext); break; case ODataParameterReaderState.Entry: parameterName = reader.Name; parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName); Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName)); IEdmEntityTypeReference entityTypeReference = parameter.Type as IEdmEntityTypeReference; Contract.Assert(entityTypeReference != null); ODataReader entryReader = reader.CreateEntryReader(); object item = ODataEntityDeserializer.ReadEntryOrFeed(entryReader); ODataEntityDeserializer entityDeserializer = (ODataEntityDeserializer)DeserializerProvider.GetEdmTypeDeserializer(entityTypeReference); payload[parameterName] = entityDeserializer.ReadInline(item, entityTypeReference, readContext); break; case ODataParameterReaderState.Feed: parameterName = reader.Name; parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName); Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName)); IEdmCollectionTypeReference feedType = parameter.Type as IEdmCollectionTypeReference; Contract.Assert(feedType != null); ODataReader feedReader = reader.CreateFeedReader(); object feed = ODataEntityDeserializer.ReadEntryOrFeed(feedReader); ODataFeedDeserializer feedDeserializer = (ODataFeedDeserializer)DeserializerProvider.GetEdmTypeDeserializer(feedType); object result = feedDeserializer.ReadInline(feed, feedType, readContext); IEdmTypeReference elementTypeReference = feedType.ElementType(); Contract.Assert(elementTypeReference.IsEntity()); IEnumerable enumerable = result as IEnumerable; if (enumerable != null) { if (readContext.IsUntyped) { EdmEntityObjectCollection entityCollection = new EdmEntityObjectCollection(feedType); foreach (EdmEntityObject entityObject in enumerable) { entityCollection.Add(entityObject); } payload[parameterName] = entityCollection; } else { Type elementClrType = EdmLibHelpers.GetClrType(elementTypeReference, readContext.Model); IEnumerable castedResult = _castMethodInfo.MakeGenericMethod(elementClrType) .Invoke(null, new[] { result }) as IEnumerable; payload[parameterName] = castedResult; } } break; } } return(payload); }