-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrequestbody_test.go
More file actions
130 lines (111 loc) · 4.62 KB
/
Copy pathrequestbody_test.go
File metadata and controls
130 lines (111 loc) · 4.62 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
package arazzo_test
import (
"testing"
"github.com/speakeasy-api/openapi/arazzo"
"github.com/speakeasy-api/openapi/arazzo/core"
"github.com/speakeasy-api/openapi/marshaller"
"github.com/speakeasy-api/openapi/pointer"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestRequestBody_Validate_JSONPathInPayload_Success(t *testing.T) {
t.Parallel()
// This test reproduces the bug where JSONPath-like expressions in payload
// are incorrectly validated as Arazzo runtime expressions
ctx := t.Context()
// Create a payload that contains JSONPath-like expressions ($.Id, $.time, etc.)
// but these should NOT be validated as Arazzo expressions since they're just payload data
payloadNode := &yaml.Node{
Kind: yaml.MappingNode,
Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "dataSource"},
{Kind: yaml.ScalarNode, Value: "/query?query=select * from Account"},
{Kind: yaml.ScalarNode, Value: "keyBy"},
{Kind: yaml.SequenceNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "$.Id"},
}},
{Kind: yaml.ScalarNode, Value: "requiredData"},
{Kind: yaml.MappingNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "Currentbal"},
{Kind: yaml.ScalarNode, Value: "$.CurrentBalance"},
{Kind: yaml.ScalarNode, Value: "SubAcc"},
{Kind: yaml.ScalarNode, Value: "$.SubAccount"},
{Kind: yaml.ScalarNode, Value: "id"},
{Kind: yaml.ScalarNode, Value: "$.Id"},
}},
{Kind: yaml.ScalarNode, Value: "sourceModifiedDate"},
{Kind: yaml.SequenceNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "$.time"},
}},
},
}
requestBody := &arazzo.RequestBody{
ContentType: pointer.From("application/json"),
Payload: payloadNode,
Model: marshaller.Model[core.RequestBody]{
Valid: true,
},
}
// This should NOT return validation errors for the JSONPath expressions in the payload
errs := requestBody.Validate(ctx)
// The bug causes validation errors like:
// "expression is not valid, must begin with one of [url, method, statusCode, request, response, inputs, outputs, steps, workflows, sourceDescriptions, components]: $.time"
assert.Empty(t, errs, "JSONPath-like expressions in payload should not be validated as Arazzo expressions")
}
func TestRequestBody_Validate_AnyPayloadData_Success(t *testing.T) {
t.Parallel()
// This test ensures that ANY data in payloads is allowed, including invalid expressions
// since payloads are arbitrary user data and should not be validated as Arazzo expressions
ctx := t.Context()
// Create a payload with various expression-like data that should all be ignored
payloadNode := &yaml.Node{
Kind: yaml.MappingNode,
Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "invalidExpression"},
{Kind: yaml.ScalarNode, Value: "$invalidExpression"}, // Would be invalid if validated
{Kind: yaml.ScalarNode, Value: "jsonPath"},
{Kind: yaml.ScalarNode, Value: "$.field.subfield"}, // JSONPath expression
{Kind: yaml.ScalarNode, Value: "template"},
{Kind: yaml.ScalarNode, Value: "${variable}"}, // Template-like expression
},
}
requestBody := &arazzo.RequestBody{
ContentType: pointer.From("application/json"),
Payload: payloadNode,
Model: marshaller.Model[core.RequestBody]{
Valid: true,
},
}
errs := requestBody.Validate(ctx)
// All payload data should be allowed without validation errors
assert.Empty(t, errs, "Payload data should not be validated as Arazzo expressions")
}
func TestRequestBody_Validate_TopLevelExpression_ValidatesCorrectly(t *testing.T) {
t.Parallel()
// Test that top-level Arazzo expressions are properly validated
// Test valid top-level expression
validRequestBody := &arazzo.RequestBody{}
validPayloadNode := &yaml.Node{
Kind: yaml.ScalarNode,
Value: "$inputs.userId",
}
validRequestBody.Payload = validPayloadNode
validRequestBody.Model = marshaller.Model[core.RequestBody]{
Valid: true,
}
validationErrors := validRequestBody.Validate(t.Context())
assert.Empty(t, validationErrors, "Valid top-level expression should not produce validation errors")
// Test invalid top-level expression (valid type but invalid format)
invalidRequestBody := &arazzo.RequestBody{}
invalidPayloadNode := &yaml.Node{
Kind: yaml.ScalarNode,
Value: "$inputs", // Missing required name after $inputs
}
invalidRequestBody.Payload = invalidPayloadNode
invalidRequestBody.Model = marshaller.Model[core.RequestBody]{
Valid: true,
}
validationErrors = invalidRequestBody.Validate(t.Context())
assert.NotEmpty(t, validationErrors, "Invalid top-level expression should produce validation errors")
assert.Contains(t, validationErrors[0].Error(), "payload expression is not valid")
}