コード例 #1
0
 public void Initialize(HttpContext httpContext, ILogger <OpenApiQueryOptions> logger, ModelStateDictionary modelStateDictionary)
 {
     if (httpContext.Request.Query.TryGetValue("$filter", out var values))
     {
         if (values.Count == 1)
         {
             RawValue = values[0];
             var binder = httpContext.RequestServices.GetRequiredService <IOpenApiTypeHandler>();
             var parser = new QueryExpressionParser(values[0], binder);
             try
             {
                 parser.PushThis(Parameter);
                 FilterClause = parser.CommonExpr();
                 parser.PopThis();
             }
             catch (Exception e)
             {
                 logger.LogError(e, "Failed to parse filter");
                 modelStateDictionary.TryAddModelException("$filter", e);
             }
         }
         else
         {
             modelStateDictionary.TryAddModelError("$filter", "Only one $filter can be specified per request");
         }
     }
 }
コード例 #2
0
        public void Initialize(
            HttpContext httpContext,
            ILogger <OpenApiQueryOptions> logger,
            ModelStateDictionary modelState)
        {
            if (httpContext.Request.Query.TryGetValues(QueryOptionKeys.FilterKeys, out var values))
            {
                using var enumerator = values.GetEnumerator();
                if (enumerator.MoveNext())
                {
                    RawValue = enumerator.Current;
                    var binder = httpContext.RequestServices.GetRequiredService <IOpenApiTypeHandler>();
                    var parser = new QueryExpressionParser(enumerator.Current, binder);
                    try
                    {
                        parser.PushThis(Parameter);
                        FilterClause = parser.CommonExpr();
                        parser.PopThis();
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e, "Failed to parse filter");
                        modelState.TryAddModelException(QueryOptionKeys.FilterKeys.First(), e);
                    }

                    if (enumerator.MoveNext())
                    {
                        modelState.TryAddModelError(QueryOptionKeys.FilterKeys.First(),
                                                    "Multiple filter clauses found, only one can be specified.");
                    }
                }
            }
        }
コード例 #3
0
        public void TestDateTimeNow()
        {
            var parser = new QueryExpressionParser("now()", new DefaultOpenApiTypeHandler());
            var expr   = parser.CommonExpr();

            Assert.AreEqual(typeof(DateTimeOffset), expr.Type);
        }
コード例 #4
0
        private void TestParse_DateTimeOffset(string str, DateTimeOffset expected)
        {
            var parser = new QueryExpressionParser(str, new DefaultOpenApiTypeHandler());

            parser.PushThis(Expression.Constant(this));
            var expr = parser.CommonExpr();

            parser.PopThis();
            Assert.IsInstanceOfType(((ConstantExpression)expr).Value, typeof(DateTimeOffset));
            Assert.AreEqual(expected, (DateTimeOffset)((ConstantExpression)expr).Value);
        }
コード例 #5
0
 internal void Initialize(QueryExpressionParser parser, ModelStateDictionary modelStateDictionary)
 {
     try
     {
         parser.PushThis(Parameter);
         FilterClause = parser.CommonExpr();
         parser.PopThis();
     }
     catch (Exception e)
     {
         modelStateDictionary.TryAddModelException("$filter", e);
     }
 }
コード例 #6
0
        protected void ExpressionTest(string actualQuery, string expectedLinq, Type type, bool tolerantTypeCheck = false)
        {
            var parser = new QueryExpressionParser(actualQuery, new DefaultOpenApiTypeHandler());
            var expr   = parser.CommonExpr();

            if (tolerantTypeCheck)
            {
                Assert.IsTrue(type.IsAssignableFrom(expr.Type), "type.IsAssignableFrom(expr.Type)");
            }
            else
            {
                Assert.AreEqual(type, expr.Type);
            }
            Assert.AreEqual(expectedLinq, Convert.ToString(expr, CultureInfo.InvariantCulture));
        }
コード例 #7
0
            private void OrderByItemList()
            {
                while (true)
                {
                    var expression = _parser.CommonExpr();
                    var direction  = OrderByDirection.Acending;

                    if (_parser.CurrentTokenKind == QueryExpressionTokenKind.Keyword)
                    {
                        var order = (string)_parser.TokenData;
                        switch (order)
                        {
                        case "asc":
                            direction = OrderByDirection.Acending;
                            _parser.NextToken();
                            break;

                        case "desc":
                            direction = OrderByDirection.Descending;
                            _parser.NextToken();
                            break;

                        default:
                            _parser.ReportError(
                                $"Invalid token '{order}' after sort expression, expecting 'asc' or 'desc'");
                            return;
                        }
                    }

                    var item = new OrderByClause(expression, direction);
                    _clauses.Add(item);

                    if (_parser.CurrentTokenKind == QueryExpressionTokenKind.Comma)
                    {
                        _parser.NextToken();
                    }
                    else
                    {
                        break;
                    }
                }
            }