private static void VerifySortRestrictions(SortRestrictions sort)
        {
            Assert.NotNull(sort);

            Assert.NotNull(sort.Sortable);
            Assert.False(sort.Sortable.Value);

            Assert.NotNull(sort.AscendingOnlyProperties);
            Assert.Single(sort.AscendingOnlyProperties);
            Assert.Equal("abc", sort.AscendingOnlyProperties.First());

            Assert.NotNull(sort.DescendingOnlyProperties);
            Assert.Single(sort.DescendingOnlyProperties);
            Assert.Equal("rst", sort.DescendingOnlyProperties.First());

            Assert.NotNull(sort.NonSortableProperties);
            Assert.Single(sort.NonSortableProperties);
            Assert.Equal("Emails", sort.NonSortableProperties.First());

            Assert.True(sort.IsAscendingOnlyProperty("abc"));
            Assert.False(sort.IsAscendingOnlyProperty("rst"));

            Assert.False(sort.IsDescendingOnlyProperty("abc"));
            Assert.True(sort.IsDescendingOnlyProperty("rst"));

            Assert.False(sort.IsNonSortableProperty("abc"));
            Assert.True(sort.IsNonSortableProperty("Emails"));
        }
Beispiel #2
0
        /// <summary>
        /// Create $orderby parameter for the <see cref="IEdmEntitySet"/>.
        /// </summary>
        /// <param name="context">The OData context.</param>
        /// <param name="target">The Edm annotation target.</param>
        /// <returns>The created <see cref="OpenApiParameter"/> or null.</returns>
        public static OpenApiParameter CreateOrderBy(this ODataContext context, IEdmVocabularyAnnotatable target, IEdmEntityType entityType)
        {
            Utils.CheckArgumentNull(context, nameof(context));
            Utils.CheckArgumentNull(target, nameof(target));
            Utils.CheckArgumentNull(entityType, nameof(entityType));

            SortRestrictions sort = context.Model.GetSortRestrictions(target);

            if (sort != null && !sort.IsSortable)
            {
                return(null);
            }

            IList <IOpenApiAny> orderByItems = new List <IOpenApiAny>();

            foreach (var property in entityType.StructuralProperties())
            {
                if (sort != null && sort.IsNonSortableProperty(property.Name))
                {
                    continue;
                }

                bool isAscOnly = sort != null?sort.IsAscendingOnlyProperty(property.Name) : false;

                bool isDescOnly = sort != null?sort.IsDescendingOnlyProperty(property.Name) : false;

                if (isAscOnly || isDescOnly)
                {
                    if (isAscOnly)
                    {
                        orderByItems.Add(new OpenApiString(property.Name));
                    }
                    else
                    {
                        orderByItems.Add(new OpenApiString(property.Name + " desc"));
                    }
                }
                else
                {
                    orderByItems.Add(new OpenApiString(property.Name));
                    orderByItems.Add(new OpenApiString(property.Name + " desc"));
                }
            }

            return(new OpenApiParameter
            {
                Name = "$orderby",
                In = ParameterLocation.Query,
                Description = "Order items by property values",
                Schema = new OpenApiSchema
                {
                    // Remove array in favor of single instance for PowerApps
                    Type = "string",
                    Enum = orderByItems
                },
                Style = ParameterStyle.Simple
            });
        }