Skip to content

Commit 9a450a3

Browse files
committed
refactor: openapi3edit: combine OperationMore and OperationEditor
1 parent 745fb1e commit 9a450a3

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/getkin/kin-openapi v0.60.0
88
github.com/ghodss/yaml v1.0.0
99
github.com/grokify/gocharts v1.3.0
10-
github.com/grokify/simplego v0.25.4
10+
github.com/grokify/simplego v0.26.1
1111
github.com/jessevdk/go-flags v1.5.0
1212
github.com/pkg/errors v0.9.1
1313
github.com/rs/zerolog v1.21.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ github.com/grokify/gocharts v1.3.0 h1:/M9cUup80jQtXBPb6RoPQJrGpb/90PFI5IZ58t5cJk
9393
github.com/grokify/gocharts v1.3.0/go.mod h1:uhe0lH04KtJuYRuzreaujjYZ/8x53Numw8JN/sa2/fw=
9494
github.com/grokify/simplego v0.0.3/go.mod h1:mOeVoV0XRtBVrkTxFdN4e9u7yeiRurhCtxsk+CA7gO4=
9595
github.com/grokify/simplego v0.0.21/go.mod h1:8BYycDukEZvM7jyu8mfG9On4/pG6fEhPYlE3TBsB/Xw=
96-
github.com/grokify/simplego v0.25.4 h1:Gq6aOcs+uiYTwT6ubkK/WzMZQ0wH9B/MA0CTZMTuRL4=
97-
github.com/grokify/simplego v0.25.4/go.mod h1:B24I7pmy89MLOQ2ySaigcsrNEe+DXK7opD2LMK2LJeg=
96+
github.com/grokify/simplego v0.26.1 h1:T/mjgK4PXYFgj0IsAFGRwtN6+Ts8poM5zR80jTA0fak=
97+
github.com/grokify/simplego v0.26.1/go.mod h1:B24I7pmy89MLOQ2ySaigcsrNEe+DXK7opD2LMK2LJeg=
9898
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
9999
github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
100100
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=

openapi3edit/operations.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,38 @@ import (
99
"github.com/grokify/swaggman/openapi3"
1010
)
1111

