Skip to content

Commit ba352fc

Browse files
committed
feat: openapi3: add operation functions
1 parent 7458bc1 commit ba352fc

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

openapi3/modify/deprecated.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package modify
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
oas3 "github.com/getkin/kin-openapi/openapi3"
8+
"github.com/grokify/simplego/fmt/fmtutil"
9+
"github.com/grokify/swaggman/openapi3"
10+
)
11+
12+
/*
13+
func CopyOperation(src oas3.Operation) oas3.Operation {
14+
dst := oas3.Operation{
15+
ExtensionProps: src.ExtensionProps,
16+
Tags: src.Tags,
17+
Summary: src.Summary,
18+
Description: src.Description,
19+
OperationID: src.OperationID,
20+
Parameters: src.Parameters,
21+
RequestBody: src.RequestBody,
22+
Responses: src.Responses,
23+
Callbacks: src.Callbacks,
24+
Deprecated: src.Deprecated,
25+
Security: src.Security,
26+
Servers: src.Servers,
27+
ExternalDocs: src.ExternalDocs,
28+
}
29+
return dst
30+
}
31+
*/
32+
33+
func SpecSchemasSetDeprecated(spec *oas3.Swagger, newDeprecated bool) {
34+
for _, schemaRef := range spec.Components.Schemas {
35+
if len(schemaRef.Ref) == 0 && schemaRef.Value != nil {
36+
schemaRef.Value.Deprecated = newDeprecated
37+
}
38+
}
39+
}
40+
41+
func SpecOperationsSetDeprecated(spec *oas3.Swagger, newDeprecated bool) {
42+
openapi3.VisitOperations(
43+
spec,
44+
func(path, method string, op *oas3.Operation) {
45+
/*if op == nil {
46+
return
47+
}*/
48+
49+
// op = &*op
50+
51+
op.Summary = "SUMSUMSUMARY"
52+
op.Deprecated = true
53+
op.Tags = append(op.Tags, "NEW_TAG")
54+
55+
newOp := oas3.Operation{
56+
ExtensionProps: op.ExtensionProps,
57+
Tags: op.Tags,
58+
Summary: op.Summary,
59+
Description: op.Description,
60+
OperationID: op.OperationID,
61+
Parameters: op.Parameters,
62+
RequestBody: op.RequestBody,
63+
Responses: op.Responses,
64+
Callbacks: op.Callbacks,
65+
Deprecated: op.Deprecated,
66+
Security: op.Security,
67+
Servers: op.Servers,
68+
ExternalDocs: op.ExternalDocs,
69+
}
70+
71+
//newOp := oas3.Operation{}
72+
// reflect.Copy([]oas3.Operation{*op}, []oas3.Operation{newOp})
73+
SpecSetOperation(spec, path, method, newOp)
74+
if 1 == 0 {
75+
ps := reflect.ValueOf(op)
76+
// struct
77+
s := ps.Elem()
78+
if s.Kind() == reflect.Struct {
79+
// exported field
80+
f := s.FieldByName("Deprecated")
81+
if f.IsValid() {
82+
// A Value can be changed only if it is
83+
// addressable and was not obtained by
84+
// the use of unexported struct fields.
85+
if f.CanSet() {
86+
// change value of N
87+
if f.Kind() == reflect.Bool {
88+
f.SetBool(false)
89+
fmt.Println("SETTING_REEFLECT")
90+
//x := int64(7)
91+
/*if !f.OverflowInt(x) {
92+
f.Set(x)
93+
}*/
94+
}
95+
}
96+
}
97+
}
98+
}
99+
100+
if 1 == 0 && op.OperationID == "listGlipGroups" {
101+
op.Deprecated = false
102+
fmt.Printf("DEP [%v]\n", op.Deprecated)
103+
104+
fmtutil.PrintJSON(op)
105+
panic("ABC_VISIT")
106+
fmt.Printf("DEP [%v]\n", op.Deprecated)
107+
if 1 == 0 {
108+
panic("YYY")
109+
fmt.Printf("DEP [%v]\n", op.Deprecated)
110+
op.Deprecated = false
111+
fmt.Printf("DEP [%v]\n", op.Deprecated)
112+
fmtutil.PrintJSON(op)
113+
fmt.Printf("SSUM [%s]\n", op.Summary)
114+
op.Summary = op.Summary + " " + "ABC "
115+
fmt.Printf("SSUM [%s]\n", op.Summary)
116+
}
117+
//panic("ZZZ_LISTGLIPGROUPS")
118+
}
119+
120+
},
121+
)
122+
}
123+
124+
/*
125+
126+
func SpecOperationsRemoveDeprecated(spec *oas3.Swagger) {
127+
openapi3.VisitOperations(
128+
spec,
129+
func(path, method string, op *oas3.Operation) {
130+
if op == nil {
131+
return
132+
}
133+
op.Deprecated = false
134+
// SpecSetOperation(spec, path, method, &*op)
135+
// op = &*op
136+
if 1 == 1 && op.OperationID == "listGlipGroups" {
137+
op.Deprecated = false
138+
fmt.Printf("DEP [%v]\n", op.Deprecated)
139+
140+
fmtutil.PrintJSON(*op)
141+
fmt.Printf("DEP [%v]\n", op.Deprecated)
142+
if 1 == 0 {
143+
panic("YYY")
144+
fmt.Printf("DEP [%v]\n", op.Deprecated)
145+
op.Deprecated = true
146+
fmt.Printf("DEP [%v]\n", op.Deprecated)
147+
fmtutil.PrintJSON(op)
148+
fmt.Printf("SSUM [%s]\n", op.Summary)
149+
op.Summary = op.Summary + " " + "ABC "
150+
fmt.Printf("SSUM [%s]\n", op.Summary)
151+
}
152+
panic("ZZZ_LISTGLIPGROUPS")
153+
}
154+
},
155+
)
156+
}
157+
*/

