private void WriteParameters(ODataMessageWriterTestWrapper messageWriter, ODataParameters parameters, IEdmOperationImport functionImport) { ODataParameterWriter parameterWriter = messageWriter.CreateODataParameterWriter(functionImport); parameterWriter.WriteStart(); foreach (var parameter in parameters) { ODataCollectionStart collectionStart = parameter.Value as ODataCollectionStart; ODataResourceSet feed; ODataResource entry; if (collectionStart != null) { ODataCollectionWriter collectionWriter = parameterWriter.CreateCollectionWriter(parameter.Key); this.WriteCollection(collectionWriter, collectionStart); collectionWriter.Flush(); } else if ((feed = parameter.Value as ODataResourceSet) != null) { this.WriteFeed(parameterWriter.CreateResourceSetWriter(parameter.Key), feed); } else if ((entry = parameter.Value as ODataResource) != null) { this.WriteEntry(parameterWriter.CreateResourceWriter(parameter.Key), entry); } else { parameterWriter.WriteValue(parameter.Key, parameter.Value); } } parameterWriter.WriteEnd(); parameterWriter.Flush(); }
/// <summary> /// Writes collection value in body operation parameter. /// </summary> /// <param name="parameterWriter">The odata parameter writer.</param> /// <param name="operationParameter">The operation parameter.</param> /// <param name="edmCollectionType">The edm collection type.</param> private void WriteCollectionValueInBodyOperationParameter(ODataParameterWriter parameterWriter, BodyOperationParameter operationParameter, IEdmCollectionType edmCollectionType) { ClientEdmModel model = this.requestInfo.Model; var elementTypeKind = edmCollectionType.ElementType.TypeKind(); if (elementTypeKind == EdmTypeKind.Entity || elementTypeKind == EdmTypeKind.Complex) { ODataWriter feedWriter = parameterWriter.CreateResourceSetWriter(operationParameter.Name); feedWriter.WriteStart(new ODataResourceSet()); IEnumerator enumerator = ((ICollection)operationParameter.Value).GetEnumerator(); while (enumerator.MoveNext()) { Object collectionItem = enumerator.Current; if (collectionItem == null) { if (elementTypeKind == EdmTypeKind.Complex) { feedWriter.WriteStart((ODataResource)null); feedWriter.WriteEnd(); continue; } else { throw new NotSupportedException(Strings.Serializer_NullCollectionParamterItemValue(operationParameter.Name)); } } IEdmType edmItemType = model.GetOrCreateEdmType(collectionItem.GetType()); Debug.Assert(edmItemType != null, "edmItemType != null"); if (edmItemType.TypeKind != EdmTypeKind.Entity && edmItemType.TypeKind != EdmTypeKind.Complex) { throw new NotSupportedException(Strings.Serializer_InvalidCollectionParamterItemType(operationParameter.Name, edmItemType.TypeKind)); } Debug.Assert(model.GetClientTypeAnnotation(edmItemType).ElementType != null, "edmItemType.GetClientTypeAnnotation().ElementType != null"); ODataResourceWrapper entry = this.CreateODataResourceFromEntityOperationParameter(model.GetClientTypeAnnotation(edmItemType), collectionItem); Debug.Assert(entry != null, "entry != null"); ODataWriterHelper.WriteResource(feedWriter, entry); } feedWriter.WriteEnd(); feedWriter.Flush(); } else { ODataCollectionWriter collectionWriter = parameterWriter.CreateCollectionWriter(operationParameter.Name); ODataCollectionStart odataCollectionStart = new ODataCollectionStart(); collectionWriter.WriteStart(odataCollectionStart); IEnumerator enumerator = ((ICollection)operationParameter.Value).GetEnumerator(); while (enumerator.MoveNext()) { Object collectionItem = enumerator.Current; if (collectionItem == null) { collectionWriter.WriteItem(null); continue; } IEdmType edmItemType = model.GetOrCreateEdmType(collectionItem.GetType()); Debug.Assert(edmItemType != null, "edmItemType != null"); switch (edmItemType.TypeKind) { case EdmTypeKind.Primitive: { object primitiveItemValue = ODataPropertyConverter.ConvertPrimitiveValueToRecognizedODataType(collectionItem, collectionItem.GetType()); collectionWriter.WriteItem(primitiveItemValue); break; } case EdmTypeKind.Enum: { ODataEnumValue enumTmp = this.propertyConverter.CreateODataEnumValue(model.GetClientTypeAnnotation(edmItemType).ElementType, collectionItem, false); collectionWriter.WriteItem(enumTmp); break; } default: // EdmTypeKind.Entity // EdmTypeKind.Row // EdmTypeKind.EntityReference throw new NotSupportedException(Strings.Serializer_InvalidCollectionParamterItemType(operationParameter.Name, edmItemType.TypeKind)); } } collectionWriter.WriteEnd(); collectionWriter.Flush(); } }