Skip to content

fix: openapi tools received random operationNums #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions pkg/loader/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"slices"
"sort"
"strings"

"github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -56,7 +57,18 @@ func getOpenAPITools(t *openapi3.T, defaultHost string) ([]types.Tool, error) {
tools []types.Tool
operationNum = 1 // Each tool gets an operation number, beginning with 1
)
for pathString, pathObj := range t.Paths.Map() {

pathMap := t.Paths.Map()

keys := make([]string, 0, len(pathMap))
for k := range pathMap {
keys = append(keys, k)
}

sort.Strings(keys)

for _, pathString := range keys {
pathObj := pathMap[pathString]
// Handle path-level server override, if one exists
pathServer := defaultServer
if pathObj.Servers != nil && len(pathObj.Servers) > 0 {
Expand All @@ -66,8 +78,16 @@ func getOpenAPITools(t *openapi3.T, defaultHost string) ([]types.Tool, error) {
}
}

// Generate a tool for each operation in this path.
operations := pathObj.Operations()
methods := make([]string, 0, len(operations))
for method := range operations {
methods = append(methods, method)
}
sort.Strings(methods)
operations:
for method, operation := range pathObj.Operations() {
for _, method := range methods {
operation := operations[method]
// Handle operation-level server override, if one exists
operationServer := pathServer
if operation.Servers != nil && len(*operation.Servers) > 0 {
Expand Down