Skip to content

Commit

Permalink
Merge pull request #9 from speakeasy-api/fix-condition-parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy authored Jan 17, 2025
2 parents 03cd5ce + 543617c commit 92ded3c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions arazzo/criterion/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type Condition struct {

// TODO this will need to evolve to have a more AST like structure (while remaining easy to work with)
func newCondition(rawCondition string) (*Condition, error) {
// This is a raw value not a condition expressions
if !strings.HasPrefix(rawCondition, "$") {
return nil, nil
}

parts := strings.Split(rawCondition, " ")

if len(parts) < 3 {
Expand Down
9 changes: 9 additions & 0 deletions arazzo/criterion/criterion.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package criterion

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -199,6 +200,14 @@ func (c *Criterion) GetCore() *core.Criterion {
return &c.core
}

// Sync will sync any changes made to the Arazzo document models back to the core models.
func (c *Criterion) Sync(ctx context.Context) error {
if _, err := marshaller.SyncValue(ctx, c, &c.core, nil, false); err != nil {
return err
}
return nil
}

// GetCondition will return the condition as a parsed condition object
func (c *Criterion) GetCondition() (*Condition, error) {
return newCondition(c.Condition)
Expand Down
48 changes: 48 additions & 0 deletions arazzo/criterion/criterion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package criterion_test

import (
"context"
"testing"

"github.com/speakeasy-api/openapi/arazzo/criterion"
"github.com/speakeasy-api/openapi/arazzo/expression"
"github.com/speakeasy-api/openapi/pointer"
"github.com/speakeasy-api/openapi/validation"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCriterion_Validate_Success(t *testing.T) {
type args struct {
c *criterion.Criterion
opts []validation.Option
}
tests := []struct {
name string
args args
}{
{
name: "successfully validate criterion with empty json object condition",
args: args{
c: &criterion.Criterion{
Context: pointer.From(expression.Expression("$response.body")),
Type: criterion.CriterionTypeUnion{
Type: pointer.From(criterion.CriterionTypeSimple),
},
Condition: `
[
{}
]`,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.args.c.Sync(context.Background())
require.NoError(t, err)
errs := tt.args.c.Validate(tt.args.opts...)
assert.Empty(t, errs)
})
}
}
10 changes: 10 additions & 0 deletions arazzo/expression/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ func TestExpression_Validate_Success(t *testing.T) {
validateAsExpression: true,
},
},
{
name: "multiline empty json objects expression",
args: args{
e: Expression(`
[
{}
]`),
validateAsExpression: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 92ded3c

Please sign in to comment.