-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpaths.go
More file actions
268 lines (232 loc) · 7.58 KB
/
paths.go
File metadata and controls
268 lines (232 loc) · 7.58 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
package main
import (
"fmt"
"sort"
"strings"
"github.com/getkin/kin-openapi/openapi3"
)
// generatePathsTable generates a markdown table of all paths with links
// Sample output:
// ## Paths
//
// | Path | Operations |
// | ----------------------------- | ---------------- |
// | [/users](#path/users) | get, post |
// | [/users/{id}](#path/users/id) | get, put, delete |
func generatePathsTable(paths *openapi3.Paths) string {
var sb strings.Builder
sb.WriteString(pathsSection)
sb.WriteString(newlineDouble)
sb.WriteString(pathsTableHeader)
// Sort paths by path name
pathsMap := paths.Map()
pathNames := make([]string, 0, len(pathsMap))
for path := range pathsMap {
pathNames = append(pathNames, path)
}
sort.Strings(pathNames)
for _, path := range pathNames {
pathItem := pathsMap[path]
sb.WriteString("| [" + path + "](" + pathPrefix + strings.ToLower(path) + ") | ")
operations := pathItem.Operations()
methods := make([]string, 0, len(operations))
for method := range operations {
methods = append(methods, method)
}
sort.Strings(methods)
sb.WriteString(strings.Join(methods, ", ") + " |\n")
}
sb.WriteString(newlineDouble)
return sb.String()
}
// generatePathsDocumentation generates detailed markdown for each path and operation
// Sample output:
// ## <span id="path/users">/users</span>
//
// ### GET
// Get all users
//
// **Parameters:**
// | Name | Required | Type | Description | Example |
// | ----- | -------- | ------- | ----------- | ------- |
// | limit | false | integer | Max results | 10 |
//
// **Responses:**
// | Status Code | Description |
// | ----------- | ------------------------------------- |
// | 200 | [Success response](#/definitions/User) |
//
// ---
//
// ### POST
// Create new user
//
// **Request Body:**
// | Name | Required | Type | Description | Example |
// | ---- | -------- | ------ | ----------- | ------- |
// | User | true | object | User data | {...} |
//
// **Responses:**
// | Status Code | Description |
// | ----------- | ------------------------------------- |
// | 201 | [User created](#/definitions/User) |
//
// ---
//
// ## <span id="path/users/{id}">/users/{id}</span>
//
// ### GET
// Get user by ID
// ...
func generatePathsDocumentation(paths *openapi3.Paths) string {
var sb strings.Builder
// Sort paths by path name
pathsMap := paths.Map()
pathNames := make([]string, 0, len(pathsMap))
for path := range pathsMap {
pathNames = append(pathNames, path)
}
sort.Strings(pathNames)
// Generate Markdown for each path
for _, path := range pathNames {
pathItem := pathsMap[path]
sb.WriteString("## <span id=\"path" + path + "\">" + path + "</span>\n\n")
// Generate Markdown for each HTTP method in the path
operations := pathItem.Operations()
methods := make([]string, 0, len(operations))
for method := range operations {
methods = append(methods, method)
}
sort.Strings(methods)
for _, method := range methods {
operation := operations[method]
sb.WriteString(generateOperationDocumentation(method, operation))
}
}
return sb.String()
}
// generateOperationDocumentation generates markdown for a single operation
// Sample output:
// ### GET
// Get user by ID
//
// **Parameters:**
// | Name | Required | Type | Description | Example |
// | ---- | -------- | ------ | ----------- | ------- |
// | id | true | string | User ID | 123 |
//
// **Responses:**
// | Status Code | Description |
// | ----------- | ------------------------------------- |
// | 200 | [Success response](#/definitions/User) |
func generateOperationDocumentation(method string, operation *openapi3.Operation) string {
var sb strings.Builder
sb.WriteString("### " + method)
sb.WriteString(newlineDouble)
sb.WriteString(operation.Description)
sb.WriteString(newlineDouble)
sb.WriteString(generateParametersTable(operation.Parameters))
sb.WriteString(generateRequestBodyTable(operation.RequestBody))
sb.WriteString(generateResponsesTable(operation.Responses))
sb.WriteString(sectionSeparator) // Add a separator between each method
return sb.String()
}
// generateParametersTable generates a markdown table for operation parameters
// Sample output:
// **Parameters:**
//
// | Name | Required | Type | Description | Example |
// | ----- | -------- | ------- | ----------- | ------- |
// | id | true | string | User ID | 123 |
// | limit | false | integer | Max results | 10 |
func generateParametersTable(parameters openapi3.Parameters) string {
var sb strings.Builder
sb.WriteString(parametersSection)
sb.WriteString(newlineDouble)
sb.WriteString(paramsTableHeader)
for _, parameter := range parameters {
if parameter.Value != nil {
sb.WriteString("| " + parameter.Value.Name + " | ")
sb.WriteString(fmt.Sprintf("%v", parameter.Value.Required) + " | ")
typ, ok := parameter.Value.Extensions["type"]
if ok {
sb.WriteString(typ.(string) + " | ")
} else {
sb.WriteString(" | ")
}
sb.WriteString(oneleline(parameter.Value.Description) + " | ")
sb.WriteString(fmt.Sprintf("%v", parameter.Value.Example) + " |\n")
}
}
return sb.String()
}
// generateRequestBodyTable generates a markdown table for request body
// Sample output:
//
// **Request Body:**
//
// | Name | Required | Type | Description | Example |
// | ---- | -------- | ------ | ----------- | ------- |
// | User | true | object | User data | {...} |
func generateRequestBodyTable(requestBody *openapi3.RequestBodyRef) string {
if requestBody == nil {
return ""
}
var sb strings.Builder
sb.WriteString(newlineDouble)
sb.WriteString(requestBodySection)
sb.WriteString(newlineDouble)
sb.WriteString(paramsTableHeader)
for _, mediaType := range requestBody.Value.Content {
sb.WriteString("| " + mediaType.Schema.Value.Title + " | ")
sb.WriteString(fmt.Sprintf("%v", mediaType.Schema.Value.Required) + " | ")
typeStr := ""
if mediaType.Schema.Value.Type != nil && len(*mediaType.Schema.Value.Type) > 0 {
typeStr = (*mediaType.Schema.Value.Type)[0]
}
sb.WriteString(typeStr + " | ")
sb.WriteString(oneleline(mediaType.Schema.Value.Description) + " | ")
sb.WriteString(fmt.Sprintf("%v", mediaType.Schema.Value.Example) + " |\n")
}
return sb.String()
}
// generateResponsesTable generates a markdown table for responses
// Sample output:
//
// **Responses:**
//
// | Status Code | Description |
// | ----------- | --------------------------------------- |
// | 200 | [Success response](#/definitions/User) |
// | 404 | [User not found](#/definitions/Error) |
func generateResponsesTable(responses *openapi3.Responses) string {
var sb strings.Builder
sb.WriteString(newline)
sb.WriteString(responsesSection)
sb.WriteString(newlineDouble)
sb.WriteString(responsesTableHeader)
// Sort responses by status code
responsesMap := responses.Map()
statusCodes := make([]string, 0, len(responsesMap))
for statusCode := range responsesMap {
statusCodes = append(statusCodes, statusCode)
}
sort.Strings(statusCodes)
for _, statusCode := range statusCodes {
response := responsesMap[statusCode]
description := ""
if response.Value.Description != nil {
description = oneleline(*response.Value.Description)
}
linkToSchema := ""
ext, ok := response.Value.Extensions["schema"]
if ok {
ref, ok := ext.(map[string]any)["$ref"]
if ok {
linkToSchema = ref.(string)
}
}
sb.WriteString("| " + statusCode + " | [" + description + "](" + linkToSchema + ") |\n")
}
return sb.String()
}