Skip to content
Open
Show file tree
Hide file tree
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
66 changes: 66 additions & 0 deletions docs/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15000,12 +15000,78 @@
],
"name": "stats",
"parameters": [
{
"role": "flag",
"name": "include-remotes",
"type": "boolean",
"required": false,
"summary": "Include remote cluster data into the response"
},
{
"role": "flag",
"name": "timeout",
"type": "string",
"required": false,
"summary": "Period to wait for each node to respond.\nIf a node does not respond before its timeout expires, the response does not include its stats.\nHowever, timed out nodes are included in the response’s `_nodes.failed` property. Defaults to no timeout."
},
{
"role": "flag",
"name": "node-id",
"type": "string",
"required": false,
"summary": "Comma-separated list of node filters used to limit returned information. Defaults to all nodes in the cluster.",
"repeatable": true,
"separator": ","
},
{
"role": "flag",
"name": "error-trace",
"type": "boolean",
"required": false,
"summary": "When set to `true` Elasticsearch will include the full stack trace of errors\nwhen they occur."
},
{
"role": "flag",
"name": "filter-path",
"type": "string",
"required": false,
"summary": "Comma-separated list of filters in dot notation which reduce the response\nreturned by Elasticsearch.",
"repeatable": true
},
{
"role": "flag",
"name": "human",
"type": "boolean",
"required": false,
"summary": "When set to `true` will return statistics in a format suitable for humans.\nFor example `\"exists_time\": \"1h\"` for humans and\n`\"exists_time_in_millis\": 3600000` for computers. When disabled the human\nreadable values will be omitted. This makes sense for responses being consumed\nonly by machines."
},
{
"role": "flag",
"name": "pretty",
"type": "boolean",
"required": false,
"summary": "If set to `true` the returned JSON will be \"pretty-formatted\". Only use\nthis option for debugging only."
},
{
"role": "flag",
"name": "input-file",
"type": "string",
"required": false,
"summary": "path to a JSON file to use as command input"
},
{
"role": "dryRun",
"name": "dry-run",
"type": "boolean",
"required": false,
"summary": "validate all inputs and exit without performing any action"
},
{
"role": "flag",
"name": "no-validate",
"type": "boolean",
"required": false,
"summary": "skip input validation and send the request as-is"
}
],
"summary": "Get cluster statistics.",
Expand Down
27 changes: 25 additions & 2 deletions src/es/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,40 @@ export async function loadEsApisInFile (namespaceFile: string): Promise<EsApiDef
if (!Array.isArray(arr)) {
throw new Error(`internal error: ${namespaceFile}.js did not export ${exportKey}`)
}
return Promise.all((arr as EsApiDefinition[]).map(resolveDefinition))
return Promise.all((arr as EsApiDefinition[]).map(async (def) => {
return resolveDefinition(await attachMissingRequestSchema(namespaceFile, def))
}))
})()
moduleCache.set(namespaceFile, cached)
return cached
}

/**
* The tool definition for `cluster.stats` ships without `input` because an
* optional path param is not on the selected URL. The request schema is still
* published next to it, and that schema has the query params (`filter_path`,
* `human`, `timeout`).
*/
async function attachMissingRequestSchema (namespaceFile: string, def: EsApiDefinition): Promise<EsApiDefinition> {
if (def.input != null) return def
try {
const input = await requireSchemaModule<Record<string, unknown>>(
`@elastic/schemas/es/json/${namespaceFile}.request.json`,
)
if (input == null || typeof input !== 'object' || Array.isArray(input) || input['properties'] == null) return def
return { ...def, input }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This attaches cluster.stats.request.json as-is, which marks node_id as x-found-in: "body" even though it is really a path param in /_cluster/stats/nodes/{node_id}. The resulting --node-id flag builds a broken request.

So the node filter is silently dropped from the URL and sent as a GET body instead, while --dry-run reports the inputs as valid.

} catch (err) {
const code = (err as NodeJS.ErrnoException).code
if (code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND') return def
throw err
}
}
Comment on lines +69 to +88

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause is upstream: the schemas-js generator emits cluster_stats_definitions without input, and the request schema it would use has the wrong x-found-in for node_id.

Instead of patching this here, we should fix the code generator or ES spec so all schemas-js consumers get the fix.


/** Locates a single `EsApiDefinition` by its manifest entry. */
export async function loadEsApi (meta: EsApiMeta): Promise<EsApiDefinition> {
const defs = await loadEsApisInFile(meta.namespaceFile)
const found = defs.find(
(d) => d.name === meta.name && (d.namespace ?? null) === meta.namespace
(d) => d.name === meta.name && (d.namespace ?? null) === (meta.namespace ?? null)
)
if (found == null) {
const label = meta.namespace != null ? `${meta.namespace} ${meta.name}` : meta.name
Expand Down
11 changes: 11 additions & 0 deletions test/es/apis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { apiManifest, loadEsApi, loadEsApisInFile } from '../../src/es/apis.ts'
import { extractSchemaArgs } from '../../src/lib/json-schema-args.ts'

describe('loadEsApisInFile', () => {
it('returns the cached promise on a repeated call for the same namespace file', async () => {
Expand All @@ -18,6 +19,16 @@ describe('loadEsApisInFile', () => {
assert.equal(first, second)
assert.ok(first.length > 0)
})

it('uses the request schema when the tool definition has no input', async () => {
const defs = await loadEsApisInFile('cluster.stats')
const input = defs.find((d) => d.name === 'stats')?.input
assert.ok(input != null)
const flags = new Set(extractSchemaArgs(input).map((a) => a.cliFlag))
for (const flag of ['filter-path', 'human', 'timeout']) {
assert.ok(flags.has(flag), flag)
}
})
})

describe('loadEsApi', () => {
Expand Down
Loading