From 543617cc5e5a5acf2f2e251b0ed83a68d8d109fb Mon Sep 17 00:00:00 2001 From: Tristan Cartledge Date: Fri, 17 Jan 2025 14:20:04 +1000 Subject: [PATCH] fix: fixed parsing of value only conditions --- arazzo/criterion/condition.go | 5 +++ arazzo/criterion/criterion.go | 9 ++++++ arazzo/criterion/criterion_test.go | 48 ++++++++++++++++++++++++++++ arazzo/expression/expression_test.go | 10 ++++++ 4 files changed, 72 insertions(+) create mode 100644 arazzo/criterion/criterion_test.go diff --git a/arazzo/criterion/condition.go b/arazzo/criterion/condition.go index 01a800e..5783649 100644 --- a/arazzo/criterion/condition.go +++ b/arazzo/criterion/condition.go @@ -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 { diff --git a/arazzo/criterion/criterion.go b/arazzo/criterion/criterion.go index 301e932..55dfda4 100644 --- a/arazzo/criterion/criterion.go +++ b/arazzo/criterion/criterion.go @@ -1,6 +1,7 @@ package criterion import ( + "context" "fmt" "strings" @@ -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) diff --git a/arazzo/criterion/criterion_test.go b/arazzo/criterion/criterion_test.go new file mode 100644 index 0000000..992bc65 --- /dev/null +++ b/arazzo/criterion/criterion_test.go @@ -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) + }) + } +} diff --git a/arazzo/expression/expression_test.go b/arazzo/expression/expression_test.go index d65f751..090d70e 100644 --- a/arazzo/expression/expression_test.go +++ b/arazzo/expression/expression_test.go @@ -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) {