// This constructor is intended for unit testing only.
        internal OrderByQueryOption(string rawValue, ODataQueryContext context, IServiceProvider serviceProvider)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            if (String.IsNullOrEmpty(rawValue))
            {
                throw Error.ArgumentNullOrEmpty("rawValue");
            }

            _serviceProvider = serviceProvider;
            Context          = context;
            RawValue         = rawValue;

            Validator = new OrderByQueryValidator();
            // Validator = OrderByQueryValidator.GetOrderByQueryValidator(context); // TODO:
            _queryOptionParser = new ODataQueryOptionParser(
                context.Model,
                context.ElementType,
                context.NavigationSource,
                new Dictionary <string, string> {
                { "$orderby", rawValue }
            });
        }
示例#2
0
        internal OrderByQueryOption(string rawValue, ODataQueryContext context, string applyRaw)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (String.IsNullOrEmpty(rawValue))
            {
                throw Error.ArgumentNullOrEmpty("rawValue");
            }

            if (applyRaw == null)
            {
                throw Error.ArgumentNullOrEmpty("applyRaw");
            }

            Context            = context;
            RawValue           = rawValue;
            Validator          = OrderByQueryValidator.GetOrderByQueryValidator(context);
            _queryOptionParser = new ODataQueryOptionParser(
                context.Model,
                context.ElementType,
                context.NavigationSource,
                new Dictionary <string, string> {
                { "$orderby", rawValue }, { "$apply", applyRaw }
            });
            _queryOptionParser.ParseApply();
        }
        /// <summary>
        /// Initialize a new instance of <see cref="OrderByQueryOption"/> based on the raw $orderby value and
        /// an EdmModel from <see cref="ODataQueryContext"/>.
        /// </summary>
        /// <param name="rawValue">The raw value for $orderby query. It can be null or empty.</param>
        /// <param name="context">The <see cref="ODataQueryContext"/> which contains the <see cref="IEdmModel"/> and some type information</param>
        /// <param name="queryOptionParser">The <see cref="ODataQueryOptionParser"/> which is used to parse the query option.</param>
        /// <param name="serviceProvider"></param>
        public OrderByQueryOption(string rawValue, ODataQueryContext context, ODataQueryOptionParser queryOptionParser,
                                  IServiceProvider serviceProvider)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (String.IsNullOrEmpty(rawValue))
            {
                throw Error.ArgumentNullOrEmpty("rawValue");
            }

            if (queryOptionParser == null)
            {
                throw Error.ArgumentNull("queryOptionParser");
            }

            if (serviceProvider == null)
            {
                throw Error.ArgumentNull("serviceProvider");
            }

            Context   = context;
            RawValue  = rawValue;
            Validator = new OrderByQueryValidator();
            // Validator = OrderByQueryValidator.GetOrderByQueryValidator(context); // TODO
            _queryOptionParser = queryOptionParser;
            _serviceProvider   = serviceProvider;
        }
        public void Validate_DoesntThrowIfTheLeafOfThePathIsWithinTheAllowedProperties()
        {
            // Arrange
            IEdmModel               model    = GetEdmModel();
            IEdmEntityType          edmType  = model.SchemaElements.OfType <IEdmEntityType>().Single(t => t.Name == "LimitedEntity");
            ODataQueryContext       context  = new ODataQueryContext(model, edmType);
            OrderByQueryOption      option   = new OrderByQueryOption("ComplexProperty/Value", context);
            ODataValidationSettings settings = new ODataValidationSettings();

            settings.AllowedOrderByProperties.Add("Value");

            // Act & Assert
            OrderByQueryValidator validator = OrderByQueryValidator.GetOrderByQueryValidator(context);

            ExceptionAssert.DoesNotThrow(() => validator.Validate(option, settings));
        }
示例#5
0
        /// <summary>
        /// Initialize a new instance of <see cref="OrderByQueryOption"/> based on the raw $orderby value and
        /// an EdmModel from <see cref="ODataQueryContext"/>.
        /// </summary>
        /// <param name="rawValue">The raw value for $orderby query. It can be null or empty.</param>
        /// <param name="context">The <see cref="ODataQueryContext"/> which contains the <see cref="IEdmModel"/> and some type information</param>
        public OrderByQueryOption(string rawValue, ODataQueryContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (String.IsNullOrEmpty(rawValue))
            {
                throw Error.ArgumentNullOrEmpty("rawValue");
            }

            Context   = context;
            RawValue  = rawValue;
            Validator = new OrderByQueryValidator();
        }
