|
1
|
|
|
package loader |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"net/url" |
|
5
|
|
|
|
|
6
|
|
|
"github.com/getkin/kin-openapi/openapi3" |
|
7
|
|
|
) |
|
8
|
|
|
|
|
9
|
|
|
type processingLoader struct { |
|
10
|
|
|
loader SpecificationLoader |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
func (loader *processingLoader) LoadFromURI(uri string) (*openapi3.Swagger, error) { |
|
14
|
1 |
|
specification, err := loader.loader.LoadFromURI(uri) |
|
15
|
1 |
|
if err != nil { |
|
16
|
|
|
return nil, err |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
1 |
|
loader.processServers(&specification.Servers) |
|
20
|
|
|
|
|
21
|
1 |
|
for i := range specification.Paths { |
|
22
|
1 |
|
loader.processServers(&specification.Paths[i].Servers) |
|
23
|
1 |
|
loader.processOperation(specification.Paths[i].Get) |
|
24
|
1 |
|
loader.processOperation(specification.Paths[i].Connect) |
|
25
|
1 |
|
loader.processOperation(specification.Paths[i].Delete) |
|
26
|
1 |
|
loader.processOperation(specification.Paths[i].Get) |
|
27
|
1 |
|
loader.processOperation(specification.Paths[i].Head) |
|
28
|
1 |
|
loader.processOperation(specification.Paths[i].Options) |
|
29
|
1 |
|
loader.processOperation(specification.Paths[i].Patch) |
|
30
|
1 |
|
loader.processOperation(specification.Paths[i].Post) |
|
31
|
1 |
|
loader.processOperation(specification.Paths[i].Put) |
|
32
|
1 |
|
loader.processOperation(specification.Paths[i].Trace) |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
return specification, nil |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
func (loader *processingLoader) processServers(servers *openapi3.Servers) { |
|
39
|
1 |
|
if servers == nil { |
|
40
|
|
|
return |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
for i := range *servers { |
|
44
|
1 |
|
serverURL, err := url.Parse((*servers)[i].URL) |
|
45
|
1 |
|
if err != nil { |
|
46
|
|
|
continue |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// server urls need to be cleaned for proper routing |
|
50
|
1 |
|
path := serverURL.Path |
|
51
|
1 |
|
if path == "" { |
|
52
|
1 |
|
path = "/" |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
(*servers)[i].URL = path |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
func (loader *processingLoader) processOperation(operation *openapi3.Operation) { |
|
60
|
1 |
|
if operation != nil { |
|
61
|
1 |
|
loader.processServers(operation.Servers) |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|