Skip to content

Commit 1422336

Browse files
committed
fix: Support string literal values with spaces in simple conditions
This enables basic parsing of string literals as values in conditions, which in Arazzo are contained in single quote (') characters.
1 parent 297f241 commit 1422336

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

arazzo/criterion/condition.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,19 @@ func newCondition(rawCondition string) (*Condition, error) {
4444
return nil, fmt.Errorf("condition must at least be in the format [expression] [operator] [value]")
4545
}
4646

47-
if len(parts) > 3 || strings.ContainsAny(rawCondition, "&|") {
47+
if strings.ContainsAny(rawCondition, "&|") {
48+
// TODO this is a complex condition that we don't currently support
49+
return nil, nil
50+
}
51+
52+
// String literal value handling (single quotes) until parsing is tokenized.
53+
// Reference: https://spec.openapis.org/arazzo/v1.0.0#literals
54+
if len(parts) > 3 && strings.HasPrefix(parts[2], "'") && strings.HasSuffix(parts[len(parts)-1], "'") {
55+
parts[2] = strings.Join(parts[2:], " ")
56+
parts = parts[:3]
57+
}
58+
59+
if len(parts) > 3 {
4860
// TODO this is a complex condition that we don't currently support
4961
return nil, nil
5062
}

arazzo/criterion/condition_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package criterion
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/speakeasy-api/openapi/arazzo/expression"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestNewCondition(t *testing.T) {
12+
t.Parallel()
13+
14+
testCases := map[string]struct {
15+
raw string
16+
expected *Condition
17+
expectedError error
18+
}{
19+
"empty string": {
20+
raw: "",
21+
expected: nil,
22+
},
23+
"expression only": {
24+
raw: "$statusCode",
25+
expected: nil,
26+
expectedError: fmt.Errorf("condition must at least be in the format [expression] [operator] [value]"),
27+
},
28+
"expression and operator only": {
29+
raw: "$statusCode ==",
30+
expected: nil,
31+
expectedError: fmt.Errorf("condition must at least be in the format [expression] [operator] [value]"),
32+
},
33+
"$statusCode == 200": {
34+
raw: "$statusCode == 200",
35+
expected: &Condition{
36+
Expression: expression.Expression("$statusCode"),
37+
Operator: OperatorEQ,
38+
Value: "200",
39+
},
40+
},
41+
"$response.body#/test == 'string literal with spaces'": {
42+
raw: "$response.body#/test == 'string literal with spaces'",
43+
expected: &Condition{
44+
Expression: expression.Expression("$response.body#/test"),
45+
Operator: OperatorEQ,
46+
Value: "'string literal with spaces'",
47+
},
48+
},
49+
}
50+
51+
for testName, testCase := range testCases {
52+
t.Run(testName, func(t *testing.T) {
53+
t.Parallel()
54+
55+
actual, actualError := newCondition(testCase.raw)
56+
57+
if testCase.expectedError != nil {
58+
assert.EqualError(t, actualError, testCase.expectedError.Error())
59+
}
60+
61+
assert.EqualExportedValues(t, testCase.expected, actual)
62+
})
63+
}
64+
}

0 commit comments

Comments
 (0)