/// <inheritdoc /> public override async Task <object> ReadAsync(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { if (messageReader == null) { throw Error.ArgumentNull("messageReader"); } if (readContext == null) { throw new ArgumentNullException(nameof(readContext)); } IEdmTypeReference edmType = readContext.GetEdmType(type); Contract.Assert(edmType != null); if (!edmType.IsCollection()) { throw Error.Argument("type", SRResources.ArgumentMustBeOfType, EdmTypeKind.Collection); } IEdmCollectionTypeReference collectionType = edmType.AsCollection(); IEdmTypeReference elementType = collectionType.ElementType(); ODataCollectionReader reader = await messageReader.CreateODataCollectionReaderAsync(elementType).ConfigureAwait(false); return(ReadInline(await ReadCollectionAsync(reader).ConfigureAwait(false), edmType, readContext)); }
private static ODataCollectionValue ReadCollectionParameterValue(ODataCollectionReader collectionReader) { ODataCollectionValue value2; List <object> list = new List <object>(); while (collectionReader.Read()) { switch (collectionReader.State) { case ODataCollectionReaderState.CollectionStart: case ODataCollectionReaderState.CollectionEnd: { continue; } case ODataCollectionReaderState.Value: { list.Add(collectionReader.Item); continue; } case ODataCollectionReaderState.Completed: goto Label_004F; } throw new InvalidOperationException(System.Data.Services.Strings.DataServiceException_GeneralError); } Label_004F: value2 = new ODataCollectionValue(); value2.Items = list; return(value2); }
/// <summary> /// Reads the Action parameters payload and returns the WCF DS value representation of each parameter. /// </summary> /// <param name="segmentInfo">Info about the parameters payload to read.</param> /// <returns>The WCF DS representation of the parameters read.</returns> protected override object Read(SegmentInfo segmentInfo) { Debug.Assert(segmentInfo != null, "segmentInfo != null"); Debug.Assert( segmentInfo.TargetSource == RequestTargetSource.ServiceOperation && segmentInfo.Operation != null && segmentInfo.Operation.Kind == OperationKind.Action, "The ParametersDeserializer should only be called for an Action segment."); IEdmOperation operation = this.GetOperation(segmentInfo.Operation); ODataParameterReader reader = this.MessageReader.CreateODataParameterReader(operation); AssertReaderFormatIsExpected(this.MessageReader, ODataFormat.Json); Dictionary <string, object> parameters = new Dictionary <string, object>(EqualityComparer <string> .Default); ResourceType parameterResourceType; object convertedParameterValue; while (reader.Read()) { if (reader.State == ODataParameterReaderState.Completed) { break; } switch (reader.State) { case ODataParameterReaderState.Value: parameterResourceType = segmentInfo.Operation.Parameters.Single(p => p.Name == reader.Name).ParameterType; convertedParameterValue = this.ConvertValue(reader.Value, ref parameterResourceType); break; case ODataParameterReaderState.Collection: ODataCollectionReader collectionReader = reader.CreateCollectionReader(); parameterResourceType = segmentInfo.Operation.Parameters.Single(p => p.Name == reader.Name).ParameterType; Debug.Assert(parameterResourceType.ResourceTypeKind == ResourceTypeKind.Collection, "parameterResourceType.ResourceTypeKind == ResourceTypeKind.Collection"); convertedParameterValue = this.ConvertValue(ParameterDeserializer.ReadCollectionParameterValue(collectionReader), ref parameterResourceType); break; default: Debug.Assert(false, "Unreachable code path in Read()."); throw new InvalidOperationException(Microsoft.OData.Service.Strings.DataServiceException_GeneralError); } parameters.Add(reader.Name, convertedParameterValue); } // ODataLib allows nullable parameters to be missing from the payload. When that happens, we use null for the parameter value. foreach (IEdmOperationParameter parameterMetadata in operation.Parameters.Skip(operation.IsBound ? 1 : 0)) { object value; if (!parameters.TryGetValue(parameterMetadata.Name, out value)) { Debug.Assert(parameterMetadata.Type.IsNullable, "ODataParameterReader should only allows nullable parameters to be missing from the payload."); parameters.Add(parameterMetadata.Name, null); } } return(parameters); }
/// <summary> /// Reads the items from a collection and return it as an ODataCollectionValue. /// </summary> /// <param name="collectionReader">Collection reader to read from.</param> /// <returns>An ODataCollectionValue instance containing all items in the collection.</returns> private static ODataCollectionValue ReadCollectionParameterValue(ODataCollectionReader collectionReader) { Debug.Assert(collectionReader != null, "collectionReader != null"); Debug.Assert(collectionReader.State == ODataCollectionReaderState.Start, "The collection reader should not have been used."); List <object> collectionItems = new List <object>(); while (collectionReader.Read()) { if (collectionReader.State == ODataCollectionReaderState.Completed) { break; } switch (collectionReader.State) { case ODataCollectionReaderState.CollectionStart: case ODataCollectionReaderState.CollectionEnd: break; case ODataCollectionReaderState.Value: collectionItems.Add(collectionReader.Item); break; default: Debug.Assert(false, "Unreachable code path in ReadCollectionParameterValue()."); throw new InvalidOperationException(Microsoft.OData.Service.Strings.DataServiceException_GeneralError); } } ODataCollectionValue result = new ODataCollectionValue(); result.Items = collectionItems; return(result); }
private ODataResponse ReadResponse(ODataCollectionReader odataReader) { var collection = new List <object>(); while (odataReader.Read()) { if (odataReader.State == ODataCollectionReaderState.Completed) { break; } switch (odataReader.State) { case ODataCollectionReaderState.CollectionStart: break; case ODataCollectionReaderState.Value: collection.Add(GetPropertyValue(odataReader.Item)); break; case ODataCollectionReaderState.CollectionEnd: break; } } return(ODataResponse.FromCollection(collection)); }
/// <summary> /// Constructor. /// </summary> /// <param name="collectionReader">The reader to wrap.</param> /// <param name="testConfiguration">The test configuration to use.</param> public ODataCollectionReaderTestWrapper(ODataCollectionReader collectionReader, ReaderTestConfiguration testConfiguration) { ExceptionUtilities.CheckArgumentNotNull(collectionReader, "collectionReader"); ExceptionUtilities.CheckArgumentNotNull(testConfiguration, "testConfiguration"); this.collectionReader = collectionReader; this.testConfiguration = testConfiguration; }
private static void FillParameters(IEdmModel edmModel, List <KeyValuePair <String, Object> > parameters, Stream requestStream, IEdmOperation operation, String contentType) { if (!operation.Parameters.Any()) { return; } IODataRequestMessage requestMessage = new Infrastructure.OeInMemoryMessage(requestStream, contentType); var settings = new ODataMessageReaderSettings() { EnableMessageStreamDisposal = false }; using (var messageReader = new ODataMessageReader(requestMessage, settings, edmModel)) { ODataParameterReader parameterReader = messageReader.CreateODataParameterReader(operation); while (parameterReader.Read()) { Object value; switch (parameterReader.State) { case ODataParameterReaderState.Value: { value = OeEdmClrHelper.GetValue(edmModel, parameterReader.Value); break; } case ODataParameterReaderState.Collection: { ODataCollectionReader collectionReader = parameterReader.CreateCollectionReader(); value = OeEdmClrHelper.GetValue(edmModel, ReadCollection(collectionReader)); break; } case ODataParameterReaderState.Resource: { ODataReader reader = parameterReader.CreateResourceReader(); value = OeEdmClrHelper.GetValue(edmModel, ReadResource(reader)); break; } case ODataParameterReaderState.ResourceSet: { ODataReader reader = parameterReader.CreateResourceSetReader(); value = OeEdmClrHelper.GetValue(edmModel, ReadResourceSet(reader)); break; } default: continue; } parameters.Add(new KeyValuePair <String, Object>(parameterReader.Name, value)); } } }
/// <inheritdoc /> public override async Task <object> ReadAsync(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { if (messageReader == null) { throw Error.ArgumentNull("messageReader"); } IEdmTypeReference edmType = GetElementType(type, readContext); IEdmTypeReference elementType = edmType.AsCollection().ElementType(); ODataCollectionReader reader = await messageReader.CreateODataCollectionReaderAsync(elementType); return(ReadInline(await ReadCollectionAsync(reader), edmType, readContext)); }
private static string AddCollectionItem(ArrayList items, ODataCollectionReader reader, string typeName) { if (ODataCollectionReaderState.Value == reader.State) { items.Add(reader.Item); } else if (ODataCollectionReaderState.CollectionStart == reader.State) { typeName = reader.Item.ToString(); } return(typeName); }
internal static async Task <ODataCollectionValue> ReadCollectionAsync(ODataCollectionReader reader) { ArrayList items = new ArrayList(); string typeName = null; while (await reader.ReadAsync()) { typeName = AddCollectionItem(items, reader, typeName); } return(new ODataCollectionValue { Items = items.Cast <object>(), TypeName = typeName }); }
internal static ODataCollectionValue ReadCollection(ODataCollectionReader reader) { ArrayList items = new ArrayList(); string typeName = null; while (reader.Read()) { typeName = AddCollectionItem(items, reader, typeName); } return(new ODataCollectionValue { Items = items.Cast <object>(), TypeName = typeName }); }
/// <summary> /// Reads a value from the message reader. /// </summary> /// <param name="expectedClientType">The expected client type being materialized into.</param> /// <param name="expectedReaderType">The expected type for the underlying reader.</param> protected override void ReadWithExpectedType(IEdmTypeReference expectedClientType, IEdmTypeReference expectedReaderType) { if (!expectedClientType.IsCollection()) { throw new DataServiceClientException(DSClient.Strings.AtomMaterializer_TypeShouldBeCollectionError(expectedClientType.FullName())); } Type underlyingExpectedType = Nullable.GetUnderlyingType(this.ExpectedType) ?? this.ExpectedType; Debug.Assert(WebUtil.IsCLRTypeCollection(underlyingExpectedType, this.MaterializerContext.Model) || (SingleResult.HasValue && !SingleResult.Value), "expected type must be collection or single result must be false"); // We are here for two cases: // (1) Something like Execute<ICollection<T>>, in which case the underlyingExpectedType is ICollection<T> // (2) Execute<T> with the bool singleValue = false, in which case underlyingExpectedType is T Type collectionItemType = underlyingExpectedType; Type collectionICollectionType = ClientTypeUtil.GetImplementationType(underlyingExpectedType, typeof(ICollection <>)); if (collectionICollectionType != null) { // Case 1 : Something like Execute<ICollection<T>>, in which case the underlyingExpectedType is ICollection<T> collectionItemType = collectionICollectionType.GetGenericArguments()[0]; } else { // Case 2 : Execute<T> with the bool singleValue = false, in which case underlyingExpectedType is T collectionICollectionType = typeof(ICollection <>).MakeGenericType(new Type[] { collectionItemType }); } Type clrCollectionType = WebUtil.GetBackingTypeForCollectionProperty(collectionICollectionType); object collectionInstance = this.CollectionValueMaterializationPolicy.CreateCollectionInstance((IEdmCollectionTypeReference)expectedClientType, clrCollectionType); // Enumerator over our collection reader was created, then ApplyDataCollections was refactored to // take an enumerable instead of a ODataCollectionValue. Enumerator is being used as a bridge ODataCollectionReader collectionReader = messageReader.CreateODataCollectionReader(); NonEntityItemsEnumerable collectionEnumerable = new NonEntityItemsEnumerable(collectionReader); bool isElementNullable = expectedClientType.AsCollection().ElementType().IsNullable; this.CollectionValueMaterializationPolicy.ApplyCollectionDataValues( collectionEnumerable, null /*wireTypeName*/, collectionInstance, collectionItemType, ClientTypeUtil.GetAddToCollectionDelegate(collectionICollectionType), isElementNullable); this.currentValue = collectionInstance; }
private string WriteAndVerifyCollection(StreamResponseMessage responseMessage, ODataCollectionWriter odataWriter, bool hasModel, string mimeType) { var collectionStart = new ODataCollectionStart() { Name = "BackupContactInfo", Count = 12, NextPageLink = new Uri("http://localhost") }; if (!hasModel) { collectionStart.SetSerializationInfo(new ODataCollectionStartSerializationInfo() { CollectionTypeName = "Collection(" + NameSpace + "ContactDetails)" }); } odataWriter.WriteStart(collectionStart); odataWriter.WriteItem(WritePayloadHelper.CreatePrimaryContactODataComplexValue()); odataWriter.WriteEnd(); Stream stream = responseMessage.GetStream(); if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata)) { stream.Seek(0, SeekOrigin.Begin); var settings = new ODataMessageReaderSettings() { BaseUri = this.ServiceUri }; ODataMessageReader messageReader = new ODataMessageReader(responseMessage, settings, WritePayloadHelper.Model); ODataCollectionReader reader = messageReader.CreateODataCollectionReader(WritePayloadHelper.ContactDetailType); bool collectionRead = false; while (reader.Read()) { if (reader.State == ODataCollectionReaderState.CollectionEnd) { collectionRead = true; } } Assert.IsTrue(collectionRead, "collectionRead"); Assert.AreEqual(ODataCollectionReaderState.Completed, reader.State); } return(WritePayloadHelper.ReadStreamContent(stream)); }
private Expression[] ProcessActionInvokePostBody(IODataRequestMessage message, IEdmOperation operation) { using (var messageReader = new ODataMessageReader(message, this.GetReaderSettings())) { List <Expression> parameterValues = new List <Expression>(); var parameterReader = messageReader.CreateODataParameterReader(operation); while (parameterReader.Read()) { switch (parameterReader.State) { case ODataParameterReaderState.Value: { object clrValue = ODataObjectModelConverter.ConvertPropertyValue(parameterReader.Value); parameterValues.Add(Expression.Constant(clrValue)); break; } case ODataParameterReaderState.Collection: { ODataCollectionReader collectionReader = parameterReader.CreateCollectionReader(); object clrValue = ODataObjectModelConverter.ConvertPropertyValue(ODataObjectModelConverter.ReadCollectionParameterValue(collectionReader)); parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType())); break; } case ODataParameterReaderState.Resource: { var entryReader = parameterReader.CreateResourceReader(); object clrValue = ODataObjectModelConverter.ReadEntityOrEntityCollection(entryReader, false); parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType())); break; } case ODataParameterReaderState.ResourceSet: { var feedReader = parameterReader.CreateResourceSetReader(); var collectionList = ODataObjectModelConverter.ReadEntityOrEntityCollection(feedReader, true); parameterValues.Add(Expression.Constant(collectionList, collectionList.GetType())); break; } } } return(parameterValues.ToArray()); } }
protected override object Read(System.Data.Services.SegmentInfo segmentInfo) { Func <OperationParameter, bool> predicate = null; Func <OperationParameter, bool> func2 = null; IEdmFunctionImport functionImport = base.GetFunctionImport(segmentInfo.Operation); ODataParameterReader reader = base.MessageReader.CreateODataParameterReader(functionImport); Dictionary <string, object> dictionary = new Dictionary <string, object>(EqualityComparer <string> .Default); while (reader.Read()) { ResourceType parameterType; object obj2; switch (reader.State) { case ODataParameterReaderState.Value: if (predicate == null) { predicate = p => p.Name == reader.Name; } parameterType = segmentInfo.Operation.Parameters.Single <OperationParameter>(predicate).ParameterType; obj2 = base.ConvertValue(reader.Value, ref parameterType); break; case ODataParameterReaderState.Collection: { ODataCollectionReader collectionReader = reader.CreateCollectionReader(); if (func2 == null) { func2 = p => p.Name == reader.Name; } parameterType = segmentInfo.Operation.Parameters.Single <OperationParameter>(func2).ParameterType; obj2 = base.ConvertValue(ReadCollectionParameterValue(collectionReader), ref parameterType); break; } case ODataParameterReaderState.Completed: return(dictionary); default: throw new InvalidOperationException(System.Data.Services.Strings.DataServiceException_GeneralError); } dictionary.Add(reader.Name, obj2); } return(dictionary); }
/// <summary> /// Reads Collection Parameter values provided by a client in a POST request to invoke a particular Action. /// </summary> /// <param name="reader">OData collection reader.</param> /// <returns>OData representation of a Collection.</returns> private ODataCollectionValue ReadCollection(ODataCollectionReader reader) { ArrayList items = new ArrayList(); string typeName = null; while (reader.Read()) { if (ODataCollectionReaderState.Value == reader.State) { items.Add(reader.Item); } else if (ODataCollectionReaderState.CollectionStart == reader.State) { typeName = reader.Item.ToString(); } } return(new ODataCollectionValue { Items = items, TypeName = typeName }); }
internal static async Task <ODataCollectionValue> ReadCollectionAsync(ODataCollectionReader reader) { ArrayList items = new ArrayList(); string typeName = null; while (await reader.ReadAsync().ConfigureAwait(false)) { if (ODataCollectionReaderState.Value == reader.State) { items.Add(reader.Item); } else if (ODataCollectionReaderState.CollectionStart == reader.State) { typeName = reader.Item.ToString(); } } return(new ODataCollectionValue { Items = items.Cast <object>(), TypeName = typeName }); }
private object Convert(ODataCollectionReader reader, IEdmCollectionTypeReference collectionType, ODataDeserializerContext readContext) { IEdmTypeReference elementType = collectionType.ElementType(); Type clrElementType = EdmLibHelpers.GetClrType(elementType, readContext.Model); IList list = Activator.CreateInstance(typeof(List <>).MakeGenericType(clrElementType)) as IList; while (reader.Read()) { switch (reader.State) { case ODataCollectionReaderState.Value: object element = Convert(reader.Item, elementType, readContext); list.Add(element); break; default: break; } } return(list); }
public static ODataCollectionValue ReadCollectionParameterValue(ODataCollectionReader collectionReader) { List <object> collectionItems = new List <object>(); while (collectionReader.Read()) { if (collectionReader.State == ODataCollectionReaderState.Completed) { break; } if (collectionReader.State == ODataCollectionReaderState.Value) { collectionItems.Add(collectionReader.Item); } } ODataCollectionValue result = new ODataCollectionValue(); result.Items = collectionItems; return(result); }
private static ODataCollectionValue ReadCollection(ODataCollectionReader collectionReader) { var items = new List <Object>(); while (collectionReader.Read()) { if (collectionReader.State == ODataCollectionReaderState.Completed) { break; } if (collectionReader.State == ODataCollectionReaderState.Value) { items.Add(collectionReader.Item); } } return(new ODataCollectionValue() { Items = items }); }
/// <inheritdoc /> public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { if (messageReader == null) { throw Error.ArgumentNull("messageReader"); } IEdmTypeReference edmType = readContext.GetEdmType(type); Contract.Assert(edmType != null); if (!edmType.IsCollection()) { throw Error.Argument("type", SRResources.ArgumentMustBeOfType, EdmTypeKind.Collection); } IEdmCollectionTypeReference collectionType = edmType.AsCollection(); IEdmTypeReference elementType = collectionType.ElementType(); ODataCollectionReader reader = messageReader.CreateODataCollectionReader(elementType); return(ReadInline(ReadCollection(reader), edmType, readContext)); }
private ODataCollectionValue ReadCollection(ODataMessageReader messageReader) { Contract.Assert(messageReader != null); ODataCollectionReader reader = messageReader.CreateODataCollectionReader(_edmCollectionType.ElementType()); ArrayList items = new ArrayList(); string typeName = null; while (reader.Read()) { if (ODataCollectionReaderState.Value == reader.State) { items.Add(reader.Item); } else if (ODataCollectionReaderState.CollectionStart == reader.State) { typeName = reader.Item.ToString(); } } return(new ODataCollectionValue { Items = items, TypeName = typeName }); }
/// <summary> /// Initializes a new instance of the <see cref="NonEntityItemsEnumerable"/> class. /// </summary> /// <param name="collectionReader">The collection reader.</param> internal NonEntityItemsEnumerable(ODataCollectionReader collectionReader) { this.collectionReader = collectionReader; }
private Expression[] ProcessActionInvokePostBody(IODataRequestMessage message, IEdmOperation operation) { using (var messageReader = new ODataMessageReader(message, this.GetReaderSettings(), this.DataSource.Model)) { List <Expression> parameterValues = new List <Expression>(); var parameterReader = messageReader.CreateODataParameterReader(operation); while (parameterReader.Read()) { switch (parameterReader.State) { case ODataParameterReaderState.Value: { object clrValue = ODataObjectModelConverter.ConvertPropertyValue(parameterReader.Value); parameterValues.Add(Expression.Constant(clrValue)); break; } case ODataParameterReaderState.Collection: { ODataCollectionReader collectionReader = parameterReader.CreateCollectionReader(); object clrValue = ODataObjectModelConverter.ConvertPropertyValue(ODataObjectModelConverter.ReadCollectionParameterValue(collectionReader)); parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType())); break; } case ODataParameterReaderState.Entry: { var entryReader = parameterReader.CreateEntryReader(); object clrValue = ODataObjectModelConverter.ConvertPropertyValue(ODataObjectModelConverter.ReadEntryParameterValue(entryReader)); parameterValues.Add(Expression.Constant(clrValue, clrValue.GetType())); break; } case ODataParameterReaderState.Feed: { IList collectionList = null; var feedReader = parameterReader.CreateFeedReader(); while (feedReader.Read()) { if (feedReader.State == ODataReaderState.EntryEnd) { object clrItem = ODataObjectModelConverter.ConvertPropertyValue(feedReader.Item); if (collectionList == null) { Type itemType = clrItem.GetType(); Type listType = typeof(List <>).MakeGenericType(new[] { itemType }); collectionList = (IList)Utility.QuickCreateInstance(listType); } collectionList.Add(clrItem); } } parameterValues.Add(Expression.Constant(collectionList, collectionList.GetType())); break; } } } return(parameterValues.ToArray()); } }