-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathapply_test.go
More file actions
931 lines (827 loc) · 20.8 KB
/
Copy pathapply_test.go
File metadata and controls
931 lines (827 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
package overlay_test
import (
"bytes"
"os"
"strconv"
"testing"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath"
"github.com/speakeasy-api/openapi/overlay"
"github.com/speakeasy-api/openapi/overlay/loader"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
// NodeMatchesFile is a test that marshals the YAML file from the given node,
// then compares those bytes to those found in the expected file.
func NodeMatchesFile(
t *testing.T,
actual *yaml.Node,
expectedFile string,
msgAndArgs ...any,
) {
t.Helper()
variadoc := func(pre ...any) []any { return append(msgAndArgs, pre...) }
var actualBuf bytes.Buffer
enc := yaml.NewEncoder(&actualBuf)
enc.SetIndent(2)
err := enc.Encode(actual)
require.NoError(t, err, variadoc("failed to marshal node: ")...)
expectedBytes, err := os.ReadFile(expectedFile)
require.NoError(t, err, variadoc("failed to read expected file: ")...)
// lazy redo snapshot
// os.WriteFile(expectedFile, actualBuf.Bytes(), 0644)
// t.Log("### EXPECT START ###\n" + string(expectedBytes) + "\n### EXPECT END ###\n")
// t.Log("### ACTUAL START ###\n" + actualBuf.string() + "\n### ACTUAL END ###\n")
assert.Equal(t, string(expectedBytes), actualBuf.String(), variadoc("node does not match expected file: ")...)
}
func TestApplyTo(t *testing.T) {
t.Parallel()
node, err := loader.LoadSpecification("testdata/openapi.yaml")
require.NoError(t, err)
o, err := loader.LoadOverlay("testdata/overlay.yaml")
require.NoError(t, err)
err = o.ApplyTo(node)
require.NoError(t, err)
NodeMatchesFile(t, node, "testdata/openapi-overlayed.yaml")
}
func TestApplyToStrict(t *testing.T) {
t.Parallel()
node, err := loader.LoadSpecification("testdata/openapi.yaml")
require.NoError(t, err)
o, err := loader.LoadOverlay("testdata/overlay-mismatched.yaml")
require.NoError(t, err)
warnings, err := o.ApplyToStrict(node)
require.Error(t, err, "error applying overlay (strict): selector \"$.unknown-attribute\" did not match any targets")
assert.Len(t, warnings, 2)
o.Actions = o.Actions[1:]
node, err = loader.LoadSpecification("testdata/openapi.yaml")
require.NoError(t, err)
warnings, err = o.ApplyToStrict(node)
require.NoError(t, err)
assert.Len(t, warnings, 1)
assert.Equal(t, "update action (2 / 2) target=$.info.title: does nothing", warnings[0])
NodeMatchesFile(t, node, "testdata/openapi-strict-onechange.yaml")
node, err = loader.LoadSpecification("testdata/openapi.yaml")
require.NoError(t, err)
o, err = loader.LoadOverlay("testdata/overlay.yaml")
require.NoError(t, err)
err = o.ApplyTo(node)
require.NoError(t, err)
NodeMatchesFile(t, node, "testdata/openapi-overlayed.yaml")
}
func BenchmarkApplyToStrict(b *testing.B) {
openAPIBytes, err := os.ReadFile("testdata/openapi.yaml")
require.NoError(b, err)
overlayBytes, err := os.ReadFile("testdata/overlay-zero-change.yaml")
require.NoError(b, err)
var specNode yaml.Node
err = yaml.NewDecoder(bytes.NewReader(openAPIBytes)).Decode(&specNode)
require.NoError(b, err)
// Load overlay from bytes
var o overlay.Overlay
err = yaml.NewDecoder(bytes.NewReader(overlayBytes)).Decode(&o)
require.NoError(b, err)
// Apply overlay to spec
for b.Loop() {
_, _ = o.ApplyToStrict(&specNode)
}
}
func BenchmarkApplyToStrictBySize(b *testing.B) {
// Read the base OpenAPI spec
openAPIBytes, err := os.ReadFile("testdata/openapi.yaml")
require.NoError(b, err)
// Read the overlay spec
overlayBytes, err := os.ReadFile("testdata/overlay-zero-change.yaml")
require.NoError(b, err)
// Decode the base spec
var baseSpec yaml.Node
err = yaml.NewDecoder(bytes.NewReader(openAPIBytes)).Decode(&baseSpec)
require.NoError(b, err)
// Find the paths node and a path to duplicate
pathsNode := findPathsNode(&baseSpec)
require.NotNil(b, pathsNode)
// Get the first path item to use as template
var templatePath *yaml.Node
var templateKey string
for i := 0; i < len(pathsNode.Content); i += 2 {
if pathsNode.Content[i].Kind == yaml.ScalarNode && pathsNode.Content[i].Value[0] == '/' {
templateKey = pathsNode.Content[i].Value
templatePath = pathsNode.Content[i+1]
break
}
}
require.NotNil(b, templatePath)
// Target sizes: 2KB, 20KB, 200KB, 2MB, 20MB
targetSizes := []struct {
size int
name string
}{
{2 * 1024, "2KB"},
{20 * 1024, "20KB"},
{200 * 1024, "200KB"},
{2000 * 1024, "2M"},
}
// Calculate the base document size
var baseBuf bytes.Buffer
enc := yaml.NewEncoder(&baseBuf)
err = enc.Encode(&baseSpec)
require.NoError(b, err)
baseSize := baseBuf.Len()
// Calculate the size of a single path item by encoding it
var pathBuf bytes.Buffer
pathEnc := yaml.NewEncoder(&pathBuf)
tempNode := &yaml.Node{
Kind: yaml.MappingNode,
Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: templateKey + "-test"},
cloneNode(templatePath),
},
}
err = pathEnc.Encode(tempNode)
require.NoError(b, err)
// Approximate size contribution of one path (accounting for YAML structure)
pathItemSize := pathBuf.Len() - 10 // Subtract some overhead
for _, target := range targetSizes {
b.Run(target.name, func(b *testing.B) {
// Create a copy of the base spec
specCopy := cloneNode(&baseSpec)
pathsNodeCopy := findPathsNode(specCopy)
// Calculate how many paths we need to add
bytesNeeded := target.size - baseSize
pathsToAdd := 0
if bytesNeeded > 0 {
pathsToAdd = bytesNeeded / pathItemSize
// Add a few extra to ensure we exceed the target
pathsToAdd += 5
}
// Add the calculated number of path duplicates
for i := 0; i < pathsToAdd; i++ {
newPathKey := yaml.Node{Kind: yaml.ScalarNode, Value: templateKey + "-duplicate-" + strconv.Itoa(i)}
newPathValue := cloneNode(templatePath)
pathsNodeCopy.Content = append(pathsNodeCopy.Content, &newPathKey, newPathValue)
}
// Verify final size
var finalBuf bytes.Buffer
finalEnc := yaml.NewEncoder(&finalBuf)
err = finalEnc.Encode(specCopy)
require.NoError(b, err)
actualSize := finalBuf.Len()
b.Logf("OpenAPI size: %d bytes (target: %d, paths added: %d)", actualSize, target.size, pathsToAdd)
// Load overlay
var o overlay.Overlay
err = yaml.NewDecoder(bytes.NewReader(overlayBytes)).Decode(&o)
require.NoError(b, err)
specForTest := cloneNode(specCopy)
// Run the benchmark
b.ResetTimer()
for b.Loop() {
_, _ = o.ApplyToStrict(specForTest)
}
})
}
}
// Helper function to find the paths node in the OpenAPI spec
func findPathsNode(node *yaml.Node) *yaml.Node {
if node.Kind == yaml.DocumentNode && len(node.Content) > 0 {
node = node.Content[0]
}
if node.Kind != yaml.MappingNode {
return nil
}
for i := 0; i < len(node.Content); i += 2 {
if node.Content[i].Value == "paths" {
return node.Content[i+1]
}
}
return nil
}
// Helper function to deep clone a YAML node
func cloneNode(node *yaml.Node) *yaml.Node {
if node == nil {
return nil
}
clone := &yaml.Node{
Kind: node.Kind,
Style: node.Style,
Tag: node.Tag,
Value: node.Value,
Anchor: node.Anchor,
Alias: node.Alias,
HeadComment: node.HeadComment,
LineComment: node.LineComment,
FootComment: node.FootComment,
Line: node.Line,
Column: node.Column,
}
if node.Content != nil {
clone.Content = make([]*yaml.Node, len(node.Content))
for i, child := range node.Content {
clone.Content[i] = cloneNode(child)
}
}
return clone
}
func TestApplyTo_CopyVersionToHeader(t *testing.T) {
t.Parallel()
node, err := loader.LoadSpecification("testdata/openapi-version-header.yaml")
require.NoError(t, err)
o, err := loader.LoadOverlay("testdata/overlay-version-header.yaml")
require.NoError(t, err)
err = o.ApplyTo(node)
require.NoError(t, err)
NodeMatchesFile(t, node, "testdata/openapi-version-header-expected.yaml")
}
// applyOverlay is a test helper that unmarshals the input/overlay YAML,
// applies the overlay (lax or strict), and returns the result.
func applyOverlay(t *testing.T, inputYAML, overlayYAML string) *yaml.Node {
t.Helper()
var specNode yaml.Node
err := yaml.Unmarshal([]byte(inputYAML), &specNode)
require.NoError(t, err, "unmarshal input spec should succeed")
var o overlay.Overlay
err = yaml.Unmarshal([]byte(overlayYAML), &o)
require.NoError(t, err, "unmarshal overlay should succeed")
err = o.ApplyTo(&specNode)
require.NoError(t, err, "apply overlay should succeed")
return &specNode
}
// applyOverlayStrict is a test helper that applies in strict mode.
func applyOverlayStrict(t *testing.T, inputYAML, overlayYAML string) ([]string, error) {
t.Helper()
var specNode yaml.Node
err := yaml.Unmarshal([]byte(inputYAML), &specNode)
require.NoError(t, err, "unmarshal input spec should succeed")
var o overlay.Overlay
err = yaml.Unmarshal([]byte(overlayYAML), &o)
require.NoError(t, err, "unmarshal overlay should succeed")
return o.ApplyToStrict(&specNode)
}
// marshalNode is a test helper that marshals a YAML node to a string.
func marshalNode(t *testing.T, node *yaml.Node) string {
t.Helper()
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
err := enc.Encode(node)
require.NoError(t, err, "encode result should succeed")
return buf.String()
}
// --- 1.0.0 Tests: Preserve existing behavior ---
func TestApplyTo_V100_TypeMismatchReplace(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputYAML string
overlayYAML string
expectedYAML string
}{
{
name: "object replaces scalar",
inputYAML: `root:
key: value
`,
overlayYAML: `overlay: 1.0.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root.key"
update:
nested: object
`,
expectedYAML: `root:
key:
nested: object
`,
},
{
name: "scalar replaces object",
inputYAML: `root:
key:
nested: object
`,
overlayYAML: `overlay: 1.0.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root.key"
update: simple
`,
expectedYAML: `root:
key: simple
`,
},
{
name: "array replaces object",
inputYAML: `root:
key:
a: 1
`,
overlayYAML: `overlay: 1.0.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root.key"
update:
- item1
- item2
`,
expectedYAML: `root:
key:
- item1
- item2
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := applyOverlay(t, tt.inputYAML, tt.overlayYAML)
assert.Equal(t, tt.expectedYAML, marshalNode(t, result), "output should match expected YAML")
})
}
}
func TestApplyTo_V100_ArrayConcatenation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputYAML string
overlayYAML string
expectedYAML string
}{
{
name: "top-level array target with array update concatenates",
inputYAML: `items:
- a
- b
`,
overlayYAML: `overlay: 1.0.0
info:
title: test
version: 1.0.0
actions:
- target: "$.items"
update:
- c
- d
`,
expectedYAML: `items:
- a
- b
- c
- d
`,
},
{
name: "nested array in object merge concatenates",
inputYAML: `root:
list:
- existing
`,
overlayYAML: `overlay: 1.0.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root"
update:
list:
- new
`,
expectedYAML: `root:
list:
- existing
- new
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := applyOverlay(t, tt.inputYAML, tt.overlayYAML)
assert.Equal(t, tt.expectedYAML, marshalNode(t, result), "output should match expected YAML")
})
}
}
// --- 1.1.0 Tests: New spec-compliant behavior ---
func TestApplyTo_V110_TopLevelArrayAppend(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputYAML string
overlayYAML string
expectedYAML string
}{
{
name: "object appended to array as single element",
inputYAML: `tags:
- name: existing
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.tags"
update:
name: newTag
description: appended
`,
expectedYAML: `tags:
- name: existing
- name: newTag
description: appended
`,
},
{
name: "scalar appended to array as single element",
inputYAML: `tags:
- pets
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.tags"
update: admin
`,
expectedYAML: `tags:
- pets
- admin
`,
},
{
name: "array update concatenated with array target",
inputYAML: `tags:
- name: existing
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.tags"
update:
- name: new1
- name: new2
`,
expectedYAML: `tags:
- name: existing
- name: new1
- name: new2
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := applyOverlay(t, tt.inputYAML, tt.overlayYAML)
assert.Equal(t, tt.expectedYAML, marshalNode(t, result), "output should match expected YAML")
})
}
}
func TestApplyTo_V110_TopLevelObjectMerge(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputYAML string
overlayYAML string
expectedYAML string
}{
{
name: "new keys added to object",
inputYAML: `info:
title: Original
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.info"
update:
description: Added
`,
expectedYAML: `info:
title: Original
description: Added
`,
},
{
name: "existing key recursively merged",
inputYAML: `info:
title: Original
contact:
name: Old
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.info"
update:
contact:
email: new@example.com
`,
expectedYAML: `info:
title: Original
contact:
name: Old
email: new@example.com
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := applyOverlay(t, tt.inputYAML, tt.overlayYAML)
assert.Equal(t, tt.expectedYAML, marshalNode(t, result), "output should match expected YAML")
})
}
}
func TestApplyTo_V110_TopLevelPrimitiveReplace(t *testing.T) {
t.Parallel()
inputYAML := `info:
title: Original
`
overlayYAML := `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.info.title"
update: Replaced
`
expectedYAML := `info:
title: Replaced
`
result := applyOverlay(t, inputYAML, overlayYAML)
assert.Equal(t, expectedYAML, marshalNode(t, result), "scalar should be replaced")
}
func TestApplyTo_V110_RecursiveArrayConcat(t *testing.T) {
t.Parallel()
inputYAML := `paths:
/pets:
get:
parameters:
- name: limit
in: query
`
overlayYAML := `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.paths['/pets'].get"
update:
parameters:
- name: offset
in: query
`
expectedYAML := `paths:
/pets:
get:
parameters:
- name: limit
in: query
- name: offset
in: query
`
result := applyOverlay(t, inputYAML, overlayYAML)
assert.Equal(t, expectedYAML, marshalNode(t, result), "nested arrays should be concatenated during object merge")
}
func TestApplyTo_V110_RecursiveTypeMismatch_Strict(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputYAML string
overlayYAML string
errContains string
}{
{
name: "array target with object update within recursive merge",
inputYAML: `root:
key:
- item1
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root"
update:
key:
nested: value
`,
errContains: `key "key": type mismatch: target is array but update is object`,
},
{
name: "scalar target with array update within recursive merge",
inputYAML: `root:
key: scalar
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root"
update:
key:
- item1
`,
errContains: `key "key": type mismatch: target is scalar but update is array`,
},
{
name: "scalar target with object update within recursive merge",
inputYAML: `root:
key: scalar
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root"
update:
key:
nested: value
`,
errContains: `key "key": type mismatch: target is scalar but update is object`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, err := applyOverlayStrict(t, tt.inputYAML, tt.overlayYAML)
require.Error(t, err, "strict mode should return error on recursive type mismatch")
assert.Contains(t, err.Error(), tt.errContains, "error should describe the mismatch")
})
}
}
func TestApplyTo_V110_RecursiveTypeMismatch_Lax(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputYAML string
overlayYAML string
expectedYAML string
}{
{
name: "array target with object update replaces gracefully",
inputYAML: `root:
key:
- item1
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root"
update:
key:
nested: value
`,
expectedYAML: `root:
key:
nested: value
`,
},
{
name: "scalar target with object update replaces gracefully",
inputYAML: `root:
key: scalar
`,
overlayYAML: `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root"
update:
key:
nested: value
`,
expectedYAML: `root:
key:
nested: value
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := applyOverlay(t, tt.inputYAML, tt.overlayYAML)
assert.Equal(t, tt.expectedYAML, marshalNode(t, result), "lax mode should replace gracefully")
})
}
}
func TestApplyTo_V110_CopyArrayAppend(t *testing.T) {
t.Parallel()
inputYAML := `source:
name: copied
description: from source
targets:
- name: existing
`
overlayYAML := `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.targets"
copy: "$.source"
`
result := applyOverlay(t, inputYAML, overlayYAML)
expectedYAML := `source:
name: copied
description: from source
targets:
- name: existing
- name: copied
description: from source
`
assert.Equal(t, expectedYAML, marshalNode(t, result), "copy into array should append the object")
}
func TestApplyTo_V110_HomogeneityCheck_Strict(t *testing.T) {
t.Parallel()
// This test uses a wildcard that selects nodes of different types.
// In 1.1.0 strict mode, this should error.
inputYAML := `root:
arrayKey:
- item1
objectKey:
nested: value
`
overlayYAML := `overlay: 1.1.0
info:
title: test
version: 1.0.0
actions:
- target: "$.root.*"
update:
newKey: newValue
`
_, err := applyOverlayStrict(t, inputYAML, overlayYAML)
require.Error(t, err, "strict mode should error on mixed node types")
assert.Contains(t, err.Error(), "mixed node types", "error should mention mixed types")
}
func TestApplyTo_V100_BackwardCompatibility(t *testing.T) {
t.Parallel()
node, err := loader.LoadSpecification("testdata/openapi.yaml")
require.NoError(t, err)
o, err := loader.LoadOverlay("testdata/overlay.yaml")
require.NoError(t, err)
err = o.ApplyTo(node)
require.NoError(t, err)
NodeMatchesFile(t, node, "testdata/openapi-overlayed.yaml", "1.0.0 overlay should produce identical output")
}
func TestApplyTo_V100_BackwardCompatibility_Strict(t *testing.T) {
t.Parallel()
node, err := loader.LoadSpecification("testdata/openapi.yaml")
require.NoError(t, err)
o, err := loader.LoadOverlay("testdata/overlay.yaml")
require.NoError(t, err)
_, err = o.ApplyToStrict(node)
require.NoError(t, err)
NodeMatchesFile(t, node, "testdata/openapi-overlayed.yaml", "1.0.0 strict overlay should produce identical output")
}
func TestApplyToOld(t *testing.T) {
t.Parallel()
nodeOld, err := loader.LoadSpecification("testdata/openapi.yaml")
require.NoError(t, err)
nodeNew, err := loader.LoadSpecification("testdata/openapi.yaml")
require.NoError(t, err)
o, err := loader.LoadOverlay("testdata/overlay-old.yaml")
require.NoError(t, err)
warnings, err := o.ApplyToStrict(nodeOld)
require.NoError(t, err)
require.Len(t, warnings, 2)
require.Contains(t, warnings[0], "invalid rfc9535 jsonpath")
require.Contains(t, warnings[1], "x-speakeasy-jsonpath: rfc9535")
path, err := jsonpath.NewPath(`$.paths["/anything/selectGlobalServer"]`)
require.NoError(t, err)
result := path.Query(nodeOld)
require.NoError(t, err)
require.Empty(t, result)
o.JSONPathVersion = "rfc9535"
_, err = o.ApplyToStrict(nodeNew)
require.ErrorContains(t, err, "unexpected token") // should error out: invalid nodepath
// now lets fix it.
o.Actions[0].Target = "$.paths.*[?(@[\"x-my-ignore\"])]"
_, err = o.ApplyToStrict(nodeNew)
require.ErrorContains(t, err, "did not match any targets")
// Now lets fix it.
o.Actions[0].Target = "$.paths[?(@[\"x-my-ignore\"])]" // @ should always refer to the child node in RFC 9535..
_, err = o.ApplyToStrict(nodeNew)
require.NoError(t, err)
result = path.Query(nodeNew)
require.NoError(t, err)
require.Empty(t, result)
}