Repository navigation
Expand file tree
/
Copy pathroute.go
More file actions
259 lines (214 loc) · 6.95 KB
/
Copy pathroute.go
File metadata and controls
259 lines (214 loc) · 6.95 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
package rip
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
ripjson "github.com/dolanor/rip/encoding/json"
"github.com/dolanor/rip/internal/ripreflect"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3gen"
)
type Route interface {
Path() string
Handler() http.HandlerFunc
OpenAPISchema() *openapi3.T
}
type EntityRoute[
Ent any,
EP EntityProvider[Ent],
] struct {
path string
handlerFunc http.HandlerFunc
provider EP
openAPISchema *openapi3.T
generator *openapi3gen.Generator
}
func (er *EntityRoute[Ent, EP]) Path() string {
return er.path
}
func (er *EntityRoute[Ent, EP]) Handler() http.HandlerFunc {
return er.handlerFunc
}
func (er *EntityRoute[Ent, EP]) OpenAPISchema() *openapi3.T {
return er.openAPISchema
}
// NewEntityRoute generates an endpoint that will handle all the methods for the entityProvider at the
// given path and with specific options.
// It returns the path, the endpoint handler and the schema of this route.
func NewEntityRoute[
Ent any,
EP EntityProvider[Ent],
](path string, entityProvider EP, options ...EntityRouteOption) *EntityRoute[Ent, EP] {
var cfg entityRouteConfig
for _, o := range options {
o(&cfg)
}
generator := openapi3gen.NewGenerator(
openapi3gen.UseAllExportedFields(),
)
// just a base that we can merge with other entity routes on the router
oaSpec := newOpenApiSpec("", "should not be use as is", "")
rt := EntityRoute[Ent, EP]{
path: path,
provider: entityProvider,
openAPISchema: &oaSpec,
generator: generator,
}
rt.generateOperation()
_, handler := rt.createEntityHandler(path, entityProvider, cfg)
rt.handlerFunc = handler
return &rt
}
func (rt *EntityRoute[Ent, EP]) createEntityHandler(
urlPath string,
ep EP,
cfg entityRouteConfig,
) (path string, handler http.HandlerFunc) {
if len(cfg.codecs.Codecs) == 0 {
err := fmt.Sprintf("no codecs defined on route: %s", urlPath)
panic(err)
}
cfg = setEntityRouteConfigDefaults(cfg)
return handleEntityWithPath(urlPath, ep.Create, ep.Get, ep.Update, ep.Delete, ep.List, cfg)
}
func (rt *EntityRoute[Ent, EP]) generateOperation() {
var ent Ent
tag, ok := ripreflect.TagFromType(ent)
if !ok {
panic("generate OpenAPI operation: can not get type tag")
}
// we register the /{entity}/{id} path ID parameter once
// and save it as an OpenAPI path parameter (so we don't have to duplicate it on
// every OpenAPI operation.
{
entityPath := path.Join(rt.path, "{id}")
param := openapi3.NewPathParameter("id")
param.Description = "id of the " + tag
param.Schema = openapi3.NewStringSchema().NewRef()
rt.openAPISchema.Paths.Set(entityPath, &openapi3.PathItem{
Parameters: []*openapi3.ParameterRef{
{
Value: param,
},
},
})
}
for _, method := range []string{
http.MethodPost,
http.MethodGet,
http.MethodPut,
http.MethodDelete,
} {
op := openapi3.NewOperation()
op.Tags = append(op.Tags, tag)
if (method == http.MethodPost || method == http.MethodPut) && tag != "string" {
bodySchema, ok := rt.openAPISchema.Components.Schemas[tag]
if !ok {
var err error
// TODO test with the entity or a pointer to it
bodySchema, err = rt.generator.NewSchemaRefForValue(ent, rt.openAPISchema.Components.Schemas)
if err != nil {
// there is no point of going further, and silently failing would be bad.
panic("generate OpenAPI operation: can not generate schema ref for request value: " + fmt.Sprintf("%+v: %v", ent, err))
}
rt.openAPISchema.Components.Schemas[tag] = bodySchema
}
requestBody := openapi3.NewRequestBody().
WithRequired(true).
WithDescription("Request body for " + tag)
if bodySchema != nil {
// TODO add route options multiple encoding
content := openapi3.NewContentWithSchema(bodySchema.Value, []string{"application/json"})
content["application/json"].Schema.Ref = "#/components/schemas/" + tag
requestBody.WithContent(content)
}
// add request body to operation
op.RequestBody = &openapi3.RequestBodyRef{
Value: requestBody,
}
}
// TODO: handle more API responses.
response := openapi3.NewResponse().WithDescription("OK")
if method != http.MethodDelete {
// Response body
responseSchema, ok := rt.openAPISchema.Components.Schemas[tag]
if !ok {
var err error
// TODO test with the entity ent or a pointer to it
responseSchema, err = rt.generator.NewSchemaRefForValue(ent, rt.openAPISchema.Components.Schemas)
if err != nil {
// there is no point of going further, and silently failing would be bad.
panic("generate OpenAPI operation: can not generate schema ref for response value: " + fmt.Sprintf("%+v: %v", ent, err))
}
rt.openAPISchema.Components.Schemas[tag] = responseSchema
}
if responseSchema == nil {
panic("could not find response schema: " + tag)
}
content := openapi3.NewContentWithSchema(responseSchema.Value, []string{"application/json"})
content["application/json"].Schema.Ref = "#/components/schemas/" + tag
response.WithContent(content)
}
op.AddResponse(200, response)
entityPath := rt.path
switch method {
case http.MethodGet,
http.MethodPut,
http.MethodDelete:
entityPath = path.Join(rt.path, "{id}")
}
rt.openAPISchema.AddOperation(entityPath, method, op)
}
rt.generateList()
}
func (rt *EntityRoute[Ent, EP]) generateList() {
method := http.MethodGet
var ent Ent
tag, ok := ripreflect.TagFromType(ent)
if !ok {
panic("generate OpenAPI operation: can not get type tag")
}
op := openapi3.NewOperation()
op.Tags = append(op.Tags, tag)
// Response body
itemResponseSchema, ok := rt.openAPISchema.Components.Schemas[tag]
if !ok {
var err error
// TODO test with the entity ent or a pointer to it
itemResponseSchema, err = rt.generator.NewSchemaRefForValue(ent, rt.openAPISchema.Components.Schemas)
if err != nil {
// there is no point of going further, and silently failing would be bad.
panic("generate OpenAPI operation: can not generate schema ref for response value: " + fmt.Sprintf("%+v: %v", ent, err))
}
rt.openAPISchema.Components.Schemas[tag] = itemResponseSchema
}
// TODO: handle more API responses.
if itemResponseSchema == nil {
panic("could not find response schema: " + tag)
}
itemsResponseSchema := openapi3.NewArraySchema().WithItems(itemResponseSchema.Value)
content := openapi3.NewContentWithSchema(itemsResponseSchema, []string{"application/json"})
response := openapi3.NewResponse().WithDescription("OK").WithContent(content)
op.AddResponse(200, response)
entityPath := rt.path
rt.openAPISchema.AddOperation(entityPath, method, op)
}
func dumpSchema(title string, schema any) {
b, _ := json.Marshal(schema)
fmt.Print(string(b))
fmt.Fprintln(os.Stderr)
}
func setEntityRouteConfigDefaults(cfg entityRouteConfig) entityRouteConfig {
if len(cfg.codecs.Codecs) == 0 {
cfg.codecs.Register(ripjson.Codec)
}
if cfg.listPageSize == 0 {
cfg.listPageSize = 20
}
if cfg.listPageSizeMax == 0 {
cfg.listPageSizeMax = 100
}
return cfg
}