/// <inheritdoc /> public override async Task <object> ReadAsync(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { if (messageReader == null) { throw Error.ArgumentNull(nameof(messageReader)); } if (readContext == null) { throw Error.ArgumentNull(nameof(readContext)); } IEdmTypeReference edmType = readContext.GetEdmType(type); Contract.Assert(edmType != null); // TODO: is it ok to read the top level collection of entity? if (!(edmType.IsCollection() && edmType.AsCollection().ElementType().IsStructured())) { throw Error.Argument("edmType", SRResources.ArgumentMustBeOfType, EdmTypeKind.Complex + " or " + EdmTypeKind.Entity); } ODataReader resourceSetReader = await messageReader.CreateODataResourceSetReaderAsync().ConfigureAwait(false); object resourceSet = await resourceSetReader.ReadResourceOrResourceSetAsync().ConfigureAwait(false); return(ReadInline(resourceSet, edmType, readContext)); }
/// <inheritdoc /> public override async Task <object> ReadAsync(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { if (messageReader == null) { throw new ArgumentNullException(nameof(messageReader)); } if (readContext == null) { throw new ArgumentNullException(nameof(readContext)); } IEdmTypeReference edmType = readContext.GetEdmType(type); Contract.Assert(edmType != null); if (!edmType.IsStructured()) { throw Error.Argument("type", SRResources.ArgumentMustBeOfType, "Structured"); } IEdmStructuredTypeReference structuredType = edmType.AsStructured(); IEdmNavigationSource navigationSource = null; if (structuredType.IsEntity()) { if (readContext.Path == null) { throw Error.Argument("readContext", SRResources.ODataPathMissing); } navigationSource = readContext.Path.GetNavigationSource(); if (navigationSource == null) { throw new SerializationException(SRResources.NavigationSourceMissingDuringDeserialization); } } ODataReader odataReader = await messageReader .CreateODataResourceReaderAsync(navigationSource, structuredType.StructuredDefinition()).ConfigureAwait(false); ODataResourceWrapper topLevelResource = await odataReader.ReadResourceOrResourceSetAsync().ConfigureAwait(false) as ODataResourceWrapper; Contract.Assert(topLevelResource != null); return(ReadInline(topLevelResource, structuredType, readContext)); }
/// <inheritdoc /> public override async Task <object> ReadAsync(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { if (messageReader == null) { throw Error.ArgumentNull(nameof(messageReader)); } if (readContext == null) { throw Error.ArgumentNull(nameof(readContext)); } // TODO: Need to modify how to get the Edm type for the delta resource set? IEdmTypeReference edmType = readContext.GetEdmType(type); Contract.Assert(edmType != null); IEdmEntityType entityType = edmType.Definition as IEdmEntityType; EdmDeltaCollectionType edmCollectionType = new EdmDeltaCollectionType(edmType); edmType = new EdmCollectionTypeReference(edmCollectionType); // TODO: is it ok to read the top level collection of entity? if (!(edmType.IsCollection() && edmType.AsCollection().ElementType().IsStructured())) { throw Error.Argument("edmType", SRResources.ArgumentMustBeOfType, EdmTypeKind.Complex + " or " + EdmTypeKind.Entity); } IEdmNavigationSource navigationSource; if (readContext.Path == null) { throw Error.Argument("readContext", SRResources.ODataPathMissing); } navigationSource = readContext.Path.GetNavigationSource(); if (navigationSource == null) { throw new SerializationException(SRResources.NavigationSourceMissingDuringDeserialization); } ODataReader resourceSetReader = await messageReader.CreateODataDeltaResourceSetReaderAsync(navigationSource as IEdmEntitySet, entityType).ConfigureAwait(false); object deltaResourceSet = await resourceSetReader.ReadResourceOrResourceSetAsync().ConfigureAwait(false); return(ReadInline(deltaResourceSet, edmType, readContext)); }
/// <inheritdoc /> public override async Task <object> ReadAsync(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { if (messageReader == null) { throw Error.ArgumentNull("messageReader"); } IEdmStructuredTypeReference structuredType = GetStructuredType(type, readContext); IEdmNavigationSource navigationSource = GetNavigationSource(structuredType, readContext); ODataReader odataReader = await messageReader.CreateODataResourceReaderAsync(navigationSource, structuredType.StructuredDefinition()); ODataResourceWrapper topLevelResource = await odataReader.ReadResourceOrResourceSetAsync() as ODataResourceWrapper; Contract.Assert(topLevelResource != null); return(ReadInline(topLevelResource, structuredType, readContext)); }
private async Task <ODataItemBase> ReadPayloadAsync(string payload, IEdmModel edmModel, Func <ODataMessageReader, Task <ODataReader> > createReader) { var message = new InMemoryMessage() { Stream = new MemoryStream(Encoding.UTF8.GetBytes(payload)) }; message.SetHeader("Content-Type", "application/json;odata.metadata=minimal"); ODataMessageReaderSettings readerSettings = new ODataMessageReaderSettings() { BaseUri = new Uri("http://localhost/$metadata"), EnableMessageStreamDisposal = true, Version = ODataVersion.V4, }; using (var msgReader = new ODataMessageReader((IODataResponseMessageAsync)message, readerSettings, edmModel)) { ODataReader reader = await createReader(msgReader); return(await reader.ReadResourceOrResourceSetAsync()); } }
private static object ConvertResource(ODataMessageReader oDataMessageReader, IEdmTypeReference edmTypeReference, ODataDeserializerContext readContext) { EdmEntitySet tempEntitySet = null; if (edmTypeReference.IsEntity()) { IEdmEntityTypeReference entityType = edmTypeReference.AsEntity(); tempEntitySet = new EdmEntitySet(readContext.Model.EntityContainer, "temp", entityType.EntityDefinition()); } // TODO: Sam xu, can we use the parameter-less overload ODataReader resourceReader = oDataMessageReader.CreateODataUriParameterResourceReader(tempEntitySet, edmTypeReference.ToStructuredType()); object item = resourceReader.ReadResourceOrResourceSetAsync(); ODataResourceWrapper topLevelResource = item as ODataResourceWrapper; Contract.Assert(topLevelResource != null); ODataDeserializerProvider deserializerProvider = readContext.Request.GetDeserializerProvider(); ODataResourceDeserializer entityDeserializer = (ODataResourceDeserializer)deserializerProvider.GetEdmTypeDeserializer(edmTypeReference); object value = entityDeserializer.ReadInline(topLevelResource, edmTypeReference, readContext); if (edmTypeReference.IsEntity()) { IEdmEntityTypeReference entityType = edmTypeReference.AsEntity(); return(CovertResourceId(value, topLevelResource.Resource, entityType, readContext)); } return(value); }
public async Task ReadResourceOrResourceSetAsync_ThrowsArgumentNull_EdmType() { // Arrange & Act & Assert ODataReader reader = null; await ExceptionAssert.ThrowsArgumentNullAsync(() => reader.ReadResourceOrResourceSetAsync(), "reader"); }
public override async Task <object> ReadAsync(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 = await messageReader.CreateODataParameterReaderAsync(action).ConfigureAwait(false); while (await reader.ReadAsync().ConfigureAwait(false)) { 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 = await ODataCollectionDeserializer .ReadCollectionAsync(reader.CreateCollectionReader()).ConfigureAwait(false); ODataCollectionDeserializer collectionDeserializer = (ODataCollectionDeserializer)DeserializerProvider.GetEdmTypeDeserializer(collectionType); payload[parameterName] = collectionDeserializer.ReadInline(value, collectionType, readContext); break; case ODataParameterReaderState.Resource: parameterName = reader.Name; parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName); Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName)); Contract.Assert(parameter.Type.IsStructured()); ODataReader resourceReader = reader.CreateResourceReader(); object item = await resourceReader.ReadResourceOrResourceSetAsync().ConfigureAwait(false); ODataResourceDeserializer resourceDeserializer = (ODataResourceDeserializer)DeserializerProvider.GetEdmTypeDeserializer(parameter.Type); payload[parameterName] = resourceDeserializer.ReadInline(item, parameter.Type, readContext); break; case ODataParameterReaderState.ResourceSet: 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 resourceSetType = parameter.Type as IEdmCollectionTypeReference; Contract.Assert(resourceSetType != null); ODataReader resourceSetReader = reader.CreateResourceSetReader(); object feed = await resourceSetReader.ReadResourceOrResourceSetAsync().ConfigureAwait(false); ODataResourceSetDeserializer resourceSetDeserializer = (ODataResourceSetDeserializer)DeserializerProvider.GetEdmTypeDeserializer(resourceSetType); object result = resourceSetDeserializer.ReadInline(feed, resourceSetType, readContext); IEdmTypeReference elementTypeReference = resourceSetType.ElementType(); Contract.Assert(elementTypeReference.IsStructured()); IEnumerable enumerable = result as IEnumerable; if (enumerable != null) { if (readContext.IsNoClrType) { payload[parameterName] = enumerable.ConvertToEdmObject(resourceSetType); } else { Type elementClrType = readContext.Model.GetClrType(elementTypeReference); IEnumerable castedResult = _castMethodInfo.MakeGenericMethod(elementClrType) .Invoke(null, new[] { result }) as IEnumerable; payload[parameterName] = castedResult; } } break; } } return(payload); }