-
Notifications
You must be signed in to change notification settings - Fork 28
/
operation_meta.go
138 lines (127 loc) · 3.93 KB
/
operation_meta.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
package openapi3
import (
"encoding/json"
"fmt"
"strings"
"github.com/grokify/simplego/type/stringsutil"
oas3 "github.com/getkin/kin-openapi/openapi3"
)
func OperationToMeta(url, method string, op *oas3.Operation) OperationMeta {
return OperationMeta{
OperationID: strings.TrimSpace(op.OperationID),
Summary: strings.TrimSpace(op.Summary),
Method: strings.ToUpper(strings.TrimSpace(method)),
Path: strings.TrimSpace(url),
Tags: op.Tags,
MetaNotes: []string{}}
}
// OperationMeta is used to hold additional information
// for a spec operation.
type OperationMeta struct {
OperationID string
DocsDescription string
DocsURL string
Method string
Path string
SecurityScopes []string
Summary string
Tags []string
MetaNotes []string
XThrottlingGroup string
}
func (om *OperationMeta) TrimSpace() {
om.OperationID = strings.TrimSpace(om.OperationID)
om.DocsURL = strings.TrimSpace(om.DocsURL)
om.DocsDescription = strings.TrimSpace(om.DocsDescription)
om.SecurityScopes = stringsutil.SliceCondenseSpace(om.SecurityScopes, true, false)
om.Tags = stringsutil.SliceCondenseSpace(om.Tags, true, false)
om.XThrottlingGroup = strings.TrimSpace(om.XThrottlingGroup)
}
func OperationSetRequestBodySchemaRef(op *oas3.Operation, mediaType string, schemaRef *oas3.SchemaRef) {
if op.RequestBody == nil {
op.RequestBody = &oas3.RequestBodyRef{}
}
if op.RequestBody.Value == nil {
op.RequestBody.Value = &oas3.RequestBody{
Content: oas3.NewContent()}
}
op.RequestBody.Value.Content[mediaType] = oas3.NewMediaType().WithSchemaRef(schemaRef)
}
/*
op.RequestBody = &oas3.RequestBodyRef{
Value: &oas3.RequestBody{
Content: map[string]*oas3.MediaType{
"application/json": &oas3.MediaType{
Schema: &oas3.SchemaRef{
Ref: ref,
},
// Example: //
},
},
},
}
*/
func OperationSetResponseBodySchemaRef(op *oas3.Operation, status, description, mediaType string, schemaRef *oas3.SchemaRef) error {
description = strings.TrimSpace(description)
if len(description) == 0 {
return fmt.Errorf("no response description for operationId [%s]", op.OperationID)
}
if op.Responses == nil {
op.Responses = oas3.Responses{}
}
status = strings.TrimSpace(status)
mediaType = strings.ToLower(strings.TrimSpace(mediaType))
if _, ok := op.Responses[status]; !ok || op.Responses[status] == nil {
op.Responses[status] = &oas3.ResponseRef{}
}
resRef, _ := op.Responses[status]
if resRef.Value == nil {
resRef.Value = &oas3.Response{}
}
resRef.Value.Description = &description
if resRef.Value.Content == nil {
resRef.Value.Content = oas3.NewContent()
}
resRef.Value.Content[mediaType] = oas3.NewMediaType().WithSchemaRef(schemaRef)
return nil
}
// OperationSecurityScopes retrieves a flat list of security scopes for
// an operation.
func OperationSecurityScopes(op *oas3.Operation, fullyQualified bool) []string {
securityScopes := []string{}
if op == nil || op.Security == nil {
return securityScopes
}
seqReqRaw := SecurityRequirementsToRaw(*op.Security)
for _, secReq := range seqReqRaw {
for secSchemeName, scopes := range secReq {
if fullyQualified {
secSchemeNameTrimmed := strings.TrimSpace(secSchemeName)
for _, scope := range scopes {
scope = strings.TrimSpace(scope)
if len(scope) > 0 {
securityScopes = append(securityScopes,
secSchemeNameTrimmed+"."+scope)
}
}
} else {
securityScopes = append(securityScopes, scopes...)
}
}
}
return stringsutil.SliceCondenseSpace(securityScopes, true, false)
}
// SecurityRequirementsToRaw returns a raw SecurityRequirements slice
// to be used for iterating over elements.
func SecurityRequirementsToRaw(secReqs oas3.SecurityRequirements) []map[string][]string {
bytes, err := json.Marshal(secReqs)
if err != nil {
panic(err)
}
raw := []map[string][]string{}
err = json.Unmarshal(bytes, &raw)
if err != nil {
panic(err)
}
return raw
}