Skip to content

Commit 92ded3c

Browse files
Merge pull request #9 from speakeasy-api/fix-condition-parsing
2 parents 03cd5ce + 543617c commit 92ded3c

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

arazzo/criterion/condition.go

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ type Condition struct {
3838

3939
// TODO this will need to evolve to have a more AST like structure (while remaining easy to work with)
4040
func newCondition(rawCondition string) (*Condition, error) {
41+
// This is a raw value not a condition expressions
42+
if !strings.HasPrefix(rawCondition, "$") {
43+
return nil, nil
44+
}
45+
4146
parts := strings.Split(rawCondition, " ")
4247

4348
if len(parts) < 3 {

arazzo/criterion/criterion.go

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package criterion
22

33
import (
4+
"context"
45
"fmt"
56
"strings"
67

@@ -199,6 +200,14 @@ func (c *Criterion) GetCore() *core.Criterion {
199200
return &c.core
200201
}
201202

203+
// Sync will sync any changes made to the Arazzo document models back to the core models.
204+
func (c *Criterion) Sync(ctx context.Context) error {
205+
if _, err := marshaller.SyncValue(ctx, c, &c.core, nil, false); err != nil {
206+
return err
207+
}
208+
return nil
209+
}
210+
202211
// GetCondition will return the condition as a parsed condition object
203212
func (c *Criterion) GetCondition() (*Condition, error) {
204213
return newCondition(c.Condition)

arazzo/criterion/criterion_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package criterion_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/speakeasy-api/openapi/arazzo/criterion"
8+
"github.com/speakeasy-api/openapi/arazzo/expression"
9+
"github.com/speakeasy-api/openapi/pointer"
10+
"github.com/speakeasy-api/openapi/validation"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestCriterion_Validate_Success(t *testing.T) {
16+
type args struct {
17+
c *criterion.Criterion
18+
opts []validation.Option
19+
}
20+
tests := []struct {
21+
name string
22+
args args
23+
}{
24+
{
25+
name: "successfully validate criterion with empty json object condition",
26+
args: args{
27+
c: &criterion.Criterion{
28+
Context: pointer.From(expression.Expression("$response.body")),
29+
Type: criterion.CriterionTypeUnion{
30+
Type: pointer.From(criterion.CriterionTypeSimple),
31+
},
32+
Condition: `
33+
[
34+
{}
35+
]`,
36+
},
37+
},
38+
},
39+
}
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
err := tt.args.c.Sync(context.Background())
43+
require.NoError(t, err)
44+
errs := tt.args.c.Validate(tt.args.opts...)
45+
assert.Empty(t, errs)
46+
})
47+
}
48+
}

arazzo/expression/expression_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ func TestExpression_Validate_Success(t *testing.T) {
194194
validateAsExpression: true,
195195
},
196196
},
197+
{
198+
name: "multiline empty json objects expression",
199+
args: args{
200+
e: Expression(`
201+
[
202+
{}
203+
]`),
204+
validateAsExpression: false,
205+
},
206+
},
197207
}
198208
for _, tt := range tests {
199209
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)