Skip to content

enhance: generate name for OpenAPI tools when operation ID is blank #601

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 3 commits into from
Jul 2, 2024
Merged
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion pkg/loader/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"regexp"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -115,6 +116,13 @@ func getOpenAPITools(t *openapi3.T, defaultHost string) ([]types.Tool, error) {
toolDesc = toolDesc[:1024]
}

toolName := operation.OperationID
if toolName == "" {
// When there is no operation ID, we use the method + path as the tool name and remove all characters
// except letters, numbers, underscores, and hyphens.
toolName = regexp.MustCompile(`[^a-zA-Z0-9_-]+`).ReplaceAllString(strings.ToLower(method)+strings.ReplaceAll(pathString, "/", "_"), "")
}

var (
// auths are represented as a list of maps, where each map contains the names of the required security schemes.
// Items within the same map are a logical AND. The maps themselves are a logical OR. For example:
Expand All @@ -133,7 +141,7 @@ func getOpenAPITools(t *openapi3.T, defaultHost string) ([]types.Tool, error) {
tool := types.Tool{
ToolDef: types.ToolDef{
Parameters: types.Parameters{
Name: operation.OperationID,
Name: toolName,
Description: toolDesc,
Arguments: &openapi3.Schema{
Type: &openapi3.Types{"object"},
Expand Down