( parameters: readonly OperationParameter[], requestBody: OperationRequestBody | undefined, servers: readonly ServerInfo[], )
| 408 | }; |
| 409 | |
| 410 | export const buildInputSchema = ( |
| 411 | parameters: readonly OperationParameter[], |
| 412 | requestBody: OperationRequestBody | undefined, |
| 413 | servers: readonly ServerInfo[], |
| 414 | ): Record<string, unknown> | undefined => { |
| 415 | const properties: Record<string, unknown> = {}; |
| 416 | const required: string[] = []; |
| 417 | let requiredBodyAlternatives: readonly { readonly required: readonly string[] }[] | undefined; |
| 418 | |
| 419 | for (const param of parameters) { |
| 420 | properties[param.name] = Option.getOrElse(param.schema, () => ({ type: "string" })); |
| 421 | if (param.required) required.push(param.name); |
| 422 | } |
| 423 | |
| 424 | // A path/query parameter named `server` takes precedence over the host input. |
| 425 | const serverProperty = buildServerInputProperty(servers); |
| 426 | if (serverProperty && !("server" in properties)) properties.server = serverProperty; |
| 427 | |
| 428 | if (requestBody) { |
| 429 | // When the spec declares multiple media types for this requestBody, |
| 430 | // expose `contentType` so the model can pick. Default = first declared. |
| 431 | // For mixed bodies, `body` schema tracks the default; the model is |
| 432 | // responsible for supplying a body shape that matches whichever |
| 433 | // contentType it picks. Octet-only operations use `bodyBase64` instead. |
| 434 | const contents = Option.getOrUndefined(requestBody.contents); |
| 435 | const defaultIsOctetStream = |
| 436 | requestBody.contentType.split(";")[0]?.trim().toLowerCase() === "application/octet-stream"; |
| 437 | const acceptsOctetStream = |
| 438 | defaultIsOctetStream || |
| 439 | contents?.some( |
| 440 | (content) => |
| 441 | content.contentType.split(";")[0]?.trim().toLowerCase() === "application/octet-stream", |
| 442 | ) === true; |
| 443 | const acceptsBody = |
| 444 | !defaultIsOctetStream || |
| 445 | contents?.some( |
| 446 | (content) => |
| 447 | content.contentType.split(";")[0]?.trim().toLowerCase() !== "application/octet-stream", |
| 448 | ) === true; |
| 449 | if (acceptsBody) { |
| 450 | properties.body = Option.getOrElse(requestBody.schema, () => ({ type: "object" })); |
| 451 | } |
| 452 | if (acceptsOctetStream) { |
| 453 | properties.bodyBase64 = { |
| 454 | type: "string", |
| 455 | contentEncoding: "base64", |
| 456 | contentMediaType: "application/octet-stream", |
| 457 | description: |
| 458 | "Base64-encoded bytes for application/octet-stream request bodies. When contentType is omitted, this selects application/octet-stream.", |
| 459 | }; |
| 460 | } |
| 461 | if (requestBody.required) { |
| 462 | if (acceptsOctetStream && acceptsBody) { |
| 463 | requiredBodyAlternatives = [{ required: ["body"] }, { required: ["bodyBase64"] }]; |
| 464 | } else { |
| 465 | required.push(acceptsOctetStream ? "bodyBase64" : "body"); |
| 466 | } |
| 467 | } |
no test coverage detected