openapi3/modify/operations.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package modify
22

33
import (
4+
"net/http"
45
"strings"
56

67
oas3 "github.com/getkin/kin-openapi/openapi3"
@@ -15,6 +16,25 @@ func SpecOperationsCount(spec *oas3.Swagger) uint {
1516
return count
1617
}
1718

19+
func SpecSetOperation(spec *oas3.Swagger, path, method string, op oas3.Operation) {
20+
pathItem, ok := spec.Paths[path]
21+
if !ok {
22+
pathItem = &oas3.PathItem{}
23+
}
24+
method = strings.ToUpper(strings.TrimSpace(method))
25+
switch method {
26+
case http.MethodGet:
27+
pathItem.Get = &op
28+
case http.MethodPost:
29+
pathItem.Post = &op
30+
case http.MethodPut:
31+
pathItem.Put = &op
32+
case http.MethodPatch:
33+
pathItem.Patch = &op
34+
}
35+
36+
}
37+
1838
func SpecOperationIds(spec *oas3.Swagger) map[string]int {
1939
msi := map[string]int{}
2040
openapi3.VisitOperations(spec, func(skipPath, skipMethod string, op *oas3.Operation) {

openapi3/spec_more.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package openapi3
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"net/http"
67
"os"
@@ -182,6 +183,36 @@ func (sm *SpecMore) OperationsCount() int {
182183
return len(sm.OperationMetas())
183184
}
184185

186+
func (sm *SpecMore) OperationsIDs() []string {
187+
ids := []string{}
188+
VisitOperations(sm.Spec, func(thisPath, thisMethod string, thisOp *oas3.Operation) {
189+
if thisOp == nil {
190+
return
191+
}
192+
ids = append(ids, thisOp.OperationID)
193+
})
194+
ids = stringsutil.SliceCondenseSpace(ids, false, true)
195+
return ids
196+
}
197+
198+
func (sm *SpecMore) OperationByID(wantOperationID string) (path, method string, op *oas3.Operation, err error) {
199+
wantOperationID = strings.TrimSpace(wantOperationID)
200+
VisitOperations(sm.Spec, func(thisPath, thisMethod string, thisOp *oas3.Operation) {
201+
if thisOp == nil {
202+
return
203+
}
204+
if wantOperationID == strings.TrimSpace(thisOp.OperationID) {
205+
path = thisPath
206+
method = thisMethod
207+
op = thisOp
208+
}
209+
})
210+
if len(strings.TrimSpace(method)) == 0 {
211+
err = fmt.Errorf("operation_not_found [%s]", wantOperationID)
212+
}
213+
return path, method, op, err
214+
}
215+
185216
func (sm *SpecMore) SchemaNames() []string {
186217
schemaNames := []string{}
187218
for schemaName := range sm.Spec.Components.Schemas {

0 commit comments

Comments
 (0)