/// <summary> /// Resolves the specified Edm Data Type from the payload model. /// </summary> /// <param name="fullTypeName">The full name of the type to resolve.</param> /// <returns>The EdmDataType that corresponds to the type name.</returns> private IEdmTypeReference ResolvePropertyEdmDataType(string fullTypeName) { if (string.IsNullOrEmpty(fullTypeName)) { return(null); } bool propertyIsCollection = false; if (fullTypeName.StartsWith(EdmConstants.CollectionTypeQualifier)) { fullTypeName = EntityModelUtils.GetCollectionItemTypeName(fullTypeName); propertyIsCollection = true; } string namespaceName; string typeName; DataTypeUtils.ParseFullTypeName(fullTypeName, out typeName, out namespaceName); IEdmTypeReference edmDataType = null; var complexType = this.testDescriptor.PayloadEdmModel.FindType(fullTypeName); if (complexType != null) { edmDataType = complexType.ToTypeReference(); } else { var primitiveType = EdmCoreModel.Instance.GetPrimitive(EdmCoreModel.Instance.GetPrimitiveTypeKind(typeName), true); ExceptionUtilities.CheckObjectNotNull(primitiveType, "Failed to resolve type: " + fullTypeName); edmDataType = primitiveType; } if (propertyIsCollection) { edmDataType = EdmCoreModel.GetCollection(edmDataType); } return(edmDataType); }
/// <summary> /// Visits a collection item. /// </summary> /// <param name="collectionValue">The collection to visit.</param> protected override ODataPayloadElement VisitCollectionValue(ODataCollectionValue collectionValue) { if (collectionValue == null) { return(new PrimitiveMultiValue(null, true)); } bool?isPrimitiveCollection = null; // Try to parse the type name and if the item type name is a primitive EDM type, then this is a primitive collection. string typeName = collectionValue.TypeName; if (typeName != null) { string itemTypeName = EntityModelUtils.GetCollectionItemTypeName(typeName); isPrimitiveCollection = itemTypeName != null && EntityModelUtils.GetPrimitiveEdmType(itemTypeName) != null; } List <object> items = new List <object>(); foreach (object item in collectionValue.Items) { if (!isPrimitiveCollection.HasValue) { ODataComplexValue complexItemValue = item as ODataComplexValue; // If the first item is a complex value, then the collection is of complex kind. // Note that if the first item is null, we assume primitive collection, since we can't really decide (and it's an invalid thing anyway) isPrimitiveCollection = complexItemValue == null; } if (isPrimitiveCollection.Value) { items.Add((PrimitiveValue)this.Visit(item)); } else { ODataComplexValue complexItemValue = item as ODataComplexValue; ExceptionUtilities.Assert( item == null || complexItemValue != null, "The collection was determined to be of complex values but one of its items is not an ODataComplexValue."); items.Add((ComplexInstance)this.Visit(complexItemValue)); } } if (!isPrimitiveCollection.HasValue) { // If we could not tell until now (possible only if there was no type name and no items) // assume primitive collection. isPrimitiveCollection = true; } if (isPrimitiveCollection == true) { PrimitiveMultiValue primitiveCollection = new PrimitiveMultiValue(typeName, false); foreach (object item in items) { primitiveCollection.Add((PrimitiveValue)item); } this.ConvertSerializationTypeNameAnnotation(collectionValue, primitiveCollection); return(primitiveCollection); } else { ComplexMultiValue complexCollection = new ComplexMultiValue(typeName, false); foreach (object item in items) { complexCollection.Add((ComplexInstance)item); } this.ConvertSerializationTypeNameAnnotation(collectionValue, complexCollection); return(complexCollection); } }