forked from pangpanglabs/echoswagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.go
181 lines (160 loc) · 3.79 KB
/
spec.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
package echoswagger
import (
"encoding/xml"
"net/http"
"net/url"
"reflect"
"github.com/labstack/echo"
)
const (
DefPrefix = "#/definitions/"
SwaggerVersion = "2.0"
SpecName = "swagger.json"
)
func (r *Root) specHandler(docPath string) echo.HandlerFunc {
return func(c echo.Context) error {
spec, err := r.GetSpec(c, docPath)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
var basePath string
if uri, err := url.ParseRequestURI(c.Request().Referer()); err == nil {
basePath = trimSuffixSlash(uri.Path, docPath)
spec.Host = uri.Host
} else {
basePath = trimSuffixSlash(c.Request().URL.Path, connectPath(docPath, SpecName))
spec.Host = c.Request().Host
}
spec.BasePath = basePath
return c.JSON(http.StatusOK, spec)
}
}
// Generate swagger spec data, without host & basePath info
func (r *Root) GetSpec(c echo.Context, docPath string) (Swagger, error) {
r.once.Do(func() {
r.err = r.genSpec(c)
r.cleanUp()
})
if r.err != nil {
return Swagger{}, r.err
}
return *r.spec, nil
}
func (r *Root) genSpec(c echo.Context) error {
r.spec.Swagger = SwaggerVersion
r.spec.Paths = make(map[string]interface{})
for i := range r.groups {
group := &r.groups[i]
r.spec.Tags = append(r.spec.Tags, &group.tag)
for j := range group.apis {
a := &group.apis[j]
if err := a.operation.addSecurity(r.spec.SecurityDefinitions, group.security); err != nil {
return err
}
if err := r.transfer(a); err != nil {
return err
}
}
}
for i := range r.apis {
if err := r.transfer(&r.apis[i]); err != nil {
return err
}
}
for k, v := range *r.defs {
r.spec.Definitions[k] = v.Schema
}
return nil
}
func (r *Root) transfer(a *api) error {
if err := a.operation.addSecurity(r.spec.SecurityDefinitions, a.security); err != nil {
return err
}
path := toSwaggerPath(a.route.Path)
if len(a.operation.Responses) == 0 {
a.operation.Responses["default"] = &Response{
Description: "successful operation",
}
}
if p, ok := r.spec.Paths[path]; ok {
p.(*Path).oprationAssign(a.route.Method, &a.operation)
} else {
p := &Path{}
p.oprationAssign(a.route.Method, &a.operation)
r.spec.Paths[path] = p
}
return nil
}
func (p *Path) oprationAssign(method string, operation *Operation) {
switch method {
case echo.GET:
p.Get = operation
case echo.POST:
p.Post = operation
case echo.PUT:
p.Put = operation
case echo.DELETE:
p.Delete = operation
case echo.OPTIONS:
p.Options = operation
case echo.HEAD:
p.Head = operation
case echo.PATCH:
p.Patch = operation
}
}
func (r *Root) cleanUp() {
r.echo = nil
r.groups = nil
r.apis = nil
r.defs = nil
}
// addDefinition adds definition specification and returns
// key of RawDefineDic
func (r *RawDefineDic) addDefinition(v reflect.Value) string {
exist, key := r.getKey(v)
if exist {
return key
}
schema := &JSONSchema{
Type: "object",
Properties: make(map[string]*JSONSchema),
}
(*r)[key] = RawDefine{
Value: v,
Schema: schema,
}
r.handleStruct(v, schema)
if schema.XML == nil {
schema.XML = &XMLSchema{}
}
if schema.XML.Name == "" {
schema.XML.Name = v.Type().Name()
}
return key
}
// handleStruct handles fields of a struct
func (r *RawDefineDic) handleStruct(v reflect.Value, schema *JSONSchema) {
for i := 0; i < v.NumField(); i++ {
f := v.Type().Field(i)
name, hasTag := getFieldName(f, ParamInBody)
if name == "-" {
continue
}
if f.Type == reflect.TypeOf(xml.Name{}) {
schema.handleXMLTags(f)
continue
}
if f.Type.Kind() == reflect.Struct && f.Anonymous && !hasTag {
r.handleStruct(v.Field(i), schema)
continue
}
sp := r.genSchema(v.Field(i))
sp.handleXMLTags(f)
if sp.XML != nil {
sp.handleChildXMLTags(sp.XML.Name, r)
}
schema.Properties[name] = sp
schema.handleSwaggerTags(f, name)
}
}