From 0d6ed02c3386aba22a1121c8a2e4859369762f92 Mon Sep 17 00:00:00 2001 From: Shaun Houlihan Date: Wed, 4 Nov 2026 22:03:04 -0700 Subject: [PATCH] arazzo: support &&, || and grouping in simple criteria MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Arazzo Criterion Object operators table lists && (and), || (or) and () (logical grouping) alongside the comparison operators, but simple conditions were split on the first comparison operator found, so only a single `left right` comparison could be expressed. Grammar Simple conditions are tokenized and parsed by a small recursive-descent parser. || binds looser than &&, which binds looser than the comparison operators (==, !=, <, <=, >, >=). Comparisons do not chain, and a parenthesised group is a boolean operand, so `($flag) == true` coerces the group before comparing. Malformed conditions (unterminated strings, unbalanced parentheses, misplaced or trailing operators) are errors. Tokenizing runtime expressions JSON pointers, header names and parameter names may contain operator characters, so a runtime expression is one operand that runs to the next whitespace, matching expression.SplitSimpleCondition on main: `$response.body#/a>b == 1` compares the value at /a>b. A trailing ')' that the expression does not balance itself closes an open group, so `($response.body#/flag)` works and `#/f(x)` stays intact. Consequences: - `$statusCode==200` without spaces is a single operand, as on main. - Inside a group, a pointer token ending in an unbalanced ')' cannot be written; that ')' always closes the group. Whitespace is matched as ASCII only. The tokenizer works on bytes, and unicode.IsSpace(rune(b)) misread the UTF-8 continuation bytes 0x85 and 0xA0 (as in "Å" and "à") as whitespace, splitting those operands. Compile once, cache per engine Each condition is compiled once into an evaluation tree and cached in a new criterionCaches.simple map, keyed by the trimmed condition. Syntax errors are cached too, as the regex and JSONPath caches already do, and Engine.ClearCaches() resets the map with the rest. Literal operands are converted at compile time. Runtime expressions keep their raw text and are evaluated against the current context through the engine's expression cache, so a cached tree never holds runtime values. EvaluateCriterion without an engine compiles on every call. Because conditions compile before evaluating, a syntax error is now reported ahead of a runtime error from an earlier operand. https://spec.openapis.org/arazzo/v1.0.1.html#operators Co-Authored-By: Claude Opus 5.5 --- arazzo/arazzo_benchmark_test.go | 34 ++ arazzo/criterion.go | 496 ++++++++++++++++++++++++++-- arazzo/criterion_test.go | 566 +++++++++++++++++++++++++++++++- 3 files changed, 1058 insertions(+), 38 deletions(-) diff --git a/arazzo/arazzo_benchmark_test.go b/arazzo/arazzo_benchmark_test.go index cda2cd75..d25e8ed1 100644 --- a/arazzo/arazzo_benchmark_test.go +++ b/arazzo/arazzo_benchmark_test.go @@ -9,6 +9,8 @@ import ( "testing" libopenapi "github.com/pb33f/libopenapi" + "github.com/pb33f/libopenapi/arazzo/expression" + high "github.com/pb33f/libopenapi/datamodel/high/arazzo" ) func largeArazzoDocument(stepCount, outputCount int) []byte { @@ -82,3 +84,35 @@ func BenchmarkRenderLargeArazzoSelectorMap(b *testing.B) { } } } + +func BenchmarkSimpleCriterion(b *testing.B) { + ctx := &expression.Context{StatusCode: 200, Method: "POST"} + for _, condition := range []string{ + "true", + "$statusCode == 200", + "$method == 'POST'", + "$statusCode == 200 && ($method == 'POST' || $method == 'PUT')", + } { + b.Run(condition, func(b *testing.B) { + caches := newCriterionCaches() + exprCache := map[string]expression.Expression{} + caches.parseExpr = func(s string) (expression.Expression, error) { + if e, ok := exprCache[s]; ok { + return e, nil + } + e, err := expression.Parse(s) + if err == nil { + exprCache[s] = e + } + return e, err + } + criterion := &high.Criterion{Condition: condition} + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if _, err := evaluateCriterionImpl(criterion, ctx, caches); err != nil { + b.Fatal(err) + } + } + }) + } +} diff --git a/arazzo/criterion.go b/arazzo/criterion.go index 193afd97..aa4fbcb1 100644 --- a/arazzo/criterion.go +++ b/arazzo/criterion.go @@ -11,6 +11,7 @@ import ( "github.com/pb33f/jsonpath/pkg/jsonpath" jsonpathconfig "github.com/pb33f/jsonpath/pkg/jsonpath/config" + "github.com/pb33f/libopenapi/arazzo/expression" high "github.com/pb33f/libopenapi/datamodel/high/arazzo" ) @@ -32,11 +33,13 @@ const ( criterionJSONPathLegacy criterionJSONPathDialect = "draft-goessner-dispatch-jsonpath-00" ) -// criterionCaches holds per-Engine caches for compiled criterion patterns. -// Using plain maps instead of sync.Map because Engine is not safe for concurrent use. +// criterionCaches holds per-Engine caches for compiled criterion patterns and +// simple conditions. Using plain maps instead of sync.Map because Engine is not +// safe for concurrent use. type criterionCaches struct { regex map[string]cachedCriterionRegex jsonPath map[string]cachedCriterionJSONPath + simple map[string]cachedSimpleCondition parseExpr func(string) (expression.Expression, error) } @@ -44,6 +47,7 @@ func newCriterionCaches() *criterionCaches { return &criterionCaches{ regex: make(map[string]cachedCriterionRegex), jsonPath: make(map[string]cachedCriterionJSONPath), + simple: make(map[string]cachedSimpleCondition), } } @@ -98,36 +102,19 @@ func evaluateSimpleCondition(condition string, value any) (bool, error) { func evaluateSimpleConditionString(condition string, exprCtx *expression.Context, caches *criterionCaches) (bool, error) { trimmed := strings.TrimSpace(condition) - if trimmed == "" { + // Bare boolean literals skip compilation and the cache lookup. Only the + // exact words are booleans, matching newOperandNode. + switch trimmed { + case "": + return false, nil + case "true": + return true, nil + case "false": return false, nil } - - if b, err := strconv.ParseBool(trimmed); err == nil { - return b, nil - } - - leftRaw, op, rightRaw, found := expression.SplitSimpleCondition(trimmed) - if found { - left, err := evaluateSimpleOperand(leftRaw, exprCtx, caches) - if err != nil { - return false, err - } - right, err := evaluateSimpleOperand(rightRaw, exprCtx, caches) - if err != nil { - return false, err - } - return compareSimpleValues(left, right, op) - } - - val, err := evaluateSimpleOperand(trimmed, exprCtx, caches) - if err != nil { - return false, err - } - b, ok := val.(bool) - if !ok { - return false, fmt.Errorf("simple condition %q did not evaluate to a boolean", condition) - } - return b, nil + // Compound simple conditions may combine literals, operators, and runtime + // expressions with &&, ||, and parentheses (Arazzo 1.0.1 Criterion Object). + return evaluateBooleanExpr(trimmed, exprCtx, caches) } func evaluateSimpleOperand(operand string, exprCtx *expression.Context, caches *criterionCaches) (any, error) { @@ -162,6 +149,23 @@ func evaluateSimpleOperand(operand string, exprCtx *expression.Context, caches * } func compareSimpleValues(left, right any, op string) (bool, error) { + // A null or missing operand has no ordering, so only equality is meaningful. + // Without this, nil falls through to the string comparison below as "", + // which sorts after digits and makes `$response.body#/count > 0` pass on a 204. + if left == nil || right == nil { + bothNil := left == nil && right == nil + switch op { + case "==": + return bothNil, nil + case "!=": + return !bothNil, nil + case ">", "<", ">=", "<=": + return false, nil + default: + return false, fmt.Errorf("unsupported operator %q", op) + } + } + if ln, lok := numericValue(left); lok { if rn, rok := numericValue(right); rok { switch op { @@ -327,14 +331,19 @@ func compileCriterionJSONPath( // evaluateExprString evaluates a runtime expression string, using the cached parser when available. func evaluateExprString(input string, ctx *expression.Context, caches *criterionCaches) (any, error) { + expr, err := parseRuntimeExpr(input, caches) + if err != nil { + return nil, err + } + return expression.Evaluate(expr, ctx) +} + +// parseRuntimeExpr parses a runtime expression, using the cached parser when available. +func parseRuntimeExpr(input string, caches *criterionCaches) (expression.Expression, error) { if caches != nil && caches.parseExpr != nil { - expr, err := caches.parseExpr(input) - if err != nil { - return nil, err - } - return expression.Evaluate(expr, ctx) + return caches.parseExpr(input) } - return expression.EvaluateString(input, ctx) + return expression.Parse(input) } // sprintValue converts a value to its string representation using type-specific fast paths @@ -355,3 +364,418 @@ func sprintValue(v any) string { return fmt.Sprintf("%v", v) } } + +type boolTokenKind int + +const ( + tokEOF boolTokenKind = iota + tokOperand + tokAnd + tokOr + tokEq + tokNeq + tokGt + tokLt + tokGte + tokLte + tokLParen + tokRParen +) + +type boolToken struct { + kind boolTokenKind + raw string +} + +// compiledCondition is a simple condition parsed once into an evaluation tree. +// Literal operands are converted at compile time. Runtime expressions keep their +// raw text and are resolved on every evaluation through evaluateExprString, so +// they still go through the engine's expression cache. +type compiledCondition struct { + root *conditionNode + input string +} + +type conditionNodeKind uint8 + +const ( + condOperand conditionNodeKind = iota + condGroup + condOr + condAnd + condCompare +) + +type conditionNode struct { + kind conditionNodeKind + op string // comparison operator, condCompare only + left *conditionNode // condOr, condAnd, condCompare; the inner node for condGroup + right *conditionNode // condOr, condAnd, condCompare + // Operand fields. + raw string // runtime expression text when isExpr is true + isExpr bool + literal any // pre-converted literal when isExpr is false +} + +type cachedSimpleCondition struct { + cond *compiledCondition + err error +} + +func evaluateBooleanExpr(condition string, exprCtx *expression.Context, caches *criterionCaches) (bool, error) { + cond, err := compileSimpleCondition(condition, caches) + if err != nil { + return false, err + } + return cond.truth(cond.root, exprCtx, caches) +} + +// compileSimpleCondition returns the compiled tree for condition, caching both +// successful compiles and syntax errors when caches is non-nil. +func compileSimpleCondition(condition string, caches *criterionCaches) (*compiledCondition, error) { + if caches != nil { + if cached, ok := caches.simple[condition]; ok { + return cached.cond, cached.err + } + } + cond, err := parseSimpleCondition(condition) + if caches != nil { + caches.simple[condition] = cachedSimpleCondition{cond: cond, err: err} + } + return cond, err +} + +func parseSimpleCondition(condition string) (*compiledCondition, error) { + tokens, err := tokenizeSimpleCondition(condition) + if err != nil { + return nil, err + } + p := &conditionParser{tokens: tokens, input: condition} + root, err := p.parseOr() + if err != nil { + return nil, err + } + if p.peek().kind != tokEOF { + return nil, fmt.Errorf("unexpected token %q in simple condition %q", p.peek().raw, condition) + } + return &compiledCondition{root: root, input: condition}, nil +} + +// truth evaluates n in a boolean position. +func (c *compiledCondition) truth(n *conditionNode, exprCtx *expression.Context, caches *criterionCaches) (bool, error) { + switch n.kind { + case condOr, condAnd: + left, err := c.truth(n.left, exprCtx, caches) + if err != nil { + return false, err + } + // Short-circuit: a true left side decides ||, a false one decides &&, + // and the right side is never evaluated, so its errors cannot surface. + if n.kind == condOr && left { + return true, nil + } + if n.kind == condAnd && !left { + return false, nil + } + return c.truth(n.right, exprCtx, caches) + case condCompare: + left, err := c.value(n.left, exprCtx, caches) + if err != nil { + return false, err + } + right, err := c.value(n.right, exprCtx, caches) + if err != nil { + return false, err + } + return compareSimpleValues(left, right, n.op) + case condGroup: + return c.truth(n.left, exprCtx, caches) + default: + v, err := c.value(n, exprCtx, caches) + if err != nil { + return false, err + } + // A missing runtime value (value maps it to nil) or an explicit null + // used as a bare operand is falsy, so `$statusCode == 204 || $response.body#/flag` + // still passes on an empty 204 response. + if v == nil { + return false, nil + } + b, ok := v.(bool) + if !ok { + return false, fmt.Errorf("simple condition %q did not evaluate to a boolean", c.input) + } + return b, nil + } +} + +// value evaluates n in a comparison-operand position. +func (c *compiledCondition) value(n *conditionNode, exprCtx *expression.Context, caches *criterionCaches) (any, error) { + if n.kind != condOperand { + return c.truth(n, exprCtx, caches) + } + if !n.isExpr { + return n.literal, nil + } + expr, err := parseRuntimeExpr(n.raw, caches) + if err != nil { + return nil, err + } + v, err := expression.Evaluate(expr, exprCtx) + if err != nil && exprCtx != nil && isOptionalRuntimeValue(expr.Type) { + return nil, nil + } + return v, err +} + +type conditionParser struct { + tokens []boolToken + pos int + input string +} + +func (p *conditionParser) peek() boolToken { + if p.pos >= len(p.tokens) { + return boolToken{kind: tokEOF} + } + return p.tokens[p.pos] +} + +func (p *conditionParser) next() boolToken { + tok := p.peek() + if tok.kind != tokEOF { + p.pos++ + } + return tok +} + +func (p *conditionParser) parseOr() (*conditionNode, error) { + left, err := p.parseAnd() + if err != nil { + return nil, err + } + for p.peek().kind == tokOr { + p.next() + right, err := p.parseAnd() + if err != nil { + return nil, err + } + left = &conditionNode{kind: condOr, left: left, right: right} + } + return left, nil +} + +func (p *conditionParser) parseAnd() (*conditionNode, error) { + left, err := p.parseComparison() + if err != nil { + return nil, err + } + for p.peek().kind == tokAnd { + p.next() + right, err := p.parseComparison() + if err != nil { + return nil, err + } + left = &conditionNode{kind: condAnd, left: left, right: right} + } + return left, nil +} + +func (p *conditionParser) parseComparison() (*conditionNode, error) { + left, err := p.parsePrimary() + if err != nil { + return nil, err + } + if op, ok := comparisonOp(p.peek().kind); ok { + p.next() + right, err := p.parsePrimary() + if err != nil { + return nil, err + } + return &conditionNode{kind: condCompare, op: op, left: left, right: right}, nil + } + return left, nil +} + +func (p *conditionParser) parsePrimary() (*conditionNode, error) { + tok := p.next() + switch tok.kind { + case tokLParen: + inner, err := p.parseOr() + if err != nil { + return nil, err + } + if p.next().kind != tokRParen { + return nil, fmt.Errorf("missing closing parenthesis in simple condition %q", p.input) + } + // Keep the group node: a parenthesised operand is coerced to a boolean + // before it is compared, e.g. `($flag) == true`. + return &conditionNode{kind: condGroup, left: inner}, nil + case tokOperand: + return newOperandNode(tok.raw), nil + default: + return nil, fmt.Errorf("unexpected token %q in simple condition %q", tok.raw, p.input) + } +} + +func newOperandNode(raw string) *conditionNode { + op := strings.TrimSpace(raw) + if strings.HasPrefix(op, "$") { + return &conditionNode{kind: condOperand, raw: op, isExpr: true} + } + // Literals never touch the expression context, so they convert once here. + literal, _ := evaluateSimpleOperand(op, nil, nil) + return &conditionNode{kind: condOperand, literal: literal} +} + +func comparisonOp(kind boolTokenKind) (string, bool) { + switch kind { + case tokEq: + return "==", true + case tokNeq: + return "!=", true + case tokGte: + return ">=", true + case tokLte: + return "<=", true + case tokGt: + return ">", true + case tokLt: + return "<", true + default: + return "", false + } +} + +func tokenizeSimpleCondition(input string) ([]boolToken, error) { + var tokens []boolToken + i := 0 + depth := 0 // open parentheses so far; the parser reports any imbalance + for i < len(input) { + for i < len(input) && isASCIISpace(input[i]) { + i++ + } + if i >= len(input) { + break + } + rest := input[i:] + switch { + case strings.HasPrefix(rest, "&&"): + tokens = append(tokens, boolToken{kind: tokAnd, raw: "&&"}) + i += 2 + case strings.HasPrefix(rest, "||"): + tokens = append(tokens, boolToken{kind: tokOr, raw: "||"}) + i += 2 + case strings.HasPrefix(rest, "=="): + tokens = append(tokens, boolToken{kind: tokEq, raw: "=="}) + i += 2 + case strings.HasPrefix(rest, "!="): + tokens = append(tokens, boolToken{kind: tokNeq, raw: "!="}) + i += 2 + case strings.HasPrefix(rest, ">="): + tokens = append(tokens, boolToken{kind: tokGte, raw: ">="}) + i += 2 + case strings.HasPrefix(rest, "<="): + tokens = append(tokens, boolToken{kind: tokLte, raw: "<="}) + i += 2 + case rest[0] == '>': + tokens = append(tokens, boolToken{kind: tokGt, raw: ">"}) + i++ + case rest[0] == '<': + tokens = append(tokens, boolToken{kind: tokLt, raw: "<"}) + i++ + case rest[0] == '(': + tokens = append(tokens, boolToken{kind: tokLParen, raw: "("}) + depth++ + i++ + case rest[0] == ')': + tokens = append(tokens, boolToken{kind: tokRParen, raw: ")"}) + depth-- + i++ + default: + start := i + if rest[0] == '\'' || rest[0] == '"' { + q := rest[0] + i++ + for i < len(input) && input[i] != q { + i++ + } + if i >= len(input) { + return nil, fmt.Errorf("unterminated string in simple condition %q", input) + } + i++ + } else if rest[0] == '$' { + i = runtimeOperandEnd(input, i, depth) + } else { + for i < len(input) && !isBoolOpStart(input[i:]) && input[i] != '(' && input[i] != ')' && !isASCIISpace(input[i]) { + i++ + } + } + tokens = append(tokens, boolToken{kind: tokOperand, raw: input[start:i]}) + } + } + return tokens, nil +} + +// runtimeOperandEnd returns the end of the runtime expression starting at +// input[start]. JSON Pointers, header names and parameter names may contain +// operator characters, so, as with expression.SplitSimpleCondition, a runtime +// expression runs to the next whitespace. Trailing ')' characters the +// expression does not balance itself close open groups instead, so +// `($response.body#/flag)` keeps working while `#/f(x)` stays intact. +func runtimeOperandEnd(input string, start, depth int) int { + end := start + 1 + opens, closes := 0, 0 + for end < len(input) && !isASCIISpace(input[end]) { + switch input[end] { + case '(': + opens++ + case ')': + closes++ + } + end++ + } + for unmatched := closes - opens; unmatched > 0 && depth > 0 && input[end-1] == ')'; unmatched-- { + end-- + depth-- + } + return end +} + +// isASCIISpace reports whether c is ASCII whitespace. The tokenizer works on +// bytes, and unicode.IsSpace(rune(c)) would misread UTF-8 continuation bytes +// such as 0x85 (in "Å") and 0xA0 (in "à") as NEL and NBSP. +func isASCIISpace(c byte) bool { + switch c { + case ' ', '\t', '\n', '\r', '\v', '\f': + return true + } + return false +} + +func isBoolOpStart(s string) bool { + if s == "" { + return false + } + return strings.HasPrefix(s, "&&") || + strings.HasPrefix(s, "||") || + strings.HasPrefix(s, "==") || + strings.HasPrefix(s, "!=") || + strings.HasPrefix(s, ">=") || + strings.HasPrefix(s, "<=") || + s[0] == '>' || + s[0] == '<' +} + +// isOptionalRuntimeValue reports whether an expression of type t reads a value +// that legitimately varies per request or response: a header, query or path +// parameter, or the body. +func isOptionalRuntimeValue(t expression.ExpressionType) bool { + switch t { + case expression.RequestHeader, expression.RequestQuery, expression.RequestPath, expression.RequestBody, + expression.ResponseHeader, expression.ResponseQuery, expression.ResponsePath, expression.ResponseBody: + return true + default: + return false + } +} diff --git a/arazzo/criterion_test.go b/arazzo/criterion_test.go index 24ac52b2..e7e3fbce 100644 --- a/arazzo/criterion_test.go +++ b/arazzo/criterion_test.go @@ -7,10 +7,11 @@ import ( "testing" "github.com/pb33f/go-yaml" - "github.com/pb33f/libopenapi/arazzo/expression" - high "github.com/pb33f/libopenapi/datamodel/high/arazzo" "github.com/pb33f/testify/assert" "github.com/pb33f/testify/require" + + "github.com/pb33f/libopenapi/arazzo/expression" + high "github.com/pb33f/libopenapi/datamodel/high/arazzo" ) func TestEvaluateCriterion_SimpleCondition_StatusCodeComparison(t *testing.T) { @@ -59,3 +60,564 @@ func TestEvaluateCriterion_SimpleCondition_NumberAndBooleanLiterals(t *testing.T assert.Equal(t, want, ok, condition) } } + +func TestEvaluateCriterion_SimpleCondition_BooleanOperators(t *testing.T) { + criterion := &high.Criterion{ + Condition: `$statusCode == 204 || ($statusCode == 200 && ($response.body#/challengeName == 'NEW_PASSWORD_REQUIRED' || $response.body#/challengeName == 'SOFTWARE_TOKEN_MFA'))`, + } + + ok, err := EvaluateCriterion(criterion, &expression.Context{StatusCode: 204}) + require.NoError(t, err) + assert.True(t, ok, "204 with an empty body should pass") + + ok, err = EvaluateCriterion(criterion, &expression.Context{ + StatusCode: 200, + ResponseBody: yamlMapping(t, map[string]any{"challengeName": "NEW_PASSWORD_REQUIRED"}), + }) + require.NoError(t, err) + assert.True(t, ok) + + ok, err = EvaluateCriterion(criterion, &expression.Context{ + StatusCode: 200, + ResponseBody: yamlMapping(t, map[string]any{"challengeName": "MFA_SETUP"}), + }) + require.NoError(t, err) + assert.False(t, ok, "unsupported challenge must fail") +} + +func yamlMapping(t *testing.T, v any) *yaml.Node { + t.Helper() + b, err := yaml.Marshal(v) + require.NoError(t, err) + var node yaml.Node + require.NoError(t, yaml.Unmarshal(b, &node)) + if node.Kind == yaml.DocumentNode && len(node.Content) > 0 { + return node.Content[0] + } + return &node +} + +// A missing runtime value resolves to null. Null has no ordering, so every +// ordering comparison against it must fail; only == and != are meaningful. +func TestEvaluateCriterion_MissingValueComparisons(t *testing.T) { + cases := []struct { + condition string + want bool + }{ + {"$response.body#/count > 0", false}, + {"$response.body#/count >= 0", false}, + {"$response.body#/count < 5", false}, + {"$response.body#/count <= 0", false}, + {"$response.body#/count == 0", false}, + {"$response.body#/count != 0", true}, + {"$response.body#/name == ''", false}, + {"$response.header.X-Count > 0", false}, + {"$statusCode == 204 && $response.body#/count > 0", false}, + {"$statusCode == 204 || $response.body#/count > 0", true}, + {"$response.body#/count > 0 || $statusCode == 204", true}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + ok, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, + &expression.Context{StatusCode: 204}) + require.NoError(t, err) + assert.Equal(t, tc.want, ok) + }) + } +} + +// Every kind of absent response or request value is null, whether the source +// map is nil or present without the key, and whether a JSON Pointer misses a +// key, overruns an array or walks into a scalar. +func TestEvaluateCriterion_AbsentRuntimeValuesAreNull(t *testing.T) { + body := yamlMapping(t, map[string]any{"items": []any{}, "data": nil}) + noHeaders := &expression.Context{StatusCode: 200, ResponseBody: body} + otherHeaders := &expression.Context{ + StatusCode: 200, + ResponseBody: body, + RequestHeaders: map[string]string{"Other": "1"}, + RequestQuery: map[string]string{"other": "1"}, + RequestPath: map[string]string{"other": "1"}, + } + // A null operand is unequal to any literal and falsy as a bare operand. + var conditions []string + for _, operand := range []string{ + "$response.body#/items/0/id", + "$response.body#/data/id", + "$request.header.X", + "$request.query.x", + "$request.path.x", + } { + conditions = append(conditions, + operand+" != 0", + "$statusCode == 200 && "+operand+" != 0", + operand+" || $statusCode == 200", + ) + } + for name, ctx := range map[string]*expression.Context{"no sources": noHeaders, "other keys": otherHeaders} { + for _, condition := range conditions { + t.Run(name+"/"+condition, func(t *testing.T) { + ok, err := EvaluateCriterion(&high.Criterion{Condition: condition}, ctx) + require.NoError(t, err) + assert.True(t, ok) + }) + } + } +} + +// Only request and response values are optional. A pointer miss inside a step +// output, an absent payload, or a nil context is still an error. +func TestEvaluateCriterion_NonExchangeMissingValuesError(t *testing.T) { + ctx := &expression.Context{ + StatusCode: 200, + Steps: map[string]*expression.StepContext{ + "s": {Outputs: map[string]any{"body": map[string]any{}}}, + }, + } + cases := []struct { + condition string + ctx *expression.Context + errText string + }{ + {"$steps.s.outputs.body#/missing == 1", ctx, `JSON pointer segment "missing" not found`}, + {"$response.payload == 1", ctx, "no response payload available"}, + {"$response.header.X == 1", nil, "nil context"}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + _, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, tc.ctx) + require.Error(t, err) + assert.Contains(t, err.Error(), tc.errText) + }) + } +} + +// An explicit JSON null in a body has no ordering and is falsy as a bare operand. +func TestEvaluateCriterion_NullBodyValueComparisons(t *testing.T) { + body := yamlMapping(t, map[string]any{"count": nil}) + ctx := &expression.Context{StatusCode: 200, ResponseBody: body} + + ok, err := EvaluateCriterion(&high.Criterion{Condition: "$response.body#/count > 0"}, ctx) + require.NoError(t, err) + assert.False(t, ok) + + ok, err = EvaluateCriterion(&high.Criterion{Condition: "$response.body#/count != 0"}, ctx) + require.NoError(t, err) + assert.True(t, ok) + + // A bare null operand is falsy, leaving the rest of the condition to decide. + ok, err = EvaluateCriterion(&high.Criterion{Condition: "$response.body#/count"}, ctx) + require.NoError(t, err) + assert.False(t, ok) + + ok, err = EvaluateCriterion(&high.Criterion{Condition: "$response.body#/count || $statusCode == 200"}, ctx) + require.NoError(t, err) + assert.True(t, ok) +} + +func TestCompareSimpleValues_Nil(t *testing.T) { + ok, err := compareSimpleValues(nil, nil, "==") + require.NoError(t, err) + assert.True(t, ok) + + ok, err = compareSimpleValues(nil, nil, "!=") + require.NoError(t, err) + assert.False(t, ok) + + ok, err = compareSimpleValues(nil, int64(1), ">") + require.NoError(t, err) + assert.False(t, ok) + + _, err = compareSimpleValues(nil, int64(1), "~=") + assert.Error(t, err) +} + +// A missing value used as a bare boolean operand is falsy rather than an error, +// so the rest of the condition still decides the outcome. +func TestEvaluateCriterion_MissingBareOperandIsFalse(t *testing.T) { + cases := []struct { + condition string + want bool + }{ + {"$response.body#/flag", false}, + {"$response.header.X-Flag", false}, + {"$statusCode == 204 || $response.body#/flag", true}, + {"$response.body#/flag || $statusCode == 204", true}, + {"$statusCode == 204 && $response.body#/flag", false}, + {"($response.body#/flag) || $statusCode == 204", true}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + ok, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, + &expression.Context{StatusCode: 204}) + require.NoError(t, err) + assert.Equal(t, tc.want, ok) + }) + } +} + +// && and || stop once the left side decides the result. Each right side here +// would fail if evaluated: an unknown step, or a non-boolean bare operand. +func TestEvaluateCriterion_ShortCircuit(t *testing.T) { + cases := []struct { + condition string + want bool + }{ + {"$statusCode == 204 || $steps.nope.outputs.x == 1", true}, + {"$statusCode == 200 && $steps.nope.outputs.x == 1", false}, + {"true || $statusCode", true}, + {"false && $statusCode", false}, + {"$statusCode == 200 || $statusCode == 204 || $steps.nope.outputs.x == 1", true}, + {"$statusCode == 200 && $steps.nope.outputs.x == 1 || $statusCode == 204", true}, + {"$statusCode == 204 || ($steps.nope.outputs.x == 1 && $statusCode)", true}, + {"($statusCode == 200 && $statusCode) || $method == 'GET'", true}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + ok, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, + &expression.Context{StatusCode: 204, Method: "GET", Steps: map[string]*expression.StepContext{}}) + require.NoError(t, err) + assert.Equal(t, tc.want, ok) + }) + } +} + +// Bare true and false take the fast path; other spellings are not booleans. +func TestEvaluateCriterion_BareBooleanLiterals(t *testing.T) { + for condition, want := range map[string]bool{"true": true, "false": false, " true\t": true, "\nfalse ": false} { + ok, err := EvaluateCriterion(&high.Criterion{Condition: condition}, &expression.Context{}) + require.NoError(t, err, condition) + assert.Equal(t, want, ok, condition) + } + for _, condition := range []string{"TRUE", "True", "t", "1", "0"} { + _, err := EvaluateCriterion(&high.Criterion{Condition: condition}, &expression.Context{}) + assert.ErrorContains(t, err, "did not evaluate to a boolean", condition) + } +} + +// A present but non-boolean bare operand is still an authoring error. +func TestEvaluateCriterion_NonBooleanBareOperandErrors(t *testing.T) { + _, err := EvaluateCriterion(&high.Criterion{Condition: "$statusCode"}, + &expression.Context{StatusCode: 204}) + require.Error(t, err) + assert.Contains(t, err.Error(), "did not evaluate to a boolean") +} + +func TestEvaluateCriterion_SimpleConditionSyntaxErrors(t *testing.T) { + cases := []struct { + condition string + errText string + }{ + {"$statusCode == 'abc", "unterminated string"}, + {"$statusCode == 204 )", `unexpected token ")"`}, + {"($statusCode == 204", "missing closing parenthesis"}, + {"&& $statusCode == 204", `unexpected token "&&"`}, + {"$statusCode ==", "unexpected token"}, + // A syntax error after ||, after && or inside a group propagates out. + {"$statusCode == 204 ||", "unexpected token"}, + {"$statusCode == 204 &&", "unexpected token"}, + {"($statusCode == 204 &&", "unexpected token"}, + // Non-optional errors still propagate from every position in the parse. + {"($steps.nope.outputs.x == 1)", `step "nope" not found`}, + {"$statusCode == 200 || $steps.nope.outputs.x == 1", `step "nope" not found`}, + {"$statusCode == 204 && $steps.nope.outputs.x == 1", `step "nope" not found`}, + {"$steps.nope.outputs.x == 1 || $statusCode == 204", `step "nope" not found`}, + {"$statusCode == $steps.nope.outputs.x", `step "nope" not found`}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + _, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, + &expression.Context{StatusCode: 204, Steps: map[string]*expression.StepContext{}}) + require.Error(t, err) + assert.Contains(t, err.Error(), tc.errText) + }) + } +} + +func TestTokenizeSimpleCondition_TrailingWhitespace(t *testing.T) { + tokens, err := tokenizeSimpleCondition("$statusCode == 204 \t") + require.NoError(t, err) + require.Len(t, tokens, 3) + assert.Equal(t, tokOperand, tokens[0].kind) + assert.Equal(t, tokEq, tokens[1].kind) + assert.Equal(t, "204", tokens[2].raw) +} + +func TestIsBoolOpStart(t *testing.T) { + assert.False(t, isBoolOpStart("")) + assert.False(t, isBoolOpStart("abc")) + for _, s := range []string{"&&", "||", "==", "!=", ">=", "<=", ">", "<"} { + assert.True(t, isBoolOpStart(s+" x"), s) + } +} + +// Comparison operator characters are legal in JSON Pointer reference tokens, +// so they belong to the runtime expression rather than splitting the condition. +func TestEvaluateCriterion_OperatorCharsInsideJSONPointer(t *testing.T) { + body := yamlMapping(t, map[string]any{"a>b": 1, "x==y": "ok", "p<=q": 3}) + ctx := &expression.Context{StatusCode: 200, ResponseBody: body} + + cases := []struct { + condition string + want bool + }{ + {"$response.body#/a>b == 1", true}, + {"$response.body#/a>b == 2", false}, + {"$response.body#/x==y == 'ok'", true}, + {"$response.body#/p<=q > 2", true}, + {"$statusCode == 200 && $response.body#/a>b == 1", true}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + ok, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, ctx) + require.NoError(t, err) + assert.Equal(t, tc.want, ok) + }) + } +} + +func TestCompileSimpleCondition_CachesTreeAndErrors(t *testing.T) { + caches := newCriterionCaches() + + first, err := compileSimpleCondition("$statusCode == 200", caches) + require.NoError(t, err) + second, err := compileSimpleCondition("$statusCode == 200", caches) + require.NoError(t, err) + assert.Same(t, first, second, "a repeated condition must reuse the compiled tree") + + _, err = compileSimpleCondition("$statusCode == 'abc", caches) + require.ErrorContains(t, err, "unterminated string") + _, err = compileSimpleCondition("$statusCode == 'abc", caches) + require.ErrorContains(t, err, "unterminated string") + assert.Len(t, caches.simple, 2, "syntax errors are cached alongside compiled trees") + + // Without caches every call compiles a fresh tree. + a, err := compileSimpleCondition("$statusCode == 200", nil) + require.NoError(t, err) + b, err := compileSimpleCondition("$statusCode == 200", nil) + require.NoError(t, err) + assert.NotSame(t, a, b) +} + +// The cached tree holds only literals and expression text, never runtime values, +// so reusing it across contexts must not leak a previous evaluation's result. +func TestEvaluateCriterion_CachedSimpleConditionUsesCurrentContext(t *testing.T) { + caches := newCriterionCaches() + criterion := &high.Criterion{Condition: "$statusCode == 200 && ($method == 'POST' || $method == 'PUT')"} + + cases := []struct { + ctx *expression.Context + want bool + }{ + {&expression.Context{StatusCode: 200, Method: "POST"}, true}, + {&expression.Context{StatusCode: 500, Method: "POST"}, false}, + {&expression.Context{StatusCode: 200, Method: "PUT"}, true}, + {&expression.Context{StatusCode: 200, Method: "GET"}, false}, + } + for _, tc := range cases { + ok, err := evaluateCriterionImpl(criterion, tc.ctx, caches) + require.NoError(t, err) + assert.Equal(t, tc.want, ok, "%d %s", tc.ctx.StatusCode, tc.ctx.Method) + } + assert.Len(t, caches.simple, 1) +} + +func TestEvaluateCriterion_SimpleConditionGroupsAndLiterals(t *testing.T) { + ctx := &expression.Context{StatusCode: 200} + cases := []struct { + condition string + want bool + }{ + // A parenthesised expression is a boolean operand of the outer comparison. + {"($statusCode == 200) == true", true}, + {"($statusCode == 500) != false", false}, + {"(true)", true}, + {"((false) || ($statusCode >= 200 && $statusCode < 300))", true}, + // Literals compare by their pre-converted types. + {"1 == 1.0", true}, + {"2 > 10", false}, + {"false == false", true}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + for _, caches := range []*criterionCaches{nil, newCriterionCaches()} { + ok, err := evaluateCriterionImpl(&high.Criterion{Condition: tc.condition}, ctx, caches) + require.NoError(t, err) + assert.Equal(t, tc.want, ok) + } + }) + } +} + +// Conditions compile before any operand is evaluated, so a syntax error wins +// over a runtime error from an operand earlier in the string. +func TestEvaluateCriterion_SyntaxErrorReportedBeforeEvaluation(t *testing.T) { + _, err := EvaluateCriterion(&high.Criterion{Condition: "$steps.nope.outputs.x == 1 )"}, + &expression.Context{Steps: map[string]*expression.StepContext{}}) + require.ErrorContains(t, err, `unexpected token ")"`) +} + +func TestEvaluateCriterion_ParenthesisedNonBooleanErrors(t *testing.T) { + _, err := EvaluateCriterion(&high.Criterion{Condition: "($statusCode) == true"}, + &expression.Context{StatusCode: 200}) + require.ErrorContains(t, err, "did not evaluate to a boolean") +} + +// A runtime expression runs to whitespace, so a ')' it does not balance itself +// closes an open group, while parentheses inside a pointer stay in the pointer. +func TestEvaluateCriterion_RuntimeOperandParentheses(t *testing.T) { + body := yamlMapping(t, map[string]any{"flag": true, "f(x)": 1, "g)": 2}) + ctx := &expression.Context{StatusCode: 200, ResponseBody: body} + + cases := []struct { + condition string + want bool + }{ + {"($response.body#/flag)", true}, + {"(($response.body#/flag))", true}, + {"($statusCode == 500 || $response.body#/flag)", true}, + {"$response.body#/f(x) == 1", true}, + {"($response.body#/f(x) == 1)", true}, + {"(($response.body#/f(x) == 1))", true}, + // Outside any group a trailing ')' belongs to the pointer. + {"$response.body#/g) == 2", true}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + ok, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, ctx) + require.NoError(t, err) + assert.Equal(t, tc.want, ok) + }) + } +} + +func TestEvaluateCriterion_NonASCIIOperands(t *testing.T) { + // "Å" is C3 85 and "à" is C3 A0; the continuation bytes must not split tokens. + body := yamlMapping(t, map[string]any{"Åsa": "à"}) + ctx := &expression.Context{ResponseBody: body} + + for condition, want := range map[string]bool{ + "$response.body#/Åsa == 'à'": true, + "$response.body#/Åsa == à": true, + "Å == à": false, + "Å == Å": true, + } { + ok, err := EvaluateCriterion(&high.Criterion{Condition: condition}, ctx) + require.NoError(t, err, condition) + assert.Equal(t, want, ok, condition) + } +} + +func TestTokenizeSimpleCondition_RuntimeOperandEndsAtWhitespace(t *testing.T) { + cases := map[string][]string{ + "$response.body#/a>b == 1": {"$response.body#/a>b", "==", "1"}, + "$response.header.X&&Y != 'z'": {"$response.header.X&&Y", "!=", "'z'"}, + "($response.body#/flag)": {"(", "$response.body#/flag", ")"}, + "(($response.body#/f(x)))": {"(", "(", "$response.body#/f(x)", ")", ")"}, + "$response.body#/g)": {"$response.body#/g)"}, + "$statusCode == 1&&$statusCode": {"$statusCode", "==", "1", "&&", "$statusCode"}, + "$statusCode\t==\n200": {"$statusCode", "==", "200"}, + } + for input, want := range cases { + tokens, err := tokenizeSimpleCondition(input) + require.NoError(t, err, input) + got := make([]string, len(tokens)) + for i, tok := range tokens { + got[i] = tok.raw + } + assert.Equal(t, want, got, input) + } +} + +func TestIsASCIISpace(t *testing.T) { + for _, c := range []byte(" \t\n\r\v\f") { + assert.True(t, isASCIISpace(c), "%q", c) + } + for _, c := range []byte{'a', '$', ')', 0x85, 0xA0} { + assert.False(t, isASCIISpace(c), "%q", c) + } +} + +// A runtime expression runs to whitespace, so an operator written directly +// after one is part of the expression: `$statusCode==200` is a single operand +// and fails to parse, as on main. A literal stops at an operator, so the +// operator may follow it without a space. +func TestEvaluateCriterion_OperatorsWithoutSpaces(t *testing.T) { + ctx := &expression.Context{StatusCode: 200} + + cases := []struct { + condition string + want bool + }{ + {"$statusCode ==200", true}, + {"200==$statusCode", true}, + {"201!=$statusCode", true}, + {"$statusCode == 200&&true", true}, + {"$statusCode == 201||true", true}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + ok, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, ctx) + require.NoError(t, err) + assert.Equal(t, tc.want, ok) + }) + } + + errorCases := []struct { + condition string + errText string + }{ + // The whole string is one runtime expression. + {"$statusCode==200", `unknown expression: "$statusCode==200"`}, + {"$statusCode&&true", `unknown expression: "$statusCode&&true"`}, + // The expression is `$statusCode==`; `200` is left over. + {"$statusCode== 200", `unexpected token "200"`}, + } + for _, tc := range errorCases { + t.Run(tc.condition, func(t *testing.T) { + _, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, ctx) + require.ErrorContains(t, err, tc.errText) + }) + } +} + +// A runtime expression runs to whitespace. Trailing ')' characters it does not +// balance itself close open groups first, one per open group; any left over +// stay in the pointer. Inside a group, a pointer ending in an unbalanced ')' +// is therefore only reachable as the group's last operand, with the closing +// ')' written directly after it. The body holds both "h" and "h)" so each case +// shows which key was read. +func TestEvaluateCriterion_PointerEndingInParen(t *testing.T) { + body := yamlMapping(t, map[string]any{"g)": 2, "h": true, "h)": false}) + ctx := &expression.Context{StatusCode: 200, ResponseBody: body} + + cases := []struct { + condition string + want bool + }{ + // Outside any group the ')' belongs to the pointer. + {"$response.body#/g) == 2", true}, + {"$response.body#/h) || $statusCode == 404", false}, + // One open group takes one trailing ')', so the pointer is /h. + {"($response.body#/h)", true}, + {"(($response.body#/h))", true}, + // A ')' beyond the open groups stays in the pointer, so it is /h). + {"($response.body#/h))", false}, + {"(($response.body#/h)))", false}, + {"(2 == $response.body#/g))", true}, + {"($statusCode == 200 && 2 == $response.body#/g))", true}, + {"($response.body#/h)) || $statusCode == 200", true}, + } + for _, tc := range cases { + t.Run(tc.condition, func(t *testing.T) { + ok, err := EvaluateCriterion(&high.Criterion{Condition: tc.condition}, ctx) + require.NoError(t, err) + assert.Equal(t, tc.want, ok) + }) + } + + // Followed by more of the group, the pointer's ')' closes the group instead, + // leaving the group's real ')' unmatched. + _, err := EvaluateCriterion(&high.Criterion{Condition: "($response.body#/g) == 2 )"}, ctx) + require.ErrorContains(t, err, `unexpected token ")"`) +}