示例#6
0
        public void ValidateOrderByQueryValidator_ThrowsIfTryingToValidateALimitedProperty(string query, string edmTypeName, string message)
        {
            // Arrange
            IEdmModel         model   = GetEdmModel();
            IEdmEntityType    edmType = model.SchemaElements.OfType <IEdmEntityType>().Single(t => t.Name == edmTypeName);
            ODataQueryContext context = new ODataQueryContext(model, edmType);

            context.DefaultQuerySettings.EnableOrderBy = true;
            OrderByQueryOption      option   = new OrderByQueryOption(query, context);
            ODataValidationSettings settings = new ODataValidationSettings();

            // Act & Assert
            OrderByQueryValidator validator = new OrderByQueryValidator();

            ExceptionAssert.Throws <ODataException>(() => validator.Validate(option, settings), message);
        }
        public void Validate_ThrowsIfTheLeafOfThePathIsntWithinTheAllowedProperties()
        {
            // Arrange
            IEdmModel               model    = GetEdmModel();
            IEdmEntityType          edmType  = model.SchemaElements.OfType <IEdmEntityType>().Single(t => t.Name == "LimitedEntity");
            ODataQueryContext       context  = new ODataQueryContext(model, edmType);
            OrderByQueryOption      option   = new OrderByQueryOption("ComplexProperty/Value", context);
            ODataValidationSettings settings = new ODataValidationSettings();

            settings.AllowedOrderByProperties.Add("NotSortableProperty");

            // Act & Assert
            OrderByQueryValidator validator = OrderByQueryValidator.GetOrderByQueryValidator(context);

            ExceptionAssert.Throws <ODataException>(() =>
                                                    validator.Validate(option, settings),
                                                    "Order by 'Value' is not allowed. To allow it, set the 'AllowedOrderByProperties' property on EnableQueryAttribute or QueryValidationSettings.");
        }
        /// <summary>
        /// Apply $orderby parameter to query.
        /// </summary>
        /// <param name="query">
        /// The OData aware query.
        /// </param>
        /// <param name="orderbyText">
        /// The $orderby parameter text.
        /// </param>
        /// <param name="entitySetName">
        /// The entity set name.
        /// </param>
        /// <typeparam name="T">
        /// The query type param
        /// </typeparam>
        /// <returns>
        /// The <see cref="ODataQuery{T}"/> query with applied order by parameter.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Argument Null Exception
        /// </exception>
        public static ODataQueryOrdered <T> OrderBy <T>(this ODataQuery <T> query, string orderbyText, string entitySetName = null)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (orderbyText == null)
            {
                throw new ArgumentNullException(nameof(orderbyText));
            }

            IEdmModel edmModel = query.EdmModel;

            ODataQueryOptionParser queryOptionParser = GetParser(
                query,
                entitySetName,
                new Dictionary <string, string> {
                { "$orderby", orderbyText }
            });

            ODataSettings settings = query.ServiceProvider.GetRequiredService <ODataSettings>();

            var orderByClause = queryOptionParser.ParseOrderBy();

            orderByClause = TranslateParameterAlias(orderByClause, queryOptionParser);

            ICollection <OrderByNode> nodes = OrderByNode.CreateCollection(orderByClause);

            var validator = new OrderByQueryValidator(settings.DefaultQuerySettings);

            validator.Validate(nodes, settings.ValidationSettings, edmModel);

            IOrderedQueryable <T> result = (IOrderedQueryable <T>)OrderByBinder.OrderApplyToCore(query, settings.QuerySettings, nodes, edmModel);

            return(new ODataQueryOrdered <T>(result, query.ServiceProvider));
        }
 public OrderByQueryValidatorTest()
 {
     _context   = ValidationTestHelper.CreateCustomerContext();
     _validator = new OrderByQueryValidator(_context.DefaultQuerySettings);
 }
示例#10
0
 public OrderByQueryValidatorTest()
 {
     _context   = ValidationTestHelper.CreateCustomerContext();
     _validator = new OrderByQueryValidator();
 }