Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed parsing of value only conditions #9

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading