/// <summary> /// Resolves an entity set with an optional type cast and updates the parse result. /// </summary> /// <param name="entitySet">The entity set to resolve the type cast against.</param> /// <param name="typeCast">The optional type cast.</param> /// <param name="readerBehavior">Reader behavior if the caller is a reader, null if no reader behavior is available.</param> /// <param name="version">The version of the payload being read.</param> /// <param name="entitySetElementType">The type of the given entity set.</param> /// <returns>The resolved entity type.</returns> private IEdmEntityType ResolveTypeCast(IEdmEntitySet entitySet, string typeCast, ODataReaderBehavior readerBehavior, ODataVersion version, IEdmEntityType entitySetElementType) { Debug.Assert(entitySet != null, "entitySet != null"); IEdmEntityType entityType = entitySetElementType; // Parse the type cast if it exists if (!string.IsNullOrEmpty(typeCast)) { EdmTypeKind typeKind; entityType = MetadataUtils.ResolveTypeNameForRead(this.model, /*expectedType*/ null, typeCast, readerBehavior, version, out typeKind) as IEdmEntityType; if (entityType == null) { throw new ODataException(ODataErrorStrings.ODataJsonLightMetadataUriParser_InvalidEntityTypeInTypeCast(UriUtilsCommon.UriToString(this.parseResult.MetadataUri), typeCast)); } // Validate that the entity type is assignable to the base type of the set if (!entitySetElementType.IsAssignableFrom(entityType)) { throw new ODataException(ODataErrorStrings.ODataJsonLightMetadataUriParser_IncompatibleEntityTypeInTypeCast(UriUtilsCommon.UriToString(this.parseResult.MetadataUri), typeCast, entitySetElementType.FullName(), entitySet.FullName())); } } return(entityType); }
private bool ShouldExpandNavigationProperty(IEdmNavigationProperty navigationProperty, ODataPath currentPath) { Debug.Assert(navigationProperty != null); Debug.Assert(currentPath != null); // not expand for the non-containment. if (!navigationProperty.ContainsTarget) { return(false); } // check the type is visited before, if visited, not expand it. IEdmEntityType navEntityType = navigationProperty.ToEntityType(); foreach (ODataSegment segment in currentPath) { if (navEntityType.IsAssignableFrom(segment.EntityType)) { return(false); } } // check whether the navigation type used to define a navigation source. // if so, not expand it. return(!_allNavigationSources.ContainsKey(navEntityType)); }
public void ValidateEntityTypeAssignableToEdmEntityType() { IEdmEntityType baseType = EdmCoreModel.Instance.GetEntityType(); EdmEntityType entityType = new EdmEntityType("NS", "Entity"); Assert.True(baseType.IsAssignableFrom(entityType)); }
internal static void ValidateEntryInExpandedLink(IEdmEntityType entryEntityType, IEdmType parentNavigationPropertyType) { if (parentNavigationPropertyType != null) { IEdmEntityType baseType = (parentNavigationPropertyType.TypeKind == EdmTypeKind.Collection) ? ((IEdmEntityType)((IEdmCollectionType)parentNavigationPropertyType).ElementType.Definition) : ((IEdmEntityType)parentNavigationPropertyType); if (!baseType.IsAssignableFrom(entryEntityType)) { throw new ODataException(Microsoft.Data.OData.Strings.WriterValidationUtils_EntryTypeInExpandedLinkNotCompatibleWithNavigationPropertyType(entryEntityType.ODataFullName(), baseType.ODataFullName())); } } }
/// <summary> /// Validates an entry in an expanded link to make sure the entity types match. /// </summary> /// <param name="entryEntityType">The <see cref="IEdmEntityType"/> of the entry.</param> /// <param name="parentNavigationPropertyType">The type of the parent navigation property.</param> internal static void ValidateEntryInExpandedLink(IEdmEntityType entryEntityType, IEdmEntityType parentNavigationPropertyType) { if (parentNavigationPropertyType == null) { return; } Debug.Assert(entryEntityType != null, "If we have a parent navigation property type we should also have an entry type."); // Make sure the entity types are compatible if (!parentNavigationPropertyType.IsAssignableFrom(entryEntityType)) { throw new ODataException(Strings.WriterValidationUtils_EntryTypeInExpandedLinkNotCompatibleWithNavigationPropertyType(entryEntityType.ODataFullName(), parentNavigationPropertyType.ODataFullName())); } }
/// <summary> /// Validates an entry in an expanded link to make sure the entity types match. /// </summary> /// <param name="entryEntityType">The <see cref="IEdmEntityType"/> of the entry.</param> /// <param name="parentNavigationPropertyType">The type of the parent navigation property.</param> internal static void ValidateEntryInExpandedLink(IEdmEntityType entryEntityType, IEdmType parentNavigationPropertyType) { DebugUtils.CheckNoExternalCallers(); if (parentNavigationPropertyType == null) { return; } Debug.Assert(entryEntityType != null, "If we have a parent navigation property type we should also have an entry type."); bool navPropIsCollection = parentNavigationPropertyType.TypeKind == EdmTypeKind.Collection; IEdmEntityType parentNavigationPropertyEntityType = (IEdmEntityType)(navPropIsCollection ? ((IEdmCollectionType)parentNavigationPropertyType).ElementType.Definition : parentNavigationPropertyType); // Make sure the entity types are compatible if (!parentNavigationPropertyEntityType.IsAssignableFrom(entryEntityType)) { throw new ODataException(Strings.WriterValidationUtils_EntryTypeInExpandedLinkNotCompatibleWithNavigationPropertyType(entryEntityType.ODataFullName(), parentNavigationPropertyEntityType.ODataFullName())); } }
/// <summary> /// Returns the fully qualified name of <paramref name="entityType"/> if it is a derived type of the <paramref name="entitySet"/>; /// returns null if <paramref name="entityType"/> is the root type of <paramref name="entitySet"/>. /// </summary> /// <param name="entitySet">The entity set in question.</param> /// <param name="entityType">The eneity type in question.</param> /// <returns> /// Returns the fully qualified name of <paramref name="entityType"/> if it is a derived type of the <paramref name="entitySet"/>; /// returns null if <paramref name="entityType"/> is the root type of <paramref name="entitySet"/>. /// </returns> private static string GetTypecast(IEdmEntitySet entitySet, IEdmEntityType entityType) { DebugUtils.CheckNoExternalCallers(); if (entitySet == null || entityType == null) { return(null); } // The client type resolver is not necessary for writes, ResolveEntitySetElementType() will return entitySet.ElementType when model is null // and we don't need to look up the type in the model. IEdmEntityType entitySetElementType = EdmTypeWriterResolver.Instance.GetElementType(entitySet); if (entitySetElementType.IsEquivalentTo(entityType)) { return(null); } if (!entitySetElementType.IsAssignableFrom(entityType)) { throw new ODataException(OData.Strings.ODataJsonLightMetadataUriBuilder_ValidateDerivedType(entitySetElementType.FullName(), entityType.FullName())); } return(entityType.ODataFullName()); }
/// <summary> /// Validates an entry in an expanded link to make sure the entity types match. /// </summary> /// <param name="entryEntityType">The <see cref="IEdmEntityType"/> of the entry.</param> /// <param name="parentNavigationPropertyType">The type of the parent navigation property.</param> internal static void ValidateEntryInExpandedLink(IEdmEntityType entryEntityType, IEdmEntityType parentNavigationPropertyType) { if (parentNavigationPropertyType == null) { return; } Debug.Assert(entryEntityType != null, "If we have a parent navigation property type we should also have an entry type."); // Make sure the entity types are compatible if (!parentNavigationPropertyType.IsAssignableFrom(entryEntityType)) { throw new ODataException(Strings.WriterValidationUtils_EntryTypeInExpandedLinkNotCompatibleWithNavigationPropertyType(entryEntityType.ODataFullName(), parentNavigationPropertyType.ODataFullName())); } }