12-
type OperationEditor struct {
12+
type OperationMore struct {
13+
UrlPath string
14+
Method string
1315
Operation *oas3.Operation
1416
}
1517

16-
func (oedit *OperationEditor) AddExternalDocs(docURL, docDescription string, preserveIfReqEmpty bool) {
17-
operationAddExternalDocs(oedit.Operation, docURL, docDescription, preserveIfReqEmpty)
18+
func (opm *OperationMore) AddExternalDocs(docURL, docDescription string, preserveIfReqEmpty bool) {
19+
operationAddExternalDocs(opm.Operation, docURL, docDescription, preserveIfReqEmpty)
1820
}
1921

20-
func (oedit *OperationEditor) AddRequestBodySchemaRef(description string, required bool, contentType string, schemaRef *oas3.SchemaRef) error {
21-
return operationAddRequestBodySchemaRef(oedit.Operation, description, required, contentType, schemaRef)
22+
func (opm *OperationMore) AddRequestBodySchemaRef(description string, required bool, contentType string, schemaRef *oas3.SchemaRef) error {
23+
return operationAddRequestBodySchemaRef(opm.Operation, description, required, contentType, schemaRef)
2224
}
2325

24-
func (oedit *OperationEditor) AddResponseBodySchemaRef(statusCode, description, contentType string, schemaRef *oas3.SchemaRef) error {
25-
return operationAddResponseBodySchemaRef(oedit.Operation, statusCode, description, contentType, schemaRef)
26+
func (opm *OperationMore) AddResponseBodySchemaRef(statusCode, description, contentType string, schemaRef *oas3.SchemaRef) error {
27+
return operationAddResponseBodySchemaRef(opm.Operation, statusCode, description, contentType, schemaRef)
28+
}
29+
30+
func (opm *OperationMore) HasParameter(paramNameWant string) bool {
31+
paramNameWantLc := strings.TrimSpace(paramNameWant)
32+
for _, paramRef := range opm.Operation.Parameters {
33+
if paramRef.Value == nil {
34+
continue
35+
}
36+
param := paramRef.Value
37+
param.Name = strings.TrimSpace(param.Name)
38+
paramNameTryLc := strings.ToLower(param.Name)
39+
if paramNameWantLc == paramNameTryLc {
40+
return true
41+
}
42+
}
43+
return false
2644
}
2745

2846
func operationAddRequestBodySchemaRef(op *oas3.Operation, description string, required bool, contentType string, schemaRef *oas3.SchemaRef) error {
@@ -234,12 +252,6 @@ type OperationMoreSet struct {
234252
OperationMores []OperationMore
235253
}
236254

237-
type OperationMore struct {
238-
UrlPath string
239-
Method string
240-
Operation *oas3.Operation
241-
}
242-
243255
func QueryOperationsByTags(spec *oas3.Swagger, tags []string) *OperationMoreSet {
244256
tagsWantMatch := map[string]int{}
245257
for _, tag := range tags {

openapi3edit/security.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77

88
oas3 "github.com/getkin/kin-openapi/openapi3"
9-
"github.com/grokify/simplego/type/stringsutil"
9+
"github.com/grokify/simplego/type/maputil"
1010
"github.com/grokify/swaggman/openapi3"
1111
)
1212

@@ -132,13 +132,14 @@ func SecuritySchemeApikeyAddOperations(spec *oas3.Swagger, tags []string, keyNam
132132
tagsMap[tagName] = 1
133133
}
134134
openapi3.VisitOperations(spec, func(skipPath, skipMethod string, op *oas3.Operation) {
135-
if !MapSliceIntersectionExists(tagsMap, op.Tags) {
135+
if !maputil.MapSliceIntersectionExists(tagsMap, op.Tags) {
136136
return
137137
}
138138
SecuritySchemeAddOperation(op, keyName, []string{})
139139
})
140140
}
141141

142+
/*
142143
func MapSliceIntersection(haystack map[string]int, needles []string, unique bool) []string {
143144
if unique {
144145
needles = stringsutil.SliceCondenseSpace(needles, true, false)
@@ -151,6 +152,7 @@ func MapSliceIntersection(haystack map[string]int, needles []string, unique bool
151152
}
152153
return matches
153154
}
155+
*/
154156

155157
func MapSliceIntersectionExists(haystack map[string]int, needles []string) bool {
156158
for _, needle := range needles {

openapi3edit/validate_and_fix.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func ParsePathParametersParens(urlPath string) []string {
3232
return paramNames
3333
}
3434

35+
/*
3536
func OperationHasParameter(paramNameWant string, op *oas3.Operation) bool {
3637
paramNameWantLc := strings.ToLower(strings.TrimSpace(paramNameWant))
3738
for _, paramRef := range op.Parameters {
@@ -47,7 +48,7 @@ func OperationHasParameter(paramNameWant string, op *oas3.Operation) bool {
4748
}
4849
return false
4950
}
50-
51+
*/
5152
// SortParameters sorts parameters according to an input name list.
5253
// This used to resort parameters inline with path path parameters
5354
// so they line up properly when rendered.
@@ -96,8 +97,9 @@ func ValidateFixOperationPathParameters(spec *oas3.Swagger, fix bool) ([]*openap
9697
}
9798
varNamesMissing := []string{}
9899
fixed := false
100+
opm := OperationMore{Operation: op}
99101
for _, varName := range varNamesPath {
100-
if !OperationHasParameter(varName, op) {
102+
if !opm.HasParameter(varName) {
101103
if fix {
102104
newParamRef := &oas3.ParameterRef{
103105
Value: &oas3.Parameter{

0 commit comments

Comments
 (0)