forked from go-fuego/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi.go
More file actions
245 lines (205 loc) · 6.91 KB
/
openapi.go
File metadata and controls
245 lines (205 loc) · 6.91 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
package fuego
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"path/filepath"
"reflect"
"regexp"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3gen"
)
func NewOpenApiSpec() openapi3.T {
info := &openapi3.Info{
Title: "OpenAPI",
Description: "OpenAPI",
Version: "0.0.1",
}
spec := openapi3.T{
OpenAPI: "3.0.3",
Info: info,
Paths: &openapi3.Paths{},
Components: &openapi3.Components{
Schemas: make(map[string]*openapi3.SchemaRef),
RequestBodies: make(map[string]*openapi3.RequestBodyRef),
Responses: make(map[string]*openapi3.ResponseRef),
},
}
return spec
}
// Hide prevents the routes in this server or group from being included in the OpenAPI spec.
func (s *Server) Hide() *Server {
s.DisableOpenapi = true
return s
}
// Show allows to display the routes. Activated by default so useless in most cases,
// but this can be useful if you desactivated the parent group.
func (s *Server) Show() *Server {
s.DisableOpenapi = false
return s
}
// OutputOpenAPISpec takes the OpenAPI spec and outputs it to a JSON file and/or serves it on a URL.
// Also serves a Swagger UI.
// To modify its behavior, use the [WithOpenAPIConfig] option.
func (s *Server) OutputOpenAPISpec() openapi3.T {
// Validate
err := s.OpenApiSpec.Validate(context.Background())
if err != nil {
slog.Error("Error validating spec", "error", err)
}
// Marshal spec to JSON
jsonSpec, err := s.marshalSpec()
if err != nil {
slog.Error("Error marshalling spec to JSON", "error", err)
}
if !s.OpenAPIConfig.DisableSwagger {
registerOpenAPIRoutes(s, jsonSpec)
}
if !s.OpenAPIConfig.DisableLocalSave {
err := saveOpenAPIToFile(s.OpenAPIConfig.JsonFilePath, jsonSpec)
if err != nil {
slog.Error("Error saving spec to local path", "error", err, "path", s.OpenAPIConfig.JsonFilePath)
}
}
return s.OpenApiSpec
}
func (s *Server) marshalSpec() ([]byte, error) {
if s.OpenAPIConfig.PrettyFormatJson {
return json.MarshalIndent(s.OpenApiSpec, "", " ")
}
return json.Marshal(s.OpenApiSpec)
}
func saveOpenAPIToFile(jsonSpecLocalPath string, jsonSpec []byte) error {
jsonFolder := filepath.Dir(jsonSpecLocalPath)
err := os.MkdirAll(jsonFolder, 0o750)
if err != nil {
return errors.New("error creating docs directory")
}
f, err := os.Create(jsonSpecLocalPath) // #nosec G304 (file path provided by developer, not by user)
if err != nil {
return errors.New("error creating file")
}
defer f.Close()
_, err = f.Write(jsonSpec)
if err != nil {
return errors.New("error writing file ")
}
slog.Info("JSON file: " + jsonSpecLocalPath)
return nil
}
// Registers the routes to serve the OpenAPI spec and Swagger UI.
func registerOpenAPIRoutes(s *Server, jsonSpec []byte) {
GetStd(s, s.OpenAPIConfig.JsonUrl, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(jsonSpec)
})
Register(s, Route[any, any]{
Method: http.MethodGet,
Path: s.OpenAPIConfig.SwaggerUrl + "/",
}, s.OpenAPIConfig.UIHandler(s.OpenAPIConfig.JsonUrl))
slog.Info(fmt.Sprintf("JSON spec: http://%s%s", s.Server.Addr, s.OpenAPIConfig.JsonUrl))
slog.Info(fmt.Sprintf("OpenAPI UI: http://%s%s/index.html", s.Server.Addr, s.OpenAPIConfig.SwaggerUrl))
}
func validateJsonSpecLocalPath(jsonSpecLocalPath string) bool {
jsonSpecLocalPathRegexp := regexp.MustCompile(`^[^\/][\/a-zA-Z0-9\-\_]+(.json)$`)
return jsonSpecLocalPathRegexp.MatchString(jsonSpecLocalPath)
}
func validateJsonSpecUrl(jsonSpecUrl string) bool {
jsonSpecUrlRegexp := regexp.MustCompile(`^\/[\/a-zA-Z0-9\-\_]+(.json)$`)
return jsonSpecUrlRegexp.MatchString(jsonSpecUrl)
}
func validateSwaggerUrl(swaggerUrl string) bool {
swaggerUrlRegexp := regexp.MustCompile(`^\/[\/a-zA-Z0-9\-\_]+[a-zA-Z0-9\-\_]$`)
return swaggerUrlRegexp.MatchString(swaggerUrl)
}
var generator = openapi3gen.NewGenerator(
openapi3gen.UseAllExportedFields(),
)
// RegisterOpenAPIOperation registers an OpenAPI operation.
func RegisterOpenAPIOperation[T any, B any](s *Server, method, path string) (*openapi3.Operation, error) {
operation := openapi3.NewOperation()
operation.Tags = s.tags
// Tags
tag := tagFromType(*new(T))
if tag != "unknown-interface" {
operation.Tags = append(operation.Tags, tag)
}
// Request body
bodyTag := tagFromType(*new(B))
if (method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch) && bodyTag != "unknown-interface" && bodyTag != "string" {
bodySchema, ok := s.OpenApiSpec.Components.Schemas[bodyTag]
if !ok {
var err error
bodySchema, err = generator.NewSchemaRefForValue(new(B), s.OpenApiSpec.Components.Schemas)
if err != nil {
return operation, err
}
s.OpenApiSpec.Components.Schemas[bodyTag] = bodySchema
}
requestBody := openapi3.NewRequestBody().
WithRequired(true).
WithDescription("Request body for " + reflect.TypeOf(*new(B)).String())
if bodySchema != nil {
content := openapi3.NewContentWithSchema(bodySchema.Value, []string{"application/json"})
content["application/json"].Schema.Ref = "#/components/schemas/" + bodyTag
requestBody.WithContent(content)
}
s.OpenApiSpec.Components.RequestBodies[bodyTag] = &openapi3.RequestBodyRef{
Value: requestBody,
}
// add request body to operation
operation.RequestBody = &openapi3.RequestBodyRef{
Ref: "#/components/requestBodies/" + bodyTag,
Value: requestBody,
}
}
// Response body
responseSchema, ok := s.OpenApiSpec.Components.Schemas[tag]
if !ok {
var err error
responseSchema, err = generator.NewSchemaRefForValue(new(T), s.OpenApiSpec.Components.Schemas)
if err != nil {
return operation, err
}
s.OpenApiSpec.Components.Schemas[tag] = responseSchema
}
response := openapi3.NewResponse().WithDescription("OK")
if responseSchema != nil {
content := openapi3.NewContentWithSchema(responseSchema.Value, []string{"application/json"})
content["application/json"].Schema.Ref = "#/components/schemas/" + tag
response.WithContent(content)
}
operation.AddResponse(200, response)
// Path parameters
for _, pathParam := range parsePathParams(path) {
parameter := openapi3.NewPathParameter(pathParam)
parameter.Schema = openapi3.NewStringSchema().NewRef()
operation.AddParameter(parameter)
}
s.OpenApiSpec.AddOperation(path, method, operation)
return operation, nil
}
func tagFromType(v any) string {
if v == nil {
return "unknown-interface"
}
return dive(reflect.TypeOf(v), 4)
}
// dive returns the name of the type of the given reflect.Type.
// If the type is a pointer, slice, array, map, channel, function, or unsafe pointer,
// it will dive into the type and return the name of the type it points to.
func dive(t reflect.Type, maxDepth int) string {
switch t.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Array, reflect.Map, reflect.Chan, reflect.Func, reflect.UnsafePointer:
if maxDepth == 0 {
return "default"
}
return dive(t.Elem(), maxDepth-1)
default:
return t.Name()
}
}