Skip to content

Cloud POST commands without an explicit body schema silently drop stdin/--input-file body #86

Description

@Anaethelion

Bug

For Cloud API commands that have no explicit body schema in their definition (e.g. create-elasticsearch-project, patch-elasticsearch-project), any body data supplied via stdin or --input-file is silently discarded and the POST/PATCH request is sent with no body, causing a 400 from the API.

Steps to reproduce

elastic cloud elasticsearch-projects create-elasticsearch-project \
  <<< '{"name":"demo","region_id":"aws-us-east-1"}'

Expected: project is created
Actual:

{
  "error": {
    "code": "cloud_api_error",
    "message": "Cloud API error 400: {\"errors\":[{\"code\":\"oapi.request_validation.bad_request\",\"message\":\"request body has an error: value is required but missing\"}]}\n"
  }
}

Root cause

src/cloud/request-builder.ts collectBody() bails out early when def.body is not a ZodObject:

function collectBody(def, input) {
  if (!(def.body instanceof z.ZodObject)) return undefined  // ← body dropped here
  ...
}

Meanwhile buildCommandSchema() in register.ts registers a z.looseObject({}) as the input schema when no params are defined, so stdin IS read and validated — but the validated data is never forwarded to the request body.

Suggested fix

When def.body is undefined and the method is POST, PATCH, or PUT, forward all input keys that are not path or query params as the raw request body:

function collectBody(def, input) {
  if (!(def.body instanceof z.ZodObject)) {
    // passthrough: for POST/PATCH/PUT commands with no explicit schema,
    // forward everything that isn't a path or query param as the body
    const reserved = new Set([
      ...(def.pathParams ?? []).map(p => p.name),
      ...(def.queryParams ?? []).map(q => q.cliFlag ?? q.name),
    ])
    const body = Object.fromEntries(
      Object.entries(input).filter(([k]) => !reserved.has(k))
    )
    return Object.keys(body).length > 0 ? body : undefined
  }
  ...
}

Metadata

Metadata

Assignees

Labels

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions