internal static IEdmNavigationProperty ValidateNavigationLink( ODataNavigationLink navigationLink, IEdmEntityType declaringEntityType, ODataPayloadKind?expandedPayloadKind, bool bypassValidation = false) { Debug.Assert(navigationLink != null, "navigationLink != null"); Debug.Assert( !expandedPayloadKind.HasValue || expandedPayloadKind.Value == ODataPayloadKind.EntityReferenceLink || expandedPayloadKind.Value == ODataPayloadKind.Entry || expandedPayloadKind.Value == ODataPayloadKind.Feed, "If an expanded payload kind is specified it must be entry, feed or entity reference link."); if (bypassValidation) { return(declaringEntityType == null ? null : declaringEntityType.FindProperty(navigationLink.Name) as IEdmNavigationProperty); } // Navigation link must have a non-empty name if (string.IsNullOrEmpty(navigationLink.Name)) { throw new ODataException(Strings.ValidationUtils_LinkMustSpecifyName); } // If we write an entity reference link, don't validate the multiplicity of the IsCollection // property if it is 'false' (since we allow writing a singleton navigation link for // a collection navigation property in requests) nor the consistency of payload kind and metadata // (which is done separately in ODataWriterCore.CheckForNavigationLinkWithContent). bool isEntityReferenceLinkPayload = expandedPayloadKind == ODataPayloadKind.EntityReferenceLink; // true only if the expandedPayloadKind has a value and the value is 'Feed' bool isFeedPayload = expandedPayloadKind == ODataPayloadKind.Feed; // Make sure the IsCollection property agrees with the payload kind for entry and feed payloads Func <object, string> errorTemplate = null; if (!isEntityReferenceLinkPayload && navigationLink.IsCollection.HasValue && expandedPayloadKind.HasValue) { // For feed/entry make sure the IsCollection property is set correctly. if (isFeedPayload != navigationLink.IsCollection.Value) { errorTemplate = expandedPayloadKind.Value == ODataPayloadKind.Feed ? (Func <object, string>)Strings.WriterValidationUtils_ExpandedLinkIsCollectionFalseWithFeedContent : Strings.WriterValidationUtils_ExpandedLinkIsCollectionTrueWithEntryContent; } } IEdmNavigationProperty navigationProperty = null; if (errorTemplate == null && declaringEntityType != null) { navigationProperty = WriterValidationUtils.ValidateNavigationPropertyDefined(navigationLink.Name, declaringEntityType); Debug.Assert(navigationProperty != null, "If we have a declaring type we expect a non-null navigation property since open nav props are not allowed."); bool isCollectionType = navigationProperty.Type.TypeKind() == EdmTypeKind.Collection; // Make sure the IsCollection property agrees with the metadata type for entry and feed payloads if (navigationLink.IsCollection.HasValue && isCollectionType != navigationLink.IsCollection) { // Ignore the case where IsCollection is 'false' and we are writing an entity reference link // (see comment above) if (!(navigationLink.IsCollection == false && isEntityReferenceLinkPayload)) { errorTemplate = isCollectionType ? (Func <object, string>)Strings.WriterValidationUtils_ExpandedLinkIsCollectionFalseWithFeedMetadata : Strings.WriterValidationUtils_ExpandedLinkIsCollectionTrueWithEntryMetadata; } } // Make sure that the payload kind agrees with the metadata. // For entity reference links we check separately in ODataWriterCore.CheckForNavigationLinkWithContent. if (!isEntityReferenceLinkPayload && expandedPayloadKind.HasValue && isCollectionType != isFeedPayload) { errorTemplate = isCollectionType ? (Func <object, string>)Strings.WriterValidationUtils_ExpandedLinkWithEntryPayloadAndFeedMetadata : Strings.WriterValidationUtils_ExpandedLinkWithFeedPayloadAndEntryMetadata; } } if (errorTemplate != null) { string uri = navigationLink.Url == null ? "null" : UriUtils.UriToString(navigationLink.Url); throw new ODataException(errorTemplate(uri)); } return(navigationProperty); }
/// <summary> /// Validates that a navigation property with the specified name exists on a given entity type. /// The entity type can be null if no metadata is available. /// </summary> /// <param name="propertyName">The name of the property to validate.</param> /// <param name="owningEntityType">The owning entity type or null if no metadata is available.</param> /// <returns>The <see cref="IEdmProperty"/> instance representing the navigation property with name <paramref name="propertyName"/> /// or null if no metadata is available.</returns> public IEdmNavigationProperty ValidateNavigationPropertyDefined(string propertyName, IEdmEntityType owningEntityType) { return(WriterValidationUtils.ValidateNavigationPropertyDefined(propertyName, owningEntityType)); }