-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.go
118 lines (104 loc) · 2.63 KB
/
schema.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
package shiftapi
import (
"fmt"
"net/http"
"reflect"
"github.com/getkin/kin-openapi/openapi3"
)
func (s *ShiftAPI) updateSchema(method, path string, inType, outType reflect.Type, handlerInfo *HandlerInfo) error {
inSchema, err := s.generateSchemaRef(inType)
if err != nil {
return err
}
outSchema, err := s.generateSchemaRef(outType)
if err != nil {
return err
}
responses := openapi3.NewResponses()
responseContent := make(map[string]*openapi3.MediaType)
responseContent["application/json"] = &openapi3.MediaType{
Schema: &openapi3.SchemaRef{
Ref: fmt.Sprintf("#/components/schemas/%s", outSchema.Ref),
},
}
responses.Set("200", &openapi3.ResponseRef{
Value: &openapi3.Response{
Description: String("Success"),
Content: responseContent,
},
})
requestContent := make(map[string]*openapi3.MediaType)
requestContent["application/json"] = &openapi3.MediaType{
Schema: &openapi3.SchemaRef{
Ref: fmt.Sprintf("#/components/schemas/%s", inSchema.Ref),
},
}
requestBody := &openapi3.RequestBodyRef{
Value: &openapi3.RequestBody{
Content: requestContent,
},
}
pathItem := s.spec.Paths.Find(path)
if pathItem == nil {
pathItem = &openapi3.PathItem{}
s.spec.Paths.Set(path, pathItem)
}
op := &openapi3.Operation{
RequestBody: requestBody,
Responses: responses,
}
if handlerInfo != nil {
op.Summary = handlerInfo.Summary
op.Description = handlerInfo.Description
op.Tags = handlerInfo.Tags
}
switch method {
case http.MethodGet:
pathItem.Get = op
case http.MethodPost:
pathItem.Post = op
case http.MethodPut:
pathItem.Put = op
case http.MethodDelete:
pathItem.Delete = op
case http.MethodPatch:
pathItem.Patch = op
case http.MethodHead:
pathItem.Head = op
case http.MethodOptions:
pathItem.Options = op
case http.MethodTrace:
pathItem.Trace = op
case http.MethodConnect:
pathItem.Connect = op
default:
return fmt.Errorf("method '%s' not supported", method)
}
// s.spec.Components.Responses.Default()
s.spec.Components.Schemas[inSchema.Ref] = &openapi3.SchemaRef{
Value: inSchema.Value,
}
s.spec.Components.Schemas[outSchema.Ref] = &openapi3.SchemaRef{
Value: outSchema.Value,
}
return nil
}
func (s *ShiftAPI) generateSchemaRef(t reflect.Type) (*openapi3.SchemaRef, error) {
schema, err := s.specGen.GenerateSchemaRef(t)
if err != nil {
return nil, err
}
// TODO why tf does kin set ref values for basic types
scrubRefs(schema)
return schema, nil
}
func scrubRefs(s *openapi3.SchemaRef) {
if s.Value.Properties == nil || len(s.Value.Properties) <= 0 {
return
}
for _, p := range s.Value.Properties {
if !p.Value.Type.Is("object") {
p.Ref = ""
}
}
}