-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_test.go
136 lines (127 loc) · 3.52 KB
/
link_test.go
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
package openapi_test
import (
"testing"
"github.com/MarkRosemaker/openapi"
"github.com/go-json-experiment/json/jsontext"
)
func TestLink_JSON(t *testing.T) {
t.Parallel()
// Computing a link from a request operation where the `$request.path.id` is used to pass a request parameter to the linked operation.
testJSON(t, []byte(`{
"/users/{id}": {
"parameters": [
{
"name": "id",
"in": "path",
"description": "the user identifier, as userId",
"required": true,
"schema": {
"type": "string"
}
}
],
"get": {
"responses": {
"200": {
"description": "the user being returned",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"uuid": {
"type": "string",
"format": "uuid"
}
}
}
}
},
"links": {
"address": {
"operationId": "getUserAddress",
"parameters": {
"userId": "$request.path.id"
}
}
}
}
}
}
},
"/users/{userid}/address": {
"parameters": [
{
"name": "userid",
"in": "path",
"description": "the user identifier, as userId",
"required": true,
"schema": {
"type": "string"
}
}
],
"get": {
"operationId": "getUserAddress",
"responses": {
"200": {
"description": "the user's address"
}
}
}
}
}`), &openapi.Paths{})
// When a runtime expression fails to evaluate, no parameter value is passed to the target operation.
// Values from the response body can be used to drive a linked operation.
testJSON(t, []byte(`{
"address": {
"operationId": "getUserAddressByUUID",
"parameters": {
"userUuid": "$response.body#/uuid"
}
}
}`), &openapi.Links{})
// As references to `operationId` MAY NOT be possible (the `operationId` is an optional field in an [Operation Object](#operation-object)), references MAY also be made through a relative `operationRef`:
testJSON(t, []byte(`{
"UserRepositories": {
"operationRef": "#/paths/~12.0~1repositories~1{username}/get",
"parameters": {
"username": "$response.body#/username"
}
}
}`), &openapi.Links{})
// or an absolute `operationRef`:
testJSON(t, []byte(`{
"UserRepositories": {
"operationRef": "https://na2.gigantic-server.com/#/paths/~12.0~1repositories~1{username}/get",
"parameters": {
"username": "$response.body#/username"
}
}
}`), &openapi.Links{})
}
func TestLink_Validate_Error(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
link openapi.Link
err string
}{
{openapi.Link{}, `operationRef or operationId must be set`},
{openapi.Link{
OperationRef: "foo",
OperationID: "bar",
}, `operationRef and operationId are mutually exclusive`},
{openapi.Link{
OperationRef: "myRef",
Extensions: jsontext.Value(`{"foo":"bar"}`),
}, `foo: ` + openapi.ErrUnknownField.Error()},
} {
t.Run(tc.err, func(t *testing.T) {
if err := tc.link.Validate(); err == nil {
t.Fatal("expected error")
} else if err.Error() != tc.err {
t.Fatalf("want: %v, got: %v", tc.err, err)
}
})
}
}