-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
openapi_test.go
267 lines (255 loc) · 5.65 KB
/
openapi_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
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
package huma_test
import (
"testing"
"github.com/danielgtaylor/huma/v2"
"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))
}