-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi.go
More file actions
151 lines (138 loc) · 3.68 KB
/
openapi.go
File metadata and controls
151 lines (138 loc) · 3.68 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
package puff
import (
_ "embed"
"net/http"
"reflect"
"regexp"
"slices"
"strconv"
"strings"
)
//go:embed static/openAPI.html
var openAPIHTML string
var Schemas = make(SchemaDefinition)
func parameterToRequestBodyOrReference(p Parameter) RequestBodyOrReference {
m := make(map[string]MediaType)
s := p.Schema
if p.Schema.Ref != "" {
s = &Schema{Ref: p.Schema.Ref}
}
m["application/json"] = MediaType{
Schema: s, // schema with just ref or entire schema
}
requestBody := RequestBodyOrReference{
Reference: "",
Description: p.Description,
Content: m,
Required: p.Required,
}
return requestBody
}
func addRoute(route *Route, tags *[]Tag, tagNames *[]string, paths *Paths) *Paths {
tag := route.Router.Tag //FIXME: tag on route should not just be tag on router
if tag == "" {
tag = route.Router.Name
}
if !slices.Contains(*tagNames, tag) {
*tagNames = append(*tagNames, tag)
*tags = append(*tags, Tag{Name: tag, Description: ""})
}
parameters := []Parameter{}
var requestBody RequestBodyOrReference
for _, p := range route.params {
if p.In == "body" {
requestBody = parameterToRequestBodyOrReference(p)
continue
}
if p.In == "file" {
requestBody = RequestBodyOrReference{
Content: map[string]MediaType{
"multipart/form-data": {
Schema: &Schema{
Type: "object",
Required: []string{p.Name},
Properties: map[string]*Schema{
p.Name: {
Type: "string",
Format: "binary",
},
},
},
},
},
}
continue
}
np := Parameter{
Name: p.Name,
Description: p.Description,
Required: p.Required,
In: p.In,
Deprecated: p.Deprecated,
}
np.Schema = p.Schema
parameters = append(parameters, np)
}
pathMethod := &Operation{
Summary: generateSummary(*route),
OperationID: generateOperationId(*route),
Tags: []string{tag},
Parameters: parameters, //NOTE: check json struct tag on ParameterOrReference
RequestBody: &requestBody,
Responses: convertRouteResponsestoOpenAPIResponses(*route),
Description: route.Description,
Callbacks: map[string]Callback{},
}
pathItem := (*paths)[route.fullPath]
switch route.Protocol {
// TODO: handle other protocols
case http.MethodGet:
pathItem.Get = pathMethod
// explicity remove request body for GET requests
pathItem.Get.RequestBody = nil
case http.MethodPost:
pathItem.Post = pathMethod
case http.MethodPut:
pathItem.Put = pathMethod
case http.MethodPatch:
pathItem.Patch = pathMethod
case http.MethodDelete:
pathItem.Delete = pathMethod
}
(*paths)[route.fullPath] = pathItem
return paths
}
func convertRouteResponsestoOpenAPIResponses(route Route) map[string]OpenAPIResponse {
// FIXME: description can potentially be pulled from a map
openAPIResponses := map[string]OpenAPIResponse{}
for statusCode, res := range route.Responses {
sc := strconv.Itoa(statusCode)
realRes := reflect.New(res()).Interface()
schema := newDefinition(&route, realRes)
openAPIResponses[sc] = OpenAPIResponse{
Content: map[string]MediaType{
"application/json": {Schema: schema},
},
Description: http.StatusText(statusCode),
}
}
return openAPIResponses
}
func generateOperationId(r Route) string {
path := r.fullPath
re := regexp.MustCompile(`/([a-zA-Z])`)
normalizedPath := re.ReplaceAllStringFunc(path, func(match string) string {
// Extract the character after "/"
char := match[1:]
// Capitalize the character and return it
return strings.ToUpper(char)
})
return strings.ToLower(r.Protocol) + normalizedPath
}
func generateSummary(r Route) string {
summary := r.Description
if len(summary) > 100 {
summary = summary[:97] + " ..."
}
return summary
}