-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
321 lines (296 loc) · 10.2 KB
/
Copy pathexample_test.go
File metadata and controls
321 lines (296 loc) · 10.2 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
package stdocs_test
import (
"encoding/json"
"fmt"
"net/http"
"github.com/FumingPower3925/stdocs"
)
// The simplest possible setup: wrap the mux, register routes, mount
// the docs, serve. The generated document is available programmatically
// through JSON() too.
func ExampleNew() {
mux := stdocs.New(stdocs.WithTitle("My API"))
mux.HandleFunc("GET /users/{id}", func(w http.ResponseWriter, r *http.Request) {})
mux.Mount() // docs UI at /docs/, spec at /docs/openapi.json
b, _ := mux.JSON()
var doc struct {
OpenAPI string `json:"openapi"`
Info struct {
Title string `json:"title"`
} `json:"info"`
}
_ = json.Unmarshal(b, &doc)
fmt.Println(doc.OpenAPI, doc.Info.Title)
// Output: 3.0.4 My API
}
// Selecting the OpenAPI version. stdocs ships the latest patch of
// each supported minor: 3.0.4 (default), 3.1.2, and 3.2.0.
func ExampleWithVersion() {
mux := stdocs.New(
stdocs.WithTitle("My API"),
stdocs.WithVersion(stdocs.OpenAPI32),
stdocs.WithSelfURL("https://api.example.com/openapi.json"),
)
mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) {})
b, _ := mux.JSON()
var doc struct {
OpenAPI string `json:"openapi"`
Self string `json:"$self"`
}
_ = json.Unmarshal(b, &doc)
fmt.Println(doc.OpenAPI, doc.Self)
// Output: 3.2.0 https://api.example.com/openapi.json
}
// Documenting request and response bodies through reflection.
func ExampleWithResponse() {
type User struct {
ID string `json:"id" doc:"Unique ID"`
Name string `json:"name"`
}
type CreateUserRequest struct {
Name string `json:"name"`
}
mux := stdocs.New(stdocs.WithTitle("My API"))
mux.HandleFunc("POST /users", func(w http.ResponseWriter, r *http.Request) {},
stdocs.Summary("Create a user"),
stdocs.WithBody(CreateUserRequest{}),
stdocs.WithResponse(201, User{}),
stdocs.WithResponseDescription(201, "The newly created user"),
)
b, _ := mux.JSON()
var doc struct {
Components struct {
Schemas map[string]json.RawMessage `json:"schemas"`
} `json:"components"`
}
_ = json.Unmarshal(b, &doc)
for name := range doc.Components.Schemas {
if name == "User" {
fmt.Println("components.schemas has User")
}
}
// Output: components.schemas has User
}
// A json.RawMessage field that carries a JSON Schema document is
// documented with openapi:"schema=json-schema": a free-form object
// stdocs does not itself validate. On such a field the example: tag
// takes a JSON literal, so an author can show a representative schema.
func Example_jsonSchemaDocumentField() {
type PlatformResponse struct {
Name string `json:"name"`
ConfigSchema json.RawMessage `json:"config_schema" openapi:"schema=json-schema" example:"{\"type\":\"object\",\"properties\":{\"host\":{\"type\":\"string\"}},\"required\":[\"host\"]}"`
}
mux := stdocs.New(stdocs.WithTitle("Platform API"))
mux.HandleFunc("GET /platforms/{id}", func(w http.ResponseWriter, r *http.Request) {},
stdocs.Summary("Get a platform"),
stdocs.WithResponse(200, PlatformResponse{}),
)
b, _ := mux.JSON()
var doc struct {
Components struct {
Schemas map[string]struct {
Properties map[string]struct {
Type string `json:"type"`
Description string `json:"description"`
} `json:"properties"`
} `json:"schemas"`
} `json:"components"`
}
_ = json.Unmarshal(b, &doc)
cs := doc.Components.Schemas["PlatformResponse"].Properties["config_schema"]
fmt.Printf("config_schema: %s — %s\n", cs.Type, cs.Description)
// Output: config_schema: object — A JSON Schema document.
}
// Registering a security scheme and requiring it on a route.
func ExampleWithBearerAuth() {
mux := stdocs.New(
stdocs.WithTitle("My API"),
stdocs.WithBearerAuth("bearerAuth", "JWT"),
stdocs.WithGlobalSecurity("bearerAuth"),
)
mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) {},
stdocs.WithNoSecurity(), // public route opts out of the global scheme
)
b, _ := mux.JSON()
var doc struct {
Paths map[string]map[string]struct {
Security []map[string][]string `json:"security"`
} `json:"paths"`
}
_ = json.Unmarshal(b, &doc)
fmt.Println(len(doc.Paths["/health"]["get"].Security))
// Output: 0
}
// Serving a docs UI for a hand-written spec (Tier 1) — no route
// introspection involved.
func ExampleDocsHandler() {
spec := []byte(`{"openapi":"3.0.4","info":{"title":"Hand-written","version":"1.0.0"},"paths":{}}`)
mux := http.NewServeMux()
mux.Handle("GET /docs/", stdocs.DocsHandler(
stdocs.WithTitle("Hand-written"),
stdocs.WithSpec(spec),
))
fmt.Println("docs mounted")
// Output: docs mounted
}
// Turning the docs off per environment without touching routes.
func ExampleMux_Docs() {
prod := true
mux := stdocs.New(stdocs.WithTitle("My API"))
mux.HandleFunc("GET /users", func(w http.ResponseWriter, r *http.Request) {})
// An explicit per-call value wins over WithDisabled in both
// directions; mux.Docs(false) serves 404 for every docs request.
mux.ServeMux.Handle("GET /docs/", mux.Docs(!prod))
fmt.Println("docs enabled:", !prod)
// Output: docs enabled: false
}
// Restricting what try-it consoles may do: the docs page's "Try it
// out" buttons send real requests, identified (best-effort) by their
// Referer. FromDocs gates a restriction — never use it to grant
// access, since the header is client-controlled.
func ExampleFromDocs() {
mux := stdocs.New(stdocs.WithTitle("My API"))
mux.HandleFunc("POST /users", func(w http.ResponseWriter, r *http.Request) {})
mux.Mount()
guard := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && mux.FromDocs(r) {
http.Error(w, "try-it requests cannot modify data", http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}
_ = guard // pass guard(mux) to http.ListenAndServe
r, _ := http.NewRequest(http.MethodPost, "/users", nil)
r.Header.Set("Referer", "https://api.example.com/docs/")
fmt.Println("from docs:", mux.FromDocs(r))
// Output: from docs: true
}
// Constraint tags and typed parameters: the struct documents its own
// validation rules, and WithParams reflects query parameters from a
// struct with the same vocabulary.
func ExampleWithParams() {
type ListParams struct {
Cursor string `query:"cursor" doc:"Opaque pagination cursor"`
Limit int `query:"limit" default:"20" minimum:"1" maximum:"100"`
}
mux := stdocs.New(stdocs.WithTitle("My API"))
mux.HandleFunc("GET /tasks", func(w http.ResponseWriter, r *http.Request) {},
stdocs.WithParams(ListParams{}),
)
raw, _ := mux.JSON()
var doc struct {
Paths map[string]map[string]struct {
Parameters []struct {
Name string `json:"name"`
Schema struct {
Type string `json:"type"`
Default json.RawMessage `json:"default"`
Maximum json.RawMessage `json:"maximum"`
} `json:"schema"`
} `json:"parameters"`
} `json:"paths"`
}
json.Unmarshal(raw, &doc)
for _, p := range doc.Paths["/tasks"]["get"].Parameters {
fmt.Printf("%s (%s) default=%s max=%s\n",
p.Name, p.Schema.Type, p.Schema.Default, p.Schema.Maximum)
}
// Output:
// cursor (string) default= max=
// limit (integer) default=20 max=100
}
// A repeated query filter — ?severity=high&severity=low — documents as
// an array parameter whose elements carry the enum. Item options nest
// inside ParamItems, which owns the element schema; the same contract
// comes from a WithParams struct field tagged
// `query:"severity" enum:"info,low,high"`.
func ExampleParamItems() {
mux := stdocs.New(stdocs.WithTitle("My API"))
mux.HandleFunc("GET /resources", func(w http.ResponseWriter, r *http.Request) {},
stdocs.QueryParam("severity", "array", "Repeated severity filter",
stdocs.ParamItems("string", stdocs.ItemEnum("info", "low", "high")),
stdocs.ParamUniqueItems(),
),
)
raw, _ := mux.JSON()
var doc struct {
Paths map[string]map[string]struct {
Parameters []struct {
Name string `json:"name"`
Schema struct {
Type string `json:"type"`
UniqueItems bool `json:"uniqueItems"`
Items struct {
Type string `json:"type"`
Enum []string `json:"enum"`
} `json:"items"`
} `json:"schema"`
} `json:"parameters"`
} `json:"paths"`
}
json.Unmarshal(raw, &doc)
p := doc.Paths["/resources"]["get"].Parameters[0]
fmt.Printf("%s (%s of %s) enum=%v uniqueItems=%v\n",
p.Name, p.Schema.Type, p.Schema.Items.Type, p.Schema.Items.Enum, p.Schema.UniqueItems)
// Output:
// severity (array of string) enum=[info low high] uniqueItems=true
}
// The shared error envelope is declared once at the mux level; every
// operation documents it unless it declares the status itself.
func ExampleWithDefaultResponse() {
type APIError struct {
Message string `json:"message"`
}
mux := stdocs.New(
stdocs.WithTitle("My API"),
stdocs.WithDefaultResponse(500, APIError{}),
)
mux.HandleFunc("GET /tasks", func(w http.ResponseWriter, r *http.Request) {})
raw, _ := mux.JSON()
var doc struct {
Paths map[string]map[string]struct {
Responses map[string]struct {
Description string `json:"description"`
} `json:"responses"`
} `json:"paths"`
}
json.Unmarshal(raw, &doc)
resps := doc.Paths["/tasks"]["get"].Responses
for _, code := range []string{"200", "500"} {
fmt.Printf("%s: %s\n", code, resps[code].Description)
}
// Output:
// 200: OK
// 500: Internal Server Error
}
// Route-opt bundles: declare a preset once, reuse it across routes.
func ExampleOpts() {
paginated := stdocs.Opts(
stdocs.QueryParam("cursor", "string", "Opaque cursor"),
stdocs.QueryParam("limit", "integer", "Page size", stdocs.ParamDefault(20)),
)
mux := stdocs.New(stdocs.WithTitle("My API"))
mux.HandleFunc("GET /tasks", func(w http.ResponseWriter, r *http.Request) {}, paginated)
mux.HandleFunc("GET /users", func(w http.ResponseWriter, r *http.Request) {}, paginated)
raw, _ := mux.JSON()
var doc struct {
Paths map[string]map[string]struct {
Parameters []struct {
Name string `json:"name"`
} `json:"parameters"`
} `json:"paths"`
}
json.Unmarshal(raw, &doc)
for _, path := range []string{"/tasks", "/users"} {
names := make([]string, 0, 2)
for _, p := range doc.Paths[path]["get"].Parameters {
names = append(names, p.Name)
}
fmt.Println(path, names)
}
// Output:
// /tasks [cursor limit]
// /users [cursor limit]
}