コード例 #1
0
        /// <summary>
        /// Validates a collection item that was read to make sure it is valid (i.e., has the correct
        /// type name and type kind) with respect to the other items in the collection.
        /// </summary>
        /// <param name="collectionItemTypeName">The type name of the item from the payload.</param>
        /// <param name="collectionItemTypeKind">The type kind of the item from the payload.</param>
        internal void ValidateCollectionItem(string collectionItemTypeName, EdmTypeKind collectionItemTypeKind)
        {
            DebugUtils.CheckNoExternalCallers();

            // Only primitive and complex values are allowed in collections
            if (collectionItemTypeKind != EdmTypeKind.Primitive && collectionItemTypeKind != EdmTypeKind.Complex)
            {
                throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_InvalidItemTypeKind(collectionItemTypeKind));
            }

            if (this.itemTypeDerivedFromCollectionValue)
            {
                Debug.Assert(this.itemTypeName != null, "this.itemType != null");

                // If the collection has a type name assign missing item type names from it.
                collectionItemTypeName = collectionItemTypeName ?? this.itemTypeName;

                // If we have a type name from the collection, make sure the type names of all items match
                this.ValidateCollectionItemTypeNameAndKind(collectionItemTypeName, collectionItemTypeKind);
            }
            else
            {
                // If we don't have a type name from the collection, store the type name and type kind of the first non-null item.
                if (this.itemTypeKind == EdmTypeKind.None)
                {
                    // Compute the kind from the specified type name if available.
                    this.itemTypeKind = collectionItemTypeName == null
                        ? collectionItemTypeKind
                        : ComputeExpectedTypeKind(collectionItemTypeName, out this.primitiveItemType);

                    // If no payload type name is specified either default to Edm.String (for primitive type kinds) or leave the type name
                    // null (for complex items without type name)
                    if (collectionItemTypeName == null)
                    {
                        this.itemTypeKind = collectionItemTypeKind;
                        if (this.itemTypeKind == EdmTypeKind.Primitive)
                        {
                            this.itemTypeName      = Metadata.EdmConstants.EdmStringTypeName;
                            this.primitiveItemType = EdmCoreModel.Instance.GetString(/*isNullable*/ false).PrimitiveDefinition();
                        }
                        else
                        {
                            this.itemTypeName      = null;
                            this.primitiveItemType = null;
                        }
                    }
                    else
                    {
                        this.itemTypeKind = ComputeExpectedTypeKind(collectionItemTypeName, out this.primitiveItemType);
                        this.itemTypeName = collectionItemTypeName;
                    }
                }

                if (collectionItemTypeName == null && collectionItemTypeKind == EdmTypeKind.Primitive)
                {
                    // Default to Edm.String if no payload type is specified and the type kind is 'Primitive'
                    collectionItemTypeName = Metadata.EdmConstants.EdmStringTypeName;
                }

                // Validate the expected and actual type names and type kinds.
                // Note that we compute the expected type kind from the expected type name and thus the payload
                // type kind (passed to this method) might be different from the computed expected type kind.
                this.ValidateCollectionItemTypeNameAndKind(collectionItemTypeName, collectionItemTypeKind);
            }
        }