generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.go
263 lines (208 loc) · 5.77 KB
/
schema.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
package jsonrpc
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"sync"
"github.com/swaggest/openapi-go/openapi3"
"github.com/swaggest/usecase"
)
// OpenAPI extracts OpenAPI documentation from HTTP handler and underlying use case interactor.
type OpenAPI struct {
mu sync.Mutex
BasePath string // URL path to docs, default "/docs/".
gen *openapi3.Reflector
annotations map[string][]func(*openapi3.Operation) error
}
// Reflector is an accessor to OpenAPI Reflector instance.
func (c *OpenAPI) Reflector() *openapi3.Reflector {
if c.gen == nil {
c.gen = &openapi3.Reflector{}
}
return c.gen
}
// Annotate adds OpenAPI operation configuration that is applied during collection.
func (c *OpenAPI) Annotate(name string, setup ...func(op *openapi3.Operation) error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.annotations == nil {
c.annotations = make(map[string][]func(op *openapi3.Operation) error)
}
c.annotations[name] = append(c.annotations[name], setup...)
}
// Collect adds use case handler to documentation.
func (c *OpenAPI) Collect(
name string,
u usecase.Interactor,
v Validator,
annotations ...func(*openapi3.Operation) error,
) (err error) {
c.mu.Lock()
defer c.mu.Unlock()
defer func() {
if err != nil {
err = fmt.Errorf("failed to reflect API schema for %s: %w", name, err)
}
}()
reflector := c.Reflector()
reflector.SpecEns().WithMapOfAnythingItem("x-envelope", "jsonrpc-2.0")
err = reflector.SpecEns().SetupOperation(http.MethodPost, name, func(op *openapi3.Operation) error {
oc := openapi3.OperationContext{
Operation: op,
HTTPMethod: http.MethodPost,
HTTPStatus: http.StatusOK,
RespContentType: "application/json",
}
err = c.setupInput(&oc, u, name, v)
if err != nil {
return fmt.Errorf("failed to setup request: %w", err)
}
err = c.setupOutput(&oc, u, name, v)
if err != nil {
return fmt.Errorf("failed to setup response: %w", err)
}
c.processUseCase(op, u)
for _, setup := range c.annotations[name] {
err = setup(op)
if err != nil {
return err
}
}
for _, setup := range annotations {
err = setup(op)
if err != nil {
return err
}
}
return nil
})
return err
}
func (c *OpenAPI) setupOutput(oc *openapi3.OperationContext, u usecase.Interactor, method string, v Validator) error {
var (
hasOutput usecase.HasOutputPort
status = http.StatusOK
)
if usecase.As(u, &hasOutput) {
oc.Output = hasOutput.OutputPort()
}
if oc.HTTPStatus == 0 {
oc.HTTPStatus = status
}
err := c.Reflector().SetupResponse(*oc)
if err != nil {
return err
}
if v != nil {
return c.provideResponseJSONSchemas(method, oc.Operation, v)
}
return nil
}
func (c *OpenAPI) setupInput(oc *openapi3.OperationContext, u usecase.Interactor, method string, v Validator) error {
var (
hasInput usecase.HasInputPort
err error
)
if usecase.As(u, &hasInput) {
oc.Input = hasInput.InputPort()
err = c.Reflector().SetupRequest(*oc)
if err != nil {
return err
}
if v != nil {
return c.provideRequestJSONSchema(method, oc.Operation, v)
}
}
return nil
}
func (c *OpenAPI) processUseCase(op *openapi3.Operation, u usecase.Interactor) {
var (
hasName usecase.HasName
hasTitle usecase.HasTitle
hasDescription usecase.HasDescription
hasTags usecase.HasTags
hasDeprecated usecase.HasIsDeprecated
)
if usecase.As(u, &hasName) {
op.WithID(hasName.Name())
}
if usecase.As(u, &hasTitle) {
op.WithSummary(hasTitle.Title())
}
if usecase.As(u, &hasTags) {
op.WithTags(hasTags.Tags()...)
}
if usecase.As(u, &hasDescription) {
op.WithDescription(hasDescription.Description())
}
if usecase.As(u, &hasDeprecated) && hasDeprecated.IsDeprecated() {
op.WithDeprecated(true)
}
}
func (c *OpenAPI) ServeHTTP(rw http.ResponseWriter, _ *http.Request) {
c.mu.Lock()
defer c.mu.Unlock()
document, err := json.MarshalIndent(c.Reflector().Spec, "", " ")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
rw.Header().Set("Content-Type", "application/json; charset=utf8")
_, err = rw.Write(document)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
// ProvideRequestJSONSchemas provides JSON Schemas for request structure.
func (c *OpenAPI) provideRequestJSONSchema(
method string,
op *openapi3.Operation,
validator Validator,
) error {
if op.RequestBody != nil && op.RequestBody.RequestBody != nil {
for ct, content := range op.RequestBody.RequestBody.Content {
if ct != "application/json" {
continue
}
schema := content.Schema.ToJSONSchema(c.Reflector().Spec)
if schema.IsTrivial(c.Reflector().ResolveJSONSchemaRef) {
continue
}
schemaData, err := schema.JSONSchemaBytes()
if err != nil {
return errors.New("failed to build JSON Schema for request body")
}
err = validator.AddParamsSchema(method, schemaData)
if err != nil {
return fmt.Errorf("failed to add validation schema for request body: %w", err)
}
}
}
return nil
}
// provideResponseJSONSchemas provides JSON schemas for response structure.
func (c *OpenAPI) provideResponseJSONSchemas(
method string,
op *openapi3.Operation,
validator Validator,
) error {
resp := op.Responses.MapOfResponseOrRefValues[strconv.Itoa(http.StatusOK)].Response
for _, cont := range resp.Content {
if cont.Schema == nil {
continue
}
schema := cont.Schema.ToJSONSchema(c.Reflector().Spec)
if schema.IsTrivial(c.Reflector().ResolveJSONSchemaRef) {
continue
}
schemaData, err := schema.JSONSchemaBytes()
if err != nil {
return errors.New("failed to build JSON Schema for response body")
}
if err := validator.AddResultSchema(method, schemaData); err != nil {
return fmt.Errorf("failed to add validation schema for response body: %w", err)
}
}
return nil
}