-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
110 lines (95 loc) · 2.72 KB
/
generator.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
package ez
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3gen"
"sigs.k8s.io/yaml"
)
type DocsGenerator struct {
server *EZServer
docs OpenAPIDocs
}
type OpenAPIDocs struct {
Components openapi3.Components `json:"components,omitempty" yaml:"components,omitempty"`
Paths openapi3.Paths `json:"paths,omitempty" yaml:"paths,omitempty"`
}
func (g *DocsGenerator) GenerateDocs() {
fmt.Println("Generating docs...")
components := openapi3.NewComponents()
components.Schemas = make(map[string]*openapi3.SchemaRef)
paths := make(map[string]*openapi3.PathItem)
g.docs = OpenAPIDocs{
Components: components,
Paths: paths,
}
for _, route := range g.server.GetRoutes() {
g.GenerateDocsForRoute(route)
}
fmt.Println("Creating files...")
g.GenerateOpenAPIFiles()
fmt.Println("Docs generated!")
}
func (g *DocsGenerator) GenerateDocsForRoute(route Route) {
var reqSchema *openapi3.SchemaRef
var resSchema *openapi3.SchemaRef
var err error
if route.Request != nil {
reqSchema, err := openapi3gen.NewSchemaRefForValue(route.Request, nil)
if err != nil {
panic(err)
}
t := reflect.TypeOf(route.Request)
g.docs.Components.Schemas[t.Name()] = reqSchema
}
if route.Response != nil {
resSchema, err = openapi3gen.NewSchemaRefForValue(route.Response, nil)
if err != nil {
panic(err)
}
t := reflect.TypeOf(route.Response)
g.docs.Components.Schemas[t.Name()] = resSchema
}
g.docs.Paths[route.Pattern] = &openapi3.PathItem{}
for _, method := range route.Method {
if method == http.MethodGet {
g.docs.Paths[route.Pattern].Get = &openapi3.Operation{
OperationID: fmt.Sprintf("%s-%s", route.Pattern, method),
Parameters: []*openapi3.ParameterRef{},
Responses: map[string]*openapi3.ResponseRef{},
}
}
if route.Request != nil {
g.docs.Paths[route.Pattern].Get.RequestBody = &openapi3.RequestBodyRef{
Value: &openapi3.RequestBody{
Required: true,
},
}
g.docs.Paths[route.Pattern].Get.RequestBody.Value.Content["application/json"] = &openapi3.MediaType{
ExtensionProps: openapi3.ExtensionProps{},
Schema: reqSchema,
}
}
}
}
func (g *DocsGenerator) GenerateOpenAPIFiles() {
b := &bytes.Buffer{}
err := json.NewEncoder(b).Encode(g.docs)
checkError(err)
schema, err := yaml.JSONToYAML(b.Bytes())
checkError(err)
b = &bytes.Buffer{}
b.Write(schema)
doc, err := openapi3.NewLoader().LoadFromData(b.Bytes())
checkError(err)
jsonB, err := json.MarshalIndent(doc, "", " ")
checkError(err)
err = ioutil.WriteFile("./openapi.json", jsonB, 0666)
checkError(err)
err = ioutil.WriteFile("./openapi.yaml", b.Bytes(), 0666)
checkError(err)
}