/// <summary>
        /// Validates a resource in an expanded link to make sure the entity types match.
        /// </summary>
        /// <param name="resourceType">The <see cref="IEdmEntityType"/> of the resource.</param>
        /// <param name="parentNavigationPropertyType">The type of the parent navigation property.</param>
        internal static void ValidateNestedResource(IEdmStructuredType resourceType, IEdmStructuredType parentNavigationPropertyType)
        {
            if (parentNavigationPropertyType == null)
            {
                return;
            }

            Debug.Assert(resourceType != null, "If we have a parent navigation property type we should also have a resource type.");

            // Make sure the entity types are compatible
            if (!parentNavigationPropertyType.IsAssignableFrom(resourceType))
            {
                throw new ODataException(Strings.WriterValidationUtils_NestedResourceTypeNotCompatibleWithParentPropertyType(resourceType.FullTypeName(), parentNavigationPropertyType.FullTypeName()));
            }
        }
        private static void VerifyComplexType(IEdmTypeReference expectedTypeReference, IEdmType payloadType, bool failIfNotRelated)
        {
            IEdmStructuredType thisType  = expectedTypeReference.AsStructured().StructuredDefinition();
            IEdmStructuredType otherType = (IEdmStructuredType)payloadType;

            if (!thisType.IsEquivalentTo(otherType))
            {
                if (thisType.IsAssignableFrom(otherType))
                {
                    throw new ODataException(Microsoft.Data.OData.Strings.ReaderValidationUtils_DerivedComplexTypesAreNotAllowed(thisType.ODataFullName(), otherType.ODataFullName()));
                }
                if (failIfNotRelated)
                {
                    throw new ODataException(Microsoft.Data.OData.Strings.ValidationUtils_IncompatibleType(otherType.ODataFullName(), thisType.ODataFullName()));
                }
            }
        }
Exemple #3
0
        internal static IEdmStructuredType GetCommonBaseType(this IEdmStructuredType firstType, IEdmStructuredType secondType)
        {
            IEdmStructuredType type;

            if (firstType.IsEquivalentTo(secondType))
            {
                return(firstType);
            }
            for (type = firstType; type != null; type = type.BaseType)
            {
                if (type.IsAssignableFrom(secondType))
                {
                    return(type);
                }
            }
            for (type = secondType; type != null; type = type.BaseType)
            {
                if (type.IsAssignableFrom(firstType))
                {
                    return(type);
                }
            }
            return(null);
        }
        /// <summary>
        /// Checks if the <paramref name="firstType"/> structured type and the <paramref name="secondType"/> structured type
        /// have a common base type.
        /// In other words, if <paramref name="secondType"/> is a subtype of <paramref name="firstType"/> or not.
        /// </summary>
        /// <param name="firstType">Type of the base type.</param>
        /// <param name="secondType">Type of the sub type.</param>
        /// <returns>The common base type or null if no common base type exists.</returns>
        internal static IEdmStructuredType GetCommonBaseType(this IEdmStructuredType firstType, IEdmStructuredType secondType)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(firstType != null, "firstType != null");
            Debug.Assert(secondType != null, "secondType != null");

            if (firstType.IsEquivalentTo(secondType))
            {
                return firstType;
            }

            IEdmStructuredType commonBaseType = firstType;
            while (commonBaseType != null)
            {
                if (commonBaseType.IsAssignableFrom(secondType))
                {
                    return commonBaseType;
                }

                commonBaseType = commonBaseType.BaseType;
            }

            commonBaseType = secondType;
            while (commonBaseType != null)
            {
                if (commonBaseType.IsAssignableFrom(firstType))
                {
                    return commonBaseType;
                }

                commonBaseType = commonBaseType.BaseType;
            }

            return null;
        }
 internal static IEdmStructuredType GetCommonBaseType(this IEdmStructuredType firstType, IEdmStructuredType secondType)
 {
     IEdmStructuredType type;
     if (firstType.IsEquivalentTo(secondType))
     {
         return firstType;
     }
     for (type = firstType; type != null; type = type.BaseType)
     {
         if (type.IsAssignableFrom(secondType))
         {
             return type;
         }
     }
     for (type = secondType; type != null; type = type.BaseType)
     {
         if (type.IsAssignableFrom(firstType))
         {
             return type;
         }
     }
     return null;
 }