-
-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathopenapi_test.go
More file actions
374 lines (348 loc) · 8.92 KB
/
Copy pathopenapi_test.go
File metadata and controls
374 lines (348 loc) · 8.92 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
package huma_test
import (
"context"
"net/http"
"testing"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/humatest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOpenAPIMarshal(t *testing.T) {
// Simple spot check to make sure we are generating valid YAML and that
// the OpenAPI generally works as expected.
num := 1.0
v := huma.OpenAPI{
OpenAPI: "3.0.0",
Info: &huma.Info{
Title: "Test API",
Version: "1.0.0",
Contact: &huma.Contact{
Name: "Daniel Taylor",
},
License: &huma.License{
Name: "MIT",
},
},
ExternalDocs: &huma.ExternalDocs{
URL: "https://example.com",
},
Tags: []*huma.Tag{
{
Name: "test",
},
},
Servers: []*huma.Server{
{
URL: "https://example.com/{foo}",
Variables: map[string]*huma.ServerVariable{
"foo": {
Default: "bar",
Enum: []string{"bar", "baz"},
},
},
},
},
Components: &huma.Components{
Schemas: huma.NewMapRegistry("#/components/schemas/", huma.DefaultSchemaNamer),
SecuritySchemes: map[string]*huma.SecurityScheme{
"oauth2": {
Type: "oauth2",
Flows: &huma.OAuthFlows{
ClientCredentials: &huma.OAuthFlow{
AuthorizationURL: "https://example.com/oauth2/authorize",
TokenURL: "https://example.com/oauth2/token",
Scopes: map[string]string{
"test": "Test scope",
},
},
},
},
},
},
Paths: map[string]*huma.PathItem{
"/test": {
Get: &huma.Operation{
Responses: map[string]*huma.Response{
"200": {
Description: "OK",
Content: map[string]*huma.MediaType{
"application/json": {
Examples: map[string]*huma.Example{
"test": {
Value: `{"test": "example"}`,
},
},
Encoding: map[string]*huma.Encoding{
"test": {
ContentType: "application/json",
},
},
Schema: &huma.Schema{
Type: "object",
Properties: map[string]*huma.Schema{
"test": {
Type: "integer",
Minimum: &num,
},
},
},
},
},
Links: map[string]*huma.Link{
"related": {
OperationID: "another-operation",
},
},
},
},
},
},
},
Extensions: map[string]any{
"x-test": 123,
},
}
// This will marshal to JSON, then convert to YAML.
out, err := v.YAML()
require.NoError(t, err)
expected := `components:
schemas: {}
securitySchemes:
oauth2:
flows:
clientCredentials:
authorizationUrl: https://example.com/oauth2/authorize
scopes:
test: Test scope
tokenUrl: https://example.com/oauth2/token
type: oauth2
externalDocs:
url: https://example.com
info:
contact:
name: Daniel Taylor
license:
name: MIT
title: Test API
version: 1.0.0
openapi: 3.0.0
paths:
/test:
get:
responses:
"200":
content:
application/json:
encoding:
test:
contentType: application/json
examples:
test:
value: "{\"test\": \"example\"}"
schema:
properties:
test:
minimum: 1
type: integer
type: object
description: OK
links:
related:
operationId: another-operation
servers:
- url: https://example.com/{foo}
variables:
foo:
default: bar
enum:
- bar
- baz
tags:
- name: test
x-test: 123
`
require.Equal(t, expected, string(out))
}
func TestDowngrade(t *testing.T) {
// Test that we can downgrade a v3 OpenAPI document to v2.
v31 := &huma.OpenAPI{
OpenAPI: "3.1.0",
Info: &huma.Info{
Title: "Test API",
Version: "1.0.0",
},
Paths: map[string]*huma.PathItem{
"/test": {
Get: &huma.Operation{
Responses: map[string]*huma.Response{
"200": {
Description: "OK",
Content: map[string]*huma.MediaType{
"application/json": {
Schema: &huma.Schema{
Type: "object",
Properties: map[string]*huma.Schema{
"test": {
Type: "integer",
ExclusiveMinimum: Ptr(0.0),
ExclusiveMaximum: Ptr(100.0),
Nullable: true,
Examples: []any{100},
},
"encoding": {
Type: huma.TypeString,
ContentEncoding: "base64",
},
},
},
},
"application/octet-stream": {},
},
},
},
},
},
},
}
v30, err := v31.Downgrade()
require.NoError(t, err)
expected := `{
"openapi": "3.0.3",
"info": {
"title": "Test API",
"version": "1.0.0"
},
"paths": {
"/test": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"test": {
"type": "integer",
"nullable": true,
"minimum": 0,
"exclusiveMinimum": true,
"maximum": 100,
"exclusiveMaximum": true,
"example": 100
},
"encoding": {
"type": "string",
"format": "base64"
}
}
}
},
"application/octet-stream": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
}
}
}
}
}
}`
// Check that the downgrade worked as expected.
assert.JSONEq(t, expected, string(v30))
}
func TestAddOperationForceUniqueOperationIDs(t *testing.T) {
oapi := &huma.OpenAPI{}
oapi.AddOperation(&huma.Operation{
OperationID: "test",
Method: http.MethodGet,
Path: "/test",
})
assert.PanicsWithValue(t, "duplicate operation ID: test", func() {
oapi.AddOperation(&huma.Operation{
OperationID: "test",
Method: http.MethodPost,
Path: "/test",
})
})
}
func TestAddOperationNormalizeOperationIDs(t *testing.T) {
oapi := &huma.OpenAPI{}
oapi.AddOperation(&huma.Operation{
OperationID: "test with spaces",
Method: http.MethodGet,
Path: "/test",
})
assert.Equal(t, "test-with-spaces", oapi.Paths["/test"].Get.OperationID)
}
// TestHiddenOperationSchemasOmitted verifies that schemas which are only used by
// `Hidden` operations are pruned from the exported OpenAPI document, while
// schemas reachable from visible operations (including transitively-referenced
// and shared types) are kept. The underlying registry is left intact so request
// validation for hidden routes continues to work.
func TestHiddenOperationSchemasOmitted(t *testing.T) {
_, api := humatest.New(t, huma.DefaultConfig("Test API", "1.0.0"))
// Shared is used by both a visible and a hidden operation, so it must be
// kept. Nested is only referenced transitively by the visible response.
type Shared struct {
Value string `json:"value"`
}
type Nested struct {
Count int `json:"count"`
}
type VisibleResponse struct {
Shared Shared `json:"shared"`
Nested Nested `json:"nested"`
}
type VisibleResp struct {
Body VisibleResponse
}
huma.Register(api, huma.Operation{
OperationID: "get-visible",
Method: http.MethodGet,
Path: "/visible",
}, func(ctx context.Context, _ *struct{}) (*VisibleResp, error) {
return &VisibleResp{}, nil
})
// SecretAdmin is used only by the hidden operation and must be omitted from
// the exported document.
type SecretAdmin struct {
Token string `json:"token" minLength:"5"`
Shared Shared `json:"shared"`
}
type HiddenInput struct {
Body SecretAdmin
}
huma.Register(api, huma.Operation{
OperationID: "admin-only",
Method: http.MethodPost,
Path: "/admin",
Hidden: true,
}, func(ctx context.Context, _ *HiddenInput) (*struct{}, error) {
return nil, nil
})
b, err := api.OpenAPI().YAML()
require.NoError(t, err)
spec := string(b)
// The hidden operation's path is not documented...
assert.NotContains(t, spec, "/admin")
// ...and neither is the schema used only by it.
assert.NotContains(t, spec, "SecretAdmin")
// Schemas reachable from the visible operation remain, including
// transitively-referenced and shared types.
assert.Contains(t, spec, "VisibleResponse")
assert.Contains(t, spec, "Nested")
assert.Contains(t, spec, "Shared")
// The underlying registry is untouched, so validation for the hidden route
// still resolves its schema: an invalid body is rejected...
resp := api.Post("/admin", map[string]any{"token": "x", "shared": map[string]any{"value": "y"}})
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code)
// ...and a valid body is accepted.
resp = api.Post("/admin", map[string]any{"token": "longenough", "shared": map[string]any{"value": "y"}})
assert.Less(t, resp.Code, 300)
}