private static void VerifySortRestrictions(SortRestrictionsType 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"));
        }
        public void UnknownAnnotatableTargetReturnsDefaultSortRestrictionsValues()
        {
            // Arrange & Act
            EdmEntityType entityType = new EdmEntityType("NS", "Entity");

            //  Act
            SortRestrictionsType sort = EdmCoreModel.Instance.GetRecord <SortRestrictionsType>(entityType);

            // Assert
            Assert.Null(sort);
        }
        public void TargetOnEntitySetReturnsCorrectSortRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location)
        {
            // Arrange
            const string template = @"
                <Annotations Target=""NS.Default/Calendars"">
                  {0}
                </Annotations>";

            IEdmModel model = GetEdmModel(template, location);

            Assert.NotNull(model); // guard

            IEdmEntitySet calendars = model.EntityContainer.FindEntitySet("Calendars");

            Assert.NotNull(calendars); // guard

            // Act
            SortRestrictionsType sort = model.GetRecord <SortRestrictionsType>(calendars);

            // Assert
            VerifySortRestrictions(sort);
        }
        public void TargetOnEntityTypeReturnsCorrectSortRestrictionsValue(EdmVocabularyAnnotationSerializationLocation location)
        {
            // Arrange
            const string template = @"
                <Annotations Target=""NS.Calendar"">
                  {0}
                </Annotations>";

            IEdmModel model = GetEdmModel(template, location);

            Assert.NotNull(model); // guard

            IEdmEntityType calendar = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Calendar");

            Assert.NotNull(calendar); // guard

            // Act
            SortRestrictionsType sort = model.GetRecord <SortRestrictionsType>(calendar);

            // Assert
            VerifySortRestrictions(sort);
        }
예제 #5
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>
        /// <param name="entityType">The Edm Entity type.</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));

            SortRestrictionsType sort = context.Model.GetRecord <SortRestrictionsType>(target, CapabilitiesConstants.SortRestrictions);

            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
                {
                    Type = "array",
                    UniqueItems = true,
                    Items = new OpenApiSchema
                    {
                        Type = "string",
                        Enum = orderByItems
                    }
                },
                Style = ParameterStyle.Form
            });
        }