/// <summary>
        /// Annotates the given payload based on the metadata in the given uri
        /// </summary>
        /// <param name="rootElement">The payload to annotate with metadata information</param>
        /// <param name="uri">The uri that corresponds to the given payload</param>
        public void ResolveMetadata(ODataPayloadElement rootElement, ODataUri uri)
        {
            ExceptionUtilities.CheckArgumentNotNull(rootElement, "rootElement");
            ExceptionUtilities.CheckArgumentNotNull(uri, "uri");

            this.InitializeMetadataStack(uri);
            this.InitialStackSize = this.MetadataStack.Count;

            rootElement.Add(new ExpectedPayloadElementTypeAnnotation() { ExpectedType = uri.GetExpectedPayloadType() });

            // if the uri did not contain any metadata, do nothing
            if (this.InitialStackSize > 0)
            {
                // if this is the result of service operation or action, the root element needs to have the function itself and its return type
                var serviceOperation = this.MetadataStack.OfType<Function>().FirstOrDefault();
                if (serviceOperation != null)
                {
                    rootElement.AddAnnotationIfNotExist(new FunctionAnnotation() { Function = serviceOperation });
                    rootElement.AddAnnotationIfNotExist(new DataTypeAnnotation() { DataType = serviceOperation.ReturnType });
                }

                this.Recurse(rootElement);
            }
        }
示例#2
0
        /// <summary>
        /// Builds the ODataPayloadContext context from OODataUri.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <returns>A OData Payload Context</returns>
        public static ODataPayloadContext BuildODataPayloadContextFromODataUri(ODataUri uri)
        {
            var payloadContext = new ODataPayloadContext();

            payloadContext.PayloadUri          = uri;
            payloadContext.ExpectedPayloadKind = uri.GetExpectedPayloadType();

            switch (payloadContext.ExpectedPayloadKind)
            {
            case ODataPayloadElementType.EntitySetInstance:
            case ODataPayloadElementType.EntityInstance:
                SetExpectedEntitySetAndTypeFromUri(payloadContext, uri);
                break;

            case ODataPayloadElementType.PrimitiveProperty:
            case ODataPayloadElementType.PrimitiveMultiValueProperty:
            case ODataPayloadElementType.ComplexProperty:
            case ODataPayloadElementType.ComplexMultiValueProperty:
            case ODataPayloadElementType.EmptyCollectionProperty:
                var propertySegment = uri.Segments.OfType <PropertySegment>().LastOrDefault();
                if (propertySegment != null)
                {
                    payloadContext.ExpectedMemberProperty = propertySegment.Property;
                    SetExpectedEntitySetAndTypeFromUri(payloadContext, uri);

                    var parentPropertySegment = uri.Segments.GetSecondToLastOrDefault <PropertySegment>();
                    if (parentPropertySegment != null)
                    {
                        var collectionDataType = parentPropertySegment.Property.PropertyType as CollectionDataType;
                        if (collectionDataType != null)
                        {
                            var elementComplexDataType = collectionDataType.ElementDataType as ComplexDataType;
                            ExceptionUtilities.CheckObjectNotNull(elementComplexDataType, "Expected non complex DataType");
                            payloadContext.ExpectedPropertyDeclaringParentType = elementComplexDataType.Definition;
                        }
                        else
                        {
                            var complexDataType = parentPropertySegment.Property.PropertyType as ComplexDataType;
                            ExceptionUtilities.CheckObjectNotNull(complexDataType, "Expected non null complex DataType");
                            payloadContext.ExpectedPropertyDeclaringParentType = complexDataType.Definition;
                        }
                    }
                    else
                    {
                        payloadContext.ExpectedPropertyDeclaringParentType = payloadContext.ExpectedEntityType;
                    }
                }
                else
                {
                    ExceptionUtilities.Assert(
                        uri.IsFunction() || uri.IsServiceOperation() || uri.IsAction(),
                        "Property uri contains no property segments and is not a function/service operation/action.");
                    var functionSegment = uri.Segments.Last() as FunctionSegment ?? uri.Segments[uri.Segments.Count - 2] as FunctionSegment;
                    ExceptionUtilities.CheckObjectNotNull(functionSegment, "Failed to find function segment in finction/service operation uri");
                    payloadContext.ExpectedFunction = functionSegment.Function;
                }

                break;

            case ODataPayloadElementType.DeferredLink:
            case ODataPayloadElementType.LinkCollection:
                var navigationSegment = uri.Segments.OfType <NavigationSegment>().Last();
                ExceptionUtilities.CheckObjectNotNull(navigationSegment, "No navigation segments found for expected navigation property uri");
                payloadContext.ExpectedNavigationProperty = navigationSegment.NavigationProperty;
                SetExpectedEntitySetAndTypeFromUri(payloadContext, uri);
                break;
            }

            return(payloadContext);
        }
        internal static bool ResponseMightIncludeNextLink(ODataUri uri)
        {
            var payloadsWithNextLinks = new[] { ODataPayloadElementType.EntitySetInstance, ODataPayloadElementType.LinkCollection };
            var expectedPayloadType = uri.GetExpectedPayloadType();
            if (!payloadsWithNextLinks.Contains(expectedPayloadType))
            {
                return false;
            }

            EntitySet expectedSet;
            ExceptionUtilities.Assert(uri.TryGetExpectedEntitySet(out expectedSet), "Could not get expected entity set");

            var pageSize = expectedSet.GetEffectivePageSize();
            if (!pageSize.HasValue)
            {
                return false;
            }

            // if the value of $top is less than 1 page size, no next link will be included
            return !uri.Top.HasValue || uri.Top.Value > pageSize.Value;
        }
