-
Notifications
You must be signed in to change notification settings - Fork 300
fix: various fixes for openapi support #208
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package loader | |
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"slices" | ||
"strings" | ||
|
||
|
@@ -15,9 +16,19 @@ import ( | |
// Each operation will become a tool definition. | ||
// The tool's Instructions will be in the format "#!sys.openapi '{JSON Instructions}'", | ||
// where the JSON Instructions are a JSON-serialized engine.OpenAPIInstructions struct. | ||
func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) { | ||
func getOpenAPITools(t *openapi3.T, defaultHost string) ([]types.Tool, error) { | ||
// Determine the default server. | ||
if len(t.Servers) == 0 { | ||
return nil, fmt.Errorf("no servers found in OpenAPI spec") | ||
if defaultHost != "" { | ||
u, err := url.Parse(defaultHost) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid default host URL: %w", err) | ||
} | ||
u.Path = "/" | ||
t.Servers = []*openapi3.Server{{URL: u.String()}} | ||
} else { | ||
return nil, fmt.Errorf("no servers found in OpenAPI spec") | ||
} | ||
Comment on lines
+19
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds support for a "default host" if no server is specified at the global level in the OpenAPI document. |
||
} | ||
defaultServer, err := parseServer(t.Servers[0]) | ||
if err != nil { | ||
|
@@ -39,6 +50,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) { | |
} | ||
} | ||
|
||
// Generate a tool for each operation. | ||
var ( | ||
toolNames []string | ||
tools []types.Tool | ||
|
@@ -54,6 +66,7 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) { | |
} | ||
} | ||
|
||
operations: | ||
for method, operation := range pathObj.Operations() { | ||
// Handle operation-level server override, if one exists | ||
operationServer := pathServer | ||
|
@@ -103,10 +116,9 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) { | |
}, | ||
} | ||
|
||
toolNames = append(toolNames, tool.Parameters.Name) | ||
|
||
// Handle query, path, and header parameters | ||
for _, param := range operation.Parameters { | ||
// Handle query, path, and header parameters, based on the parameters for this operation | ||
// and the parameters for this path. | ||
for _, param := range append(operation.Parameters, pathObj.Parameters...) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameters can also be defined at the path level ( |
||
arg := param.Value.Schema.Value | ||
|
||
if arg.Description == "" { | ||
|
@@ -161,7 +173,8 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) { | |
} | ||
|
||
if bodyMIME == "" { | ||
return nil, fmt.Errorf("no supported MIME types found for request body in operation %s", operation.OperationID) | ||
// No supported MIME types found, so just skip this operation and move on. | ||
continue operations | ||
Comment on lines
175
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now skip tools with no supported MIME types in the body instead of just returning an error for the whole document. |
||
} | ||
} | ||
|
||
|
@@ -226,6 +239,8 @@ func getOpenAPITools(t *openapi3.T) ([]types.Tool, error) { | |
return nil, err | ||
} | ||
|
||
// Register | ||
toolNames = append(toolNames, tool.Parameters.Name) | ||
tools = append(tools, tool) | ||
operationNum++ | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to check for the existence of the
paths
map, rather than the version string, since the version string is sometimes missing.