Skip to content

Commit

Permalink
feat: openapi3: add SpecMore.OperationIDsCounts(), `SpecMore.Oper…
Browse files Browse the repository at this point in the history
…ationIDsLocations()`, `SpecMore.OperationsDescriptionInfo()`
  • Loading branch information
grokify committed Oct 20, 2022
1 parent f6c94b9 commit fb8d22d
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions openapi3/spec_more.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"regexp"
"strconv"
"strings"

oas3 "github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -209,7 +210,11 @@ func (sm *SpecMore) OperationsCount() int {
if sm.Spec == nil {
return -1
}
return len(sm.OperationMetas())
count := 0
VisitOperations(sm.Spec, func(skipPath, skipMethod string, op *oas3.Operation) {
count++
})
return count
}

// OperationCountsByTag returns a histogram for operations by tag.
Expand All @@ -231,6 +236,30 @@ func (sm *SpecMore) OperationIDs() []string {
return stringsutil.SliceCondenseSpace(ids, false, true)
}

func (sm *SpecMore) OperationIDsCounts() map[string]int {
msi := map[string]int{}
VisitOperations(sm.Spec, func(skipPath, skipMethod string, op *oas3.Operation) {
msi[op.OperationID]++
})
return msi
}

// OperationIDsLocations returns a `map[string][]string` where the keys are
// operationIDs and the values are pathMethods for use in analyzing if there are
// duplicate operationIDs.
func (sm *SpecMore) OperationIDsLocations() map[string][]string {
vals := map[string][]string{}
VisitOperations(sm.Spec, func(opPath, opMethod string, op *oas3.Operation) {
if op == nil {
return
}
pathMethod := PathMethod(opPath, opMethod)
op.OperationID = strings.TrimSpace(op.OperationID)
vals[op.OperationID] = append(vals[op.OperationID], pathMethod)
})
return vals
}

func (sm *SpecMore) OperationByID(wantOperationID string) (path, method string, op *oas3.Operation, err error) {
wantOperationID = strings.TrimSpace(wantOperationID)
VisitOperations(sm.Spec, func(thisPath, thisMethod string, thisOp *oas3.Operation) {
Expand Down Expand Up @@ -402,8 +431,7 @@ func (sm *SpecMore) ServerURL(index uint) string {
return strings.TrimSpace(server.URL)
}

// ServerURLBasePath extracts the base path from a OAS URL
// which can include variables.
// ServerURLBasePath extracts the base path from a OAS URL which can include variables.
func (sm *SpecMore) ServerURLBasePath(index uint) (string, error) {
serverURL := sm.ServerURL(index)
if len(serverURL) == 0 {
Expand All @@ -416,6 +444,31 @@ func (sm *SpecMore) ServerURLBasePath(index uint) (string, error) {
return serverURLParsed.Path, nil
}

// OperationsDescriptionInfo returns information on operations with and without descriptions.
func (sm *SpecMore) OperationsDescriptionInfo() map[string][]string {
data := map[string][]string{
"opWithDesc": []string{},
"opWoutDesc": []string{},
"opWithDescCount": []string{},
"opWoutDescCount": []string{},
}
VisitOperations(sm.Spec, func(opPath, opMethod string, op *oas3.Operation) {
if op == nil {
return
}
pathMethod := PathMethod(opPath, opMethod)
op.Description = strings.TrimSpace(op.Description)
if len(op.Description) == 0 {
data["opWoutDesc"] = append(data["opWoutDesc"], pathMethod)
} else {
data["opWithDesc"] = append(data["opWithDesc"], pathMethod)
}
})
data["opWithDescCount"] = append(data["opWithDescCount"], strconv.Itoa(len(data["opWithDesc"])))
data["opWoutDescCount"] = append(data["opWoutDescCount"], strconv.Itoa(len(data["opWoutDesc"])))
return data
}

func (sm *SpecMore) SpecTagStats() SpecTagStats {
stats := SpecTagStats{
TagStats: SpecTagCounts{},
Expand Down

0 comments on commit fb8d22d

Please sign in to comment.