示例#4
0
        /// <summary>
        /// Builds the ODataPayloadContext context from OODataUri.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <returns>A OData Payload Context</returns>
        public static ODataPayloadContext BuildODataPayloadContextFromODataUri(ODataUri uri)
        {
            var payloadContext = new ODataPayloadContext();
            payloadContext.PayloadUri = uri;
            payloadContext.ExpectedPayloadKind = uri.GetExpectedPayloadType();

            switch (payloadContext.ExpectedPayloadKind)
            {
                case ODataPayloadElementType.EntitySetInstance:
                case ODataPayloadElementType.EntityInstance:
                    SetExpectedEntitySetAndTypeFromUri(payloadContext, uri);
                    break;

                case ODataPayloadElementType.PrimitiveProperty:
                case ODataPayloadElementType.PrimitiveMultiValueProperty:
                case ODataPayloadElementType.ComplexProperty:
                case ODataPayloadElementType.ComplexMultiValueProperty:
                case ODataPayloadElementType.EmptyCollectionProperty:
                    var propertySegment = uri.Segments.OfType<PropertySegment>().LastOrDefault();
                    if (propertySegment != null)
                    {
                        payloadContext.ExpectedMemberProperty = propertySegment.Property;
                        SetExpectedEntitySetAndTypeFromUri(payloadContext, uri);

                        var parentPropertySegment = uri.Segments.GetSecondToLastOrDefault<PropertySegment>();
                        if (parentPropertySegment != null)
                        {
                            var collectionDataType = parentPropertySegment.Property.PropertyType as CollectionDataType;
                            if (collectionDataType != null)
                            {
                                var elementComplexDataType = collectionDataType.ElementDataType as ComplexDataType;
                                ExceptionUtilities.CheckObjectNotNull(elementComplexDataType, "Expected non complex DataType");
                                payloadContext.ExpectedPropertyDeclaringParentType = elementComplexDataType.Definition;
                            }
                            else
                            {
                                var complexDataType = parentPropertySegment.Property.PropertyType as ComplexDataType;
                                ExceptionUtilities.CheckObjectNotNull(complexDataType, "Expected non null complex DataType");
                                payloadContext.ExpectedPropertyDeclaringParentType = complexDataType.Definition;
                            }
                        }
                        else
                        {
                            payloadContext.ExpectedPropertyDeclaringParentType = payloadContext.ExpectedEntityType;
                        }
                    }
                    else
                    {
                        ExceptionUtilities.Assert(
                            uri.IsFunction() || uri.IsServiceOperation() || uri.IsAction(),
                            "Property uri contains no property segments and is not a function/service operation/action.");
                        var functionSegment = uri.Segments.Last() as FunctionSegment ?? uri.Segments[uri.Segments.Count - 2] as FunctionSegment;
                        ExceptionUtilities.CheckObjectNotNull(functionSegment, "Failed to find function segment in finction/service operation uri");
                        payloadContext.ExpectedFunction = functionSegment.Function;
                    }

                    break;

                case ODataPayloadElementType.DeferredLink:
                case ODataPayloadElementType.LinkCollection:
                    var navigationSegment = uri.Segments.OfType<NavigationSegment>().Last();
                    ExceptionUtilities.CheckObjectNotNull(navigationSegment, "No navigation segments found for expected navigation property uri");
                    payloadContext.ExpectedNavigationProperty = navigationSegment.NavigationProperty;
                    SetExpectedEntitySetAndTypeFromUri(payloadContext, uri);
                    break;
            }

            return payloadContext;
        }