Repository navigation
fix(cli): improve schema discovery #1658
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
Open
betegon
wants to merge
2
commits into
main
Choose a base branch
from
fix/cli-schema-discovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,469
−395
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| Search the bundled API schema using product concepts or CLI command paths. | ||
| Discovery includes summaries, paths, tags, and query parameter values: `logs` | ||
| can find Explore's `dataset=logs` as well as feature-flag audit logs. Results | ||
| explain their matches and show related CLI commands when a relationship is | ||
| declared. A related API does not necessarily reproduce the command's filters, | ||
| pagination, or transformations. | ||
|
|
||
| Use an exact operation ID, SDK function name, or HTTP method and path for a | ||
| precise lookup. Concept searches return all matching candidates; deprecated | ||
| operations are marked and sorted after active ones. No endpoint is chosen just | ||
| because it appears first in the catalog. | ||
|
|
||
| With `--json`, discovery returns an array of endpoints with additive `match` | ||
| metadata. Exact single-endpoint lookups remain endpoint objects, `--search` | ||
| always returns an array, and `--all` returns the flat catalog without match | ||
| metadata. The schema is bundled from the CLI's SDK version and works offline. |
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
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
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,139 @@ | ||
| /** Extract the bundled discovery index from a dereferenced OpenAPI document. */ | ||
|
|
||
| import type { | ||
| ApiEndpoint, | ||
| ApiQueryParameter, | ||
| } from "../../src/lib/api-schema.js"; | ||
|
|
||
| const HTTP_METHODS = ["get", "post", "put", "delete", "patch"] as const; | ||
| const PATH_PARAM_PATTERN = /\{(\w+)\}/g; | ||
|
|
||
| /** Only the OpenAPI fields used by the discovery index. */ | ||
| export type OpenApiSpec = { | ||
| paths: Record<string, OpenApiPathItem>; | ||
| }; | ||
|
|
||
| type OpenApiPathItem = Partial< | ||
| Record<(typeof HTTP_METHODS)[number], OpenApiOperation> | ||
| > & { | ||
| parameters?: OpenApiParameter[]; | ||
| }; | ||
|
|
||
| type OpenApiOperation = { | ||
| operationId?: string; | ||
| summary?: string; | ||
| description?: string; | ||
| tags?: string[]; | ||
| deprecated?: boolean; | ||
| parameters?: OpenApiParameter[]; | ||
| }; | ||
|
|
||
| type OpenApiParameter = { | ||
| in: "path" | "query" | "header" | "cookie"; | ||
| name: string; | ||
| required?: boolean; | ||
| description?: string; | ||
| schema?: { | ||
| type?: string; | ||
| enum?: unknown[]; | ||
| items?: { enum?: unknown[] }; | ||
| }; | ||
| }; | ||
|
|
||
| /** Keep the existing resource grouping: the last literal URL segment. */ | ||
| function deriveResource(url: string): string { | ||
| const segments = url | ||
| .split("/") | ||
| .filter((s) => s.length > 0 && !s.startsWith("{")); | ||
| const meaningful = segments.filter((s) => s !== "api" && s !== "0"); | ||
| return meaningful.at(-1) ?? "unknown"; | ||
| } | ||
|
|
||
| function isEnumValue( | ||
| value: unknown, | ||
| ): value is string | number | boolean | null { | ||
| return ( | ||
| value === null || | ||
| typeof value === "string" || | ||
| typeof value === "number" || | ||
| typeof value === "boolean" | ||
| ); | ||
| } | ||
|
|
||
| /** Operation parameters override inherited parameters with the same name/in. */ | ||
| function extractQueryParameters( | ||
| pathItem: OpenApiPathItem, | ||
| operation: OpenApiOperation, | ||
| ): ApiQueryParameter[] { | ||
| const parameters = new Map<string, OpenApiParameter>(); | ||
| for (const parameter of [ | ||
| ...(pathItem.parameters ?? []), | ||
| ...(operation.parameters ?? []), | ||
| ]) { | ||
| if (parameter.in === "query") { | ||
| parameters.set(parameter.name, parameter); | ||
| } | ||
| } | ||
|
|
||
| return [...parameters.values()].map((parameter) => { | ||
| const schema = parameter.schema; | ||
| const values = schema?.type === "array" ? schema.items?.enum : schema?.enum; | ||
| return { | ||
| name: parameter.name, | ||
| description: parameter.description?.trim(), | ||
| type: schema?.type, | ||
| required: parameter.required === true, | ||
| enum: values?.filter(isEnumValue), | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Generate entries directly from API metadata so new operations and enum values | ||
| * become discoverable on the next schema generation, without a vocabulary map. | ||
| */ | ||
| export function extractApiEndpoints( | ||
| spec: OpenApiSpec, | ||
| sdkMap: ReadonlyMap<string, string>, | ||
| ): ApiEndpoint[] { | ||
| const endpoints: ApiEndpoint[] = []; | ||
|
|
||
| for (const [urlPath, pathItem] of Object.entries(spec.paths)) { | ||
| for (const method of HTTP_METHODS) { | ||
| const operation = pathItem[method]; | ||
| if (!operation) { | ||
| continue; | ||
| } | ||
|
|
||
| const methodUpper = method.toUpperCase(); | ||
| const fn = sdkMap.get(`${methodUpper}:${urlPath}`) ?? ""; | ||
| const queryParameters = extractQueryParameters(pathItem, operation); | ||
|
|
||
| endpoints.push({ | ||
| fn, | ||
| method: methodUpper, | ||
| path: urlPath, | ||
| summary: operation.summary?.trim(), | ||
| description: (operation.description ?? "").trim(), | ||
| tags: operation.tags, | ||
| pathParams: [...urlPath.matchAll(PATH_PARAM_PATTERN)].map((match) => | ||
| match[0].slice(1, -1), | ||
| ), | ||
| queryParams: queryParameters.map((parameter) => parameter.name), | ||
| queryParameters, | ||
| deprecated: | ||
| operation.deprecated === true || | ||
| fn.startsWith("deprecated") || | ||
| (operation.operationId ?? "").toLowerCase().includes("deprecated"), | ||
| resource: deriveResource(urlPath), | ||
| operationId: operation.operationId ?? "", | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Keep stable ordering and the existing resource/operation ID contract. | ||
| return endpoints.sort((a, b) => { | ||
| const resourceCmp = a.resource.localeCompare(b.resource); | ||
| return resourceCmp || a.operationId.localeCompare(b.operationId); | ||
| }); | ||
| } |
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
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.
I think all this should be part of the
@sentry/apimodule.