Skip to content

Commit 668c17e

Browse files
fix: fixed issue with core models not being updated on syncing changes from newly created models
1 parent 03a52fd commit 668c17e

File tree

16 files changed

+316
-127
lines changed

16 files changed

+316
-127
lines changed

arazzo/arazzo.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,19 @@ func (a *Arazzo) GetCore() *core.Arazzo {
123123
return &a.core
124124
}
125125

126+
// Sync will sync any changes made to the Arazzo document models back to the core models.
127+
func (a *Arazzo) Sync(ctx context.Context) error {
128+
if _, err := marshaller.SyncValue(ctx, a, &a.core, nil, false); err != nil {
129+
return err
130+
}
131+
return nil
132+
}
133+
126134
// Marshal will marshal the Arazzo document to the provided io.Writer.
127135
func (a *Arazzo) Marshal(ctx context.Context, w io.Writer) error {
128136
ctx = yml.ContextWithConfig(ctx, a.core.Config)
129137

130-
if _, err := marshaller.SyncValue(ctx, a, &a.core, nil); err != nil {
138+
if _, err := marshaller.SyncValue(ctx, a, &a.core, nil, false); err != nil {
131139
return err
132140
}
133141

arazzo/arazzo_test.go

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ var testArazzoInstance = &arazzo.Arazzo{
179179
Type: arazzo.FailureActionTypeRetry,
180180
RetryAfter: pointer.From(10.0),
181181
RetryLimit: pointer.From(3),
182-
Criteria: []criterion.Criterion{{Condition: "$statusCode == 500", Type: criterion.CriterionTypeUnion{
182+
Criteria: []criterion.Criterion{{Context: pointer.From(expression.Expression("$statusCode")), Condition: "$statusCode == 500", Type: criterion.CriterionTypeUnion{
183183
Type: pointer.From(criterion.CriterionTypeSimple),
184184
}}},
185185
Valid: true,
@@ -320,6 +320,9 @@ func TestArazzo_Mutate_Success(t *testing.T) {
320320
err = arazzo.Marshal(ctx, a, outBuf)
321321
require.NoError(t, err)
322322

323+
errs := a.Validate(ctx)
324+
require.Empty(t, errs)
325+
323326
assert.Equal(t, `arazzo: 1.0.0
324327
info:
325328
title: My updated workflow title
@@ -383,17 +386,23 @@ components:
383386
retryAfter: 10
384387
retryLimit: 3
385388
criteria:
386-
- condition: $statusCode == 500
389+
- context: $statusCode
390+
condition: $statusCode == 500
387391
x-test: some-value
388392
`, outBuf.String())
389393
}
390394

391395
func TestArazzo_Create_Success(t *testing.T) {
392396
outBuf := bytes.NewBuffer([]byte{})
393397

394-
err := arazzo.Marshal(context.Background(), testArazzoInstance, outBuf)
398+
ctx := context.Background()
399+
400+
err := arazzo.Marshal(ctx, testArazzoInstance, outBuf)
395401
require.NoError(t, err)
396402

403+
errs := testArazzoInstance.Validate(ctx)
404+
require.Empty(t, errs)
405+
397406
data, err := os.ReadFile("testdata/test.arazzo.yaml")
398407
require.NoError(t, err)
399408

@@ -561,15 +570,66 @@ var stressTests = []struct {
561570
wantTitle: "",
562571
},
563572
{
564-
name: "DevAttila87 Example",
573+
name: "Itarazzo Library Example",
565574
args: args{
566-
location: "https://raw.githubusercontent.com/devAttila87/arazzo/24dd4c896f98b942e61831f3529fe538089baedf/application-integration-test/src/test/resources/arazzo.yaml",
575+
location: "https://raw.githubusercontent.com/leidenheit/itarazzo-library/3b335e1c4293444add52b5f2476420e2d871b1a5/src/test/resources/test.arazzo.yaml",
567576
validationIgnores: []string{
568-
"only one of operationId, operationPath or workflowId can be set", // legit issue
577+
"expression is not valid, must begin with $: <root><id>4711</id><name>Chocolate</name></root>", // legit issue
569578
},
570579
},
571580
wantTitle: "A cookie eating workflow",
572581
},
582+
{
583+
name: "Itarazzo Client Pet Store Example",
584+
args: args{
585+
location: "https://raw.githubusercontent.com/leidenheit/itarazzo-client/b744ca1ca3a036964ae30be601f10a25b14dc52d/src/test/resources/pet-store.arazzo.yaml",
586+
validationIgnores: []string{
587+
"jsonpointer must start with /: $.status", // legit issues TODO: improve the error returned as it is wrong
588+
"jsonpointer must start with /: $.id", // legit issues TODO: improve the error returned as it is wrong
589+
},
590+
},
591+
wantTitle: "PetStore - Example of Workflows",
592+
},
593+
{
594+
name: "Ritza build-a-bot workflow",
595+
args: args{
596+
location: "https://raw.githubusercontent.com/ritza-co/e2e-testing-arazzo/c0615c3708a1e4c0fcaeb79edae78ddc4eb5ba82/arazzo.yaml",
597+
validationIgnores: []string{},
598+
},
599+
wantTitle: "Build-a-Bot Workflow",
600+
},
601+
{
602+
name: " API-Flows adyen-giving workflow",
603+
args: args{
604+
location: "https://raw.githubusercontent.com/API-Flows/openapi-workflow-registry/3d85d79232fa8f42993b2f5bd47e273b9369dc2d/root/adyen/adyen-giving.yaml",
605+
validationIgnores: []string{
606+
"in must be one of [path, query, header, cookie] but was body",
607+
},
608+
},
609+
wantTitle: "Adyen Giving",
610+
},
611+
{
612+
name: "API-Flows simple workflow",
613+
args: args{
614+
location: "https://raw.githubusercontent.com/API-Flows/openapi-workflow-parser/6b28ba4def262969c5a96bc54d08433e6c336643/src/test/resources/1.0.0/simple.workflow.yaml",
615+
validationIgnores: []string{},
616+
},
617+
wantTitle: "simple",
618+
},
619+
// Disabled for now as it is currently failing round tripping due to missing conditions
620+
// {
621+
// name: "Kartikhub swap tokens workflow",
622+
// args: args{
623+
// location: "https://raw.githubusercontent.com/Kartikhub/web3-basics/d95bc51bb935ef07d627e52c6fdfe18aaea69e18/swap-react/docs/swap-transaction-arazzo.yaml",
624+
// validationIgnores: []string{ // All valid issues
625+
// "field condition is missing",
626+
// "condition is required",
627+
// "field value is missing",
628+
// "expression is not valid, must begin with $",
629+
// },
630+
// },
631+
// wantTitle: "Swap Tokens",
632+
// },
573633
}
574634

575635
func TestArazzo_StressTests_Validate(t *testing.T) {

arazzo/core/arazzo.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78

@@ -32,6 +33,10 @@ func Unmarshal(ctx context.Context, doc io.Reader) (*Arazzo, error) {
3233
return nil, fmt.Errorf("failed to read Arazzo document: %w", err)
3334
}
3435

36+
if len(data) == 0 {
37+
return nil, errors.New("empty document")
38+
}
39+
3540
var root yaml.Node
3641
if err := yaml.Unmarshal(data, &root); err != nil {
3742
return nil, fmt.Errorf("failed to unmarshal Arazzo document: %w", err)

arazzo/core/criterion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ func (c *CriterionTypeUnion) SyncChanges(ctx context.Context, model any, valueNo
6464
tf := mv.FieldByName("Type")
6565
ef := mv.FieldByName("ExpressionType")
6666

67-
tv, err := marshaller.SyncValue(ctx, tf.Interface(), &c.Type, valueNode)
67+
tv, err := marshaller.SyncValue(ctx, tf.Interface(), &c.Type, valueNode, false)
6868
if err != nil {
6969
return nil, err
7070
}
7171

72-
ev, err := marshaller.SyncValue(ctx, ef.Interface(), &c.ExpressionType, valueNode)
72+
ev, err := marshaller.SyncValue(ctx, ef.Interface(), &c.ExpressionType, valueNode, false)
7373
if err != nil {
7474
return nil, err
7575
}

arazzo/core/reusable.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,14 @@ func (r *Reusable[T]) SyncChanges(ctx context.Context, model any, valueNode *yam
5858

5959
of := mv.FieldByName("Object")
6060
if of.IsZero() {
61-
type reusable[T any] struct {
62-
Reference marshaller.Node[*Expression] `key:"reference"`
63-
Value marshaller.Node[Value] `key:"value"`
64-
65-
RootNode *yaml.Node
66-
}
67-
68-
rl := reusable[T]{
69-
Reference: r.Reference,
70-
Value: r.Value,
71-
RootNode: r.RootNode,
72-
}
73-
7461
var err error
75-
valueNode, err = marshaller.SyncValue(ctx, model, &rl, valueNode)
62+
valueNode, err = marshaller.SyncValue(ctx, model, r, valueNode, true)
7663
if err != nil {
7764
return nil, err
7865
}
7966
} else {
8067
var err error
81-
valueNode, err = marshaller.SyncValue(ctx, of.Interface(), &r.Object, valueNode)
68+
valueNode, err = marshaller.SyncValue(ctx, of.Interface(), &r.Object, valueNode, false)
8269
if err != nil {
8370
return nil, err
8471
}

arazzo/criterion/criterion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ func (c *Criterion) Validate(opts ...validation.Option) []error {
263263
func (c *Criterion) validateCondition(opts ...validation.Option) []error {
264264
errs := []error{}
265265

266-
conditionLine := c.core.Condition.GetValueNodeOrRoot(c.core.RootNode).Line
267-
conditionColumn := c.core.Condition.GetValueNodeOrRoot(c.core.RootNode).Column
266+
conditionLine := c.GetCore().Condition.GetValueNodeOrRoot(c.GetCore().RootNode).Line
267+
conditionColumn := c.GetCore().Condition.GetValueNodeOrRoot(c.GetCore().RootNode).Column
268268

269269
switch c.Type.GetType() {
270270
case CriterionTypeSimple:

arazzo/parameter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (p *Parameter) Validate(ctx context.Context, opts ...validation.Option) []e
9191

9292
if in != "" {
9393
errs = append(errs, &validation.Error{
94-
Message: fmt.Sprintf("in must be one of [%s]", strings.Join([]string{string(InPath), string(InQuery), string(InHeader), string(InCookie)}, ", ")),
94+
Message: fmt.Sprintf("in must be one of [%s] but was %s", strings.Join([]string{string(InPath), string(InQuery), string(InHeader), string(InCookie)}, ", "), in),
9595
Line: p.core.In.GetValueNodeOrRoot(p.core.RootNode).Line,
9696
Column: p.core.In.GetValueNodeOrRoot(p.core.RootNode).Column,
9797
})

arazzo/testdata/speakeasybar.arazzo.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ workflows:
7676
operationId: createOrder
7777
parameters:
7878
- name: orderType
79-
in: body
79+
in: query
8080
value: $inputs.orderType
8181
- name: productCode
82-
in: body
82+
in: query
8383
value: $inputs.productCode
8484
- name: quantity
85-
in: body
85+
in: query
8686
value: $inputs.quantity
8787
outputs:
8888
orderNumber: $response.body#/orderNumber
@@ -176,9 +176,9 @@ components:
176176
value: $steps.authenticate.outputs.token
177177
username:
178178
name: username
179-
in: body
179+
in: query
180180
value: $inputs.username
181181
password:
182182
name: password
183-
in: body
183+
in: query
184184
value: $inputs.password

arazzo/testdata/test.arazzo.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ components:
6868
retryAfter: 10
6969
retryLimit: 3
7070
criteria:
71-
- condition: $statusCode == 500%s
71+
- context: $statusCode
72+
condition: $statusCode == 500%s
7273
x-test: some-value

jsonschema/oas31/core/value.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ func (v *EitherValue[L, R]) SyncChanges(ctx context.Context, model any, valueNod
5353
lf := mv.FieldByName("Left")
5454
rf := mv.FieldByName("Right")
5555

56-
lv, err := marshaller.SyncValue(ctx, lf.Interface(), &v.Left, valueNode)
56+
lv, err := marshaller.SyncValue(ctx, lf.Interface(), &v.Left, valueNode, false)
5757
if err != nil {
5858
return nil, err
5959
}
6060

61-
rv, err := marshaller.SyncValue(ctx, rf.Interface(), &v.Right, valueNode)
61+
rv, err := marshaller.SyncValue(ctx, rf.Interface(), &v.Right, valueNode, false)
6262
if err != nil {
6363
return nil, err
6464
}

0 commit comments

Comments
 (0)