-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add oq pipeline query language for OpenAPI schema graphs #177
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
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
340c081
feat: add oq pipeline query language for OpenAPI schema graphs
vishalg0wda b5dc93a
style: fix gofmt formatting
vishalg0wda ded07af
build: add replace directive for cmd/openapi to resolve local packages
vishalg0wda d88cea1
fix: resolve remaining testifylint errors in test files
vishalg0wda dbdaafd
fix: resolve all golangci-lint errors
vishalg0wda c02147e
fix: guard map lookup to satisfy nil-panic linter
vishalg0wda 26edf4a
fix: address PR review feedback
vishalg0wda 200bdd9
feat: add new oq pipeline stages and operation fields
vishalg0wda df5461d
fix: use assert.Len for testifylint compliance
vishalg0wda 9f3ba40
fix: address PR review feedback and improve test coverage
vishalg0wda 8af8105
feat: add TOON output format for oq
vishalg0wda f4323f9
feat: add query-reference subcommand, oq README, and fix expr parser …
vishalg0wda a91d688
feat: add edge annotations, graph analysis stages, and new schema fields
vishalg0wda 48b8cf3
refactor: swap query command arg order to query-first
vishalg0wda 41975c1
fix: remove redundant isNull field and treat empty strings as falsy i…
vishalg0wda b71bcd7
refactor: split oq/oq.go into parse, exec, format, field modules
vishalg0wda 395c19c
fix: re-trigger CI for mod-check
vishalg0wda de1339a
fix: remove replace directive, increase test coverage, and document m…
vishalg0wda cfaf308
fix: detectCycle marking non-cycle ancestors as circular, FormatToon …
vishalg0wda c5c2495
fix: update cmd/openapi dependency to latest commit
vishalg0wda e4afdd2
test: add descriptive assertion messages to oq tests
vishalg0wda 0becad8
fix: update cmd/openapi dependency to latest commit
vishalg0wda e705517
fix: avoid nilaway false positive in execSample by replacing slices.C…
vishalg0wda 06a0016
fix: update cmd/openapi dependency to latest commit
vishalg0wda b927a3f
feat: add jq-inspired syntax to oq query language
vishalg0wda 4bf74e8
fix: lint issues, update docs for jq-style oq syntax
vishalg0wda 19d9a40
Merge branch 'main' into feat/oq-query-language
vishalg0wda 833a44d
fix: update cmd/openapi dependency to latest commit
vishalg0wda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| package openapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/speakeasy-api/openapi/graph" | ||
| "github.com/speakeasy-api/openapi/openapi" | ||
| "github.com/speakeasy-api/openapi/oq" | ||
| "github.com/speakeasy-api/openapi/references" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var queryCmd = &cobra.Command{ | ||
| Use: "query <query> [input-file]", | ||
| Short: "Query an OpenAPI specification using the oq pipeline language", | ||
| Long: `Query an OpenAPI specification using the oq pipeline language to answer | ||
| structural and semantic questions about schemas and operations. | ||
|
|
||
| The query argument comes first, followed by an optional input file. If no file | ||
| is given, reads from stdin. | ||
|
|
||
| Examples: | ||
| # Deeply nested components (jq-style syntax) | ||
| openapi spec query 'schemas.components | sort_by(depth; desc) | first(10) | pick name, depth' petstore.yaml | ||
|
|
||
| # Pipe from stdin | ||
| cat spec.yaml | openapi spec query 'schemas | count' | ||
|
|
||
| # Explicit stdin | ||
| openapi spec query 'schemas | count' - | ||
|
|
||
| # Filter with select() | ||
| openapi spec query 'schemas | select(union_width > 0) | sort_by(union_width; desc) | first(10)' petstore.yaml | ||
|
|
||
| # Dead components (no incoming references) | ||
| openapi spec query 'schemas.components | select(in_degree == 0) | pick name' petstore.yaml | ||
|
|
||
| # Variable binding — exclude seed from reachable results | ||
| openapi spec query 'schemas | select(name == "Pet") | let $pet = name | reachable | select(name != $pet)' petstore.yaml | ||
|
|
||
| # User-defined functions | ||
| openapi spec query 'def hot: select(in_degree > 5); schemas.components | hot | pick name' petstore.yaml | ||
|
|
||
| # Alternative operator — fallback for null/falsy values | ||
| openapi spec query 'schemas | select(name // "none" != "none")' petstore.yaml | ||
|
|
||
| # If-then-else conditional | ||
| openapi spec query 'schemas | select(if is_component then depth > 3 else true end)' petstore.yaml | ||
|
|
||
| # Blast radius | ||
| openapi spec query 'schemas.components | select(name == "Error") | blast-radius | length' petstore.yaml | ||
|
|
||
| # Explain a query plan | ||
| openapi spec query 'schemas.components | select(depth > 5) | sort_by(depth; desc) | explain' petstore.yaml | ||
|
|
||
| Pipeline stages (jq-style): | ||
| Source: schemas, schemas.components, schemas.inline, operations | ||
| Traversal: refs-out, refs-in, reachable, ancestors, properties, union-members, items, | ||
| ops, schemas, path(A; B), connected, blast-radius, neighbors(N) | ||
| Analysis: orphans, leaves, cycles, clusters, tag-boundary, shared-refs | ||
| Filter: select(expr), pick <fields>, sort_by(field; desc), first(N), last(N), | ||
| sample(N), top(N; field), bottom(N; field), unique, group_by(field), length | ||
| Variables: let $var = expr | ||
| Functions: def name: body; def name($p): body; include "file.oq"; | ||
| Meta: explain, fields, format(table|json|markdown|toon) | ||
|
|
||
| Legacy syntax (where, sort, take, head, select fields, group-by, count) is still supported. | ||
|
|
||
| Expression operators: ==, !=, >, <, >=, <=, and, or, not, //, has(), matches, | ||
| if-then-else-end, string interpolation \(expr)`, | ||
| Args: queryArgs(), | ||
| Run: runQuery, | ||
| } | ||
|
|
||
| var queryOutputFormat string | ||
| var queryFromFile string | ||
|
|
||
| func init() { | ||
| queryCmd.Flags().StringVar(&queryOutputFormat, "format", "table", "output format: table, json, markdown, or toon") | ||
| queryCmd.Flags().StringVarP(&queryFromFile, "file", "f", "", "read query from file instead of argument") | ||
| } | ||
|
|
||
| func runQuery(cmd *cobra.Command, args []string) { | ||
| ctx := cmd.Context() | ||
|
|
||
| // args[0] = query (or input file if using -f), args[1] = input file (optional) | ||
| queryStr := "" | ||
| inputFile := "-" // default to stdin | ||
|
|
||
| if queryFromFile != "" { | ||
| data, err := os.ReadFile(queryFromFile) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error reading query file: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| queryStr = string(data) | ||
| // When using -f, all positional args are input files | ||
| if len(args) > 0 { | ||
| inputFile = args[0] | ||
| } | ||
| } else if len(args) >= 1 { | ||
| queryStr = args[0] | ||
| if len(args) >= 2 { | ||
| inputFile = args[1] | ||
| } | ||
| } | ||
|
|
||
| if queryStr == "" { | ||
| fmt.Fprintf(os.Stderr, "Error: no query provided\n") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| processor, err := NewOpenAPIProcessor(inputFile, "", false) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| if err := queryOpenAPI(ctx, processor, queryStr); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func queryOpenAPI(ctx context.Context, processor *OpenAPIProcessor, queryStr string) error { | ||
| doc, _, err := processor.LoadDocument(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if doc == nil { | ||
| return errors.New("failed to parse OpenAPI document: document is nil") | ||
| } | ||
|
|
||
| // Build index | ||
| idx := buildIndex(ctx, doc) | ||
|
|
||
| // Build graph | ||
| g := graph.Build(ctx, idx) | ||
|
|
||
| // Execute query | ||
| result, err := oq.Execute(queryStr, g) | ||
| if err != nil { | ||
| return fmt.Errorf("query error: %w", err) | ||
| } | ||
|
|
||
| // Format and output — inline format stage overrides CLI flag | ||
| format := queryOutputFormat | ||
| if result.FormatHint != "" { | ||
| format = result.FormatHint | ||
| } | ||
|
|
||
| var output string | ||
| switch format { | ||
| case "json": | ||
| output = oq.FormatJSON(result, g) | ||
| case "markdown": | ||
| output = oq.FormatMarkdown(result, g) | ||
| case "toon": | ||
| output = oq.FormatToon(result, g) | ||
| default: | ||
| output = oq.FormatTable(result, g) | ||
| } | ||
|
|
||
| fmt.Fprint(processor.stdout(), output) | ||
| if result.IsCount { | ||
| fmt.Fprintln(processor.stdout()) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func buildIndex(ctx context.Context, doc *openapi.OpenAPI) *openapi.Index { | ||
| resolveOpts := references.ResolveOptions{ | ||
| RootDocument: doc, | ||
| TargetDocument: doc, | ||
| TargetLocation: ".", | ||
| } | ||
| return openapi.BuildIndex(ctx, doc, resolveOpts) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🚩 TargetLocation set to '.' instead of actual file path
In
cmd/openapi/commands/openapi/query.go:140,TargetLocationis set to"."rather than the actual input file path. Every other call toBuildIndexin the codebase uses a meaningful file path (e.g.,"test.yaml","testdata/petstore.yaml"). TheTargetLocationis used byisFromMainDocument()to compare against the current document stack. For single-file specs this works, but for multi-file specs with external$refs, using"."could causeisFromMainDocument()to behave incorrectly (e.g., classifying external schemas as main-document schemas). The graph test atgraph/graph_test.go:29correctly uses the actual path.Was this helpful? React with 👍 or 👎 to provide feedback.