-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathoverlay_examples_test.go
More file actions
293 lines (262 loc) · 6.98 KB
/
Copy pathoverlay_examples_test.go
File metadata and controls
293 lines (262 loc) · 6.98 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
package overlay_test
import (
"fmt"
"os"
"strings"
"github.com/speakeasy-api/openapi/overlay"
"github.com/speakeasy-api/openapi/overlay/loader"
"gopkg.in/yaml.v3"
)
// Example_applying demonstrates how to apply an overlay to an OpenAPI document.
// Shows loading an overlay specification and applying it to transform an OpenAPI document.
func Example_applying() {
// Create temporary files for this example
overlayContent := `overlay: 1.0.0
info:
title: Pet Store Enhancement Overlay
version: 1.0.0
actions:
- target: $.info.description
update: Enhanced pet store API with additional features`
openAPIContent := `openapi: 3.1.0
info:
title: Pet Store API
version: 1.0.0
description: A simple pet store API
paths:
/pets:
get:
summary: List pets
responses:
'200':
description: A list of pets`
// Write temporary files
overlayFile := "temp_overlay.yaml"
openAPIFile := "temp_openapi.yaml"
if err := os.WriteFile(overlayFile, []byte(overlayContent), 0644); err != nil {
panic(err)
}
if err := os.WriteFile(openAPIFile, []byte(openAPIContent), 0644); err != nil {
panic(err)
}
defer os.Remove(overlayFile)
defer os.Remove(openAPIFile)
// Parse the overlay
overlayDoc, err := overlay.Parse(overlayFile)
if err != nil {
panic(err)
}
// Load the OpenAPI document
openAPINode, err := loader.LoadSpecification(openAPIFile)
if err != nil {
panic(err)
}
// Apply the overlay to the OpenAPI document
err = overlayDoc.ApplyTo(openAPINode)
if err != nil {
panic(err)
}
// Convert back to YAML string
var buf strings.Builder
encoder := yaml.NewEncoder(&buf)
encoder.SetIndent(2)
err = encoder.Encode(openAPINode)
if err != nil {
panic(err)
}
fmt.Printf("Transformed document:\n%s", buf.String())
// Output:
// Transformed document:
// openapi: 3.1.0
// info:
// title: Pet Store API
// version: 1.0.0
// description: Enhanced pet store API with additional features
// paths:
// /pets:
// get:
// summary: List pets
// responses:
// '200':
// description: A list of pets
}
// Example_creating demonstrates how to create an overlay specification programmatically.
// Shows building an overlay specification with update and remove actions.
func Example_creating() {
// Create update value as yaml.Node
var updateNode yaml.Node
updateNode.SetString("Enhanced API with additional features")
// Create an overlay with update and remove actions
overlayDoc := &overlay.Overlay{
Version: "1.0.0",
Info: overlay.Info{
Title: "API Enhancement Overlay",
Version: "1.0.0",
},
Actions: []overlay.Action{
{
Target: "$.info.description",
Update: updateNode,
},
{
Target: "$.paths['/deprecated-endpoint']",
Remove: true,
},
},
}
result, err := overlayDoc.ToString()
if err != nil {
panic(err)
}
fmt.Printf("Overlay specification:\n%s", result)
// Output:
// Overlay specification:
// overlay: 1.0.0
// info:
// title: API Enhancement Overlay
// version: 1.0.0
// actions:
// - target: $.info.description
// update: Enhanced API with additional features
// - target: $.paths['/deprecated-endpoint']
// remove: true
}
// Example_parsing demonstrates how to parse an overlay specification from a file.
// Shows loading an overlay file and accessing its properties.
func Example_parsing() {
overlayContent := `overlay: 1.0.0
info:
title: API Modification Overlay
version: 1.0.0
actions:
- target: $.info.title
update: Enhanced Pet Store API
- target: $.info.version
update: 2.0.0`
// Write temporary file
overlayFile := "temp_overlay.yaml"
if err := os.WriteFile(overlayFile, []byte(overlayContent), 0644); err != nil {
panic(err)
}
defer func() { _ = os.Remove(overlayFile) }()
overlayDoc, err := overlay.Parse(overlayFile)
if err != nil {
panic(err)
}
fmt.Printf("Overlay Version: %s\n", overlayDoc.Version)
fmt.Printf("Title: %s\n", overlayDoc.Info.Title)
fmt.Printf("Number of Actions: %d\n", len(overlayDoc.Actions))
for i, action := range overlayDoc.Actions {
fmt.Printf("Action %d Target: %s\n", i+1, action.Target)
}
// Output:
// Overlay Version: 1.0.0
// Title: API Modification Overlay
// Number of Actions: 2
// Action 1 Target: $.info.title
// Action 2 Target: $.info.version
}
// Example_validating demonstrates how to validate an overlay specification.
// Shows loading and validating an overlay specification for correctness.
func Example_validating() {
// Invalid overlay specification (missing required fields)
invalidOverlay := `overlay: 1.0.0
info:
title: Invalid Overlay
actions:
- target: $.info.title
description: Missing update or remove`
// Write temporary file
overlayFile := "temp_invalid_overlay.yaml"
if err := os.WriteFile(overlayFile, []byte(invalidOverlay), 0644); err != nil {
panic(err)
}
defer func() { _ = os.Remove(overlayFile) }()
overlayDoc, err := overlay.Parse(overlayFile)
if err != nil {
fmt.Printf("Parse error: %s\n", err.Error())
return
}
validationErr := overlayDoc.Validate()
if validationErr != nil {
fmt.Println("Validation errors:")
fmt.Printf(" %s\n", validationErr.Error())
} else {
fmt.Println("Overlay specification is valid!")
}
// Output:
// Validation errors:
// overlay info version must be defined
}
// Example_removing demonstrates how to use remove actions in overlays.
// Shows removing specific paths and properties from an OpenAPI document.
func Example_removing() {
// Sample OpenAPI document with endpoints to remove
openAPIContent := `openapi: 3.1.0
info:
title: API
version: 1.0.0
paths:
/users:
get:
summary: List users
/users/{id}:
get:
summary: Get user
/admin:
get:
summary: Admin endpoint
deprecated: true`
// Overlay to remove deprecated endpoints
overlayContent := `overlay: 1.0.0
info:
title: Cleanup Overlay
version: 1.0.0
actions:
- target: $.paths['/admin']
remove: true`
// Write temporary files
openAPIFile := "temp_openapi.yaml"
overlayFile := "temp_overlay.yaml"
if err := os.WriteFile(openAPIFile, []byte(openAPIContent), 0644); err != nil {
panic(err)
}
if err := os.WriteFile(overlayFile, []byte(overlayContent), 0644); err != nil {
panic(err)
}
defer func() { _ = os.Remove(openAPIFile) }()
defer func() { _ = os.Remove(overlayFile) }()
overlayDoc, err := overlay.Parse(overlayFile)
if err != nil {
panic(err)
}
openAPINode, err := loader.LoadSpecification(openAPIFile)
if err != nil {
panic(err)
}
err = overlayDoc.ApplyTo(openAPINode)
if err != nil {
panic(err)
}
var buf strings.Builder
encoder := yaml.NewEncoder(&buf)
encoder.SetIndent(2)
err = encoder.Encode(openAPINode)
if err != nil {
panic(err)
}
fmt.Printf("Document after removing deprecated endpoint:\n%s", buf.String())
// Output:
// Document after removing deprecated endpoint:
// openapi: 3.1.0
// info:
// title: API
// version: 1.0.0
// paths:
// /users:
// get:
// summary: List users
// /users/{id}:
// get:
// summary: Get user
}