|
1 | 1 | package shiftapi |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "reflect" |
| 7 | + |
4 | 8 | "github.com/getkin/kin-openapi/openapi3" |
5 | 9 | ) |
6 | 10 |
|
7 | | -type Info struct { |
8 | | - Summary string |
9 | | - Title string |
10 | | - Description string |
11 | | - TermsOfService string |
12 | | - Contact *Contact |
13 | | - License *License |
14 | | - Version string |
15 | | -} |
| 11 | +func (s *ShiftAPI) updateSchema(method, path string, inType, outType reflect.Type) error { |
16 | 12 |
|
17 | | -type Contact struct { |
18 | | - Name string |
19 | | - URL string |
20 | | - Email string |
21 | | -} |
| 13 | + inSchema, err := s.generateSchemaRef(inType) |
| 14 | + if err != nil { |
| 15 | + return err |
| 16 | + } |
22 | 17 |
|
23 | | -type License struct { |
24 | | - Name string |
25 | | - URL string |
26 | | - Identifier string |
27 | | -} |
| 18 | + outSchema, err := s.generateSchemaRef(outType) |
| 19 | + if err != nil { |
| 20 | + return err |
| 21 | + } |
| 22 | + responses := openapi3.NewResponses() |
| 23 | + responseContent := make(map[string]*openapi3.MediaType) |
| 24 | + responseContent["application/json"] = &openapi3.MediaType{ |
| 25 | + Schema: &openapi3.SchemaRef{ |
| 26 | + Ref: fmt.Sprintf("#/components/schemas/%s", outSchema.Ref), |
| 27 | + }, |
| 28 | + } |
| 29 | + responses.Set("200", &openapi3.ResponseRef{ |
| 30 | + Value: &openapi3.Response{ |
| 31 | + Description: String("Success"), |
| 32 | + Content: responseContent, |
| 33 | + }, |
| 34 | + }) |
28 | 35 |
|
29 | | -func WithInfo(info Info) func(*ShiftAPI) *ShiftAPI { |
30 | | - return func(api *ShiftAPI) *ShiftAPI { |
31 | | - api.spec.Info = &openapi3.Info{ |
32 | | - Title: info.Title, |
33 | | - Description: info.Description, |
34 | | - Version: info.Version, |
35 | | - } |
36 | | - if info.Contact != nil { |
37 | | - api.spec.Info.Contact = &openapi3.Contact{ |
38 | | - Name: info.Contact.Name, |
39 | | - URL: info.Contact.URL, |
40 | | - Email: info.Contact.Email, |
41 | | - } |
42 | | - } |
43 | | - if info.License != nil { |
44 | | - api.spec.Info.License = &openapi3.License{ |
45 | | - Name: info.License.Name, |
46 | | - URL: info.License.URL, |
47 | | - } |
48 | | - } |
49 | | - return api |
| 36 | + requestContent := make(map[string]*openapi3.MediaType) |
| 37 | + requestContent["application/json"] = &openapi3.MediaType{ |
| 38 | + Schema: &openapi3.SchemaRef{ |
| 39 | + Ref: fmt.Sprintf("#/components/schemas/%s", inSchema.Ref), |
| 40 | + }, |
| 41 | + } |
| 42 | + requestBody := &openapi3.RequestBodyRef{ |
| 43 | + Value: &openapi3.RequestBody{ |
| 44 | + Content: requestContent, |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + pathItem := s.spec.Paths.Find(path) |
| 49 | + if pathItem == nil { |
| 50 | + pathItem = &openapi3.PathItem{} |
| 51 | + s.spec.Paths.Set(path, pathItem) |
| 52 | + } |
| 53 | + |
| 54 | + op := &openapi3.Operation{ |
| 55 | + // Summary: opts.Summary, |
| 56 | + RequestBody: requestBody, |
| 57 | + // Description: opts.Description, |
| 58 | + Responses: responses, |
| 59 | + } |
| 60 | + |
| 61 | + switch method { |
| 62 | + case http.MethodGet: |
| 63 | + pathItem.Get = op |
| 64 | + case http.MethodPost: |
| 65 | + pathItem.Post = op |
| 66 | + case http.MethodPut: |
| 67 | + pathItem.Put = op |
| 68 | + case http.MethodDelete: |
| 69 | + pathItem.Delete = op |
| 70 | + case http.MethodPatch: |
| 71 | + pathItem.Patch = op |
| 72 | + case http.MethodHead: |
| 73 | + pathItem.Head = op |
| 74 | + case http.MethodOptions: |
| 75 | + pathItem.Options = op |
| 76 | + default: |
| 77 | + return fmt.Errorf("method '%s' not supported", method) |
| 78 | + } |
| 79 | + // s.spec.Components.Responses.Default() |
| 80 | + |
| 81 | + s.spec.Components.Schemas[inSchema.Ref] = &openapi3.SchemaRef{ |
| 82 | + Value: inSchema.Value, |
| 83 | + } |
| 84 | + s.spec.Components.Schemas[outSchema.Ref] = &openapi3.SchemaRef{ |
| 85 | + Value: outSchema.Value, |
50 | 86 | } |
| 87 | + return nil |
51 | 88 | } |
52 | 89 |
|
53 | | -type ExternalDocs struct { |
54 | | - Description string |
55 | | - URL string |
| 90 | +func (s *ShiftAPI) generateSchemaRef(t reflect.Type) (*openapi3.SchemaRef, error) { |
| 91 | + schema, err := s.specGen.GenerateSchemaRef(t) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + // TODO why tf does kin set ref values for basic types |
| 97 | + scrubRefs(schema) |
| 98 | + |
| 99 | + return schema, nil |
56 | 100 | } |
57 | 101 |
|
58 | | -func WithExternalDocs(externalDocs ExternalDocs) func(*ShiftAPI) *ShiftAPI { |
59 | | - return func(api *ShiftAPI) *ShiftAPI { |
60 | | - api.spec.ExternalDocs = &openapi3.ExternalDocs{ |
61 | | - Description: externalDocs.Description, |
62 | | - URL: externalDocs.URL, |
| 102 | +func scrubRefs(s *openapi3.SchemaRef) { |
| 103 | + if s.Value.Properties == nil || len(s.Value.Properties) <= 0 { |
| 104 | + return |
| 105 | + } |
| 106 | + for _, p := range s.Value.Properties { |
| 107 | + if !p.Value.Type.Is("object") { |
| 108 | + p.Ref = "" |
63 | 109 | } |
64 | | - return api |
65 | 110 | } |
66 | 111 | } |
0 commit comments