Skip to content
Merged
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
47 changes: 47 additions & 0 deletions src/core/capabilities/compoundParameters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it } from 'vitest';
import { compileOpenApi } from './openapi';

function spec(parameter: Record<string, unknown>, method = 'get') {
return {
openapi: '3.0.3',
info: { title: 'Events', version: '1' },
servers: [{ url: 'https://events.example.test' }],
paths: {
'/events': {
[method]: {
operationId: 'events.list',
parameters: [
{ name: 'q', in: 'query', schema: { type: 'string' } },
parameter
],
responses: { '200': { description: 'ok', content: { 'application/json': { schema: { type: 'array', items: { type: 'string' } } } } } }
}
}
}
};
}

describe('compound query and header parameters', () => {
it('leaves an optional array parameter out, and keeps the operation', () => {
const { manifests, errors } = compileOpenApi(spec({ name: 'eventTypes', in: 'query', style: 'form', explode: true, schema: { type: 'array', items: { type: 'string' } } }));
expect(errors).toEqual([]);
expect(manifests[0].inputs.map(input => input.name)).toEqual(['q']);
});

it('leaves an optional object header out', () => {
const { manifests } = compileOpenApi(spec({ name: 'X-Filter', in: 'header', schema: { type: 'object', properties: { a: { type: 'string' } } } }));
expect(manifests[0].inputs.map(input => input.name)).toEqual(['q']);
});

it('still refuses a write with an optional compound parameter', () => {
const { manifests, errors } = compileOpenApi(spec({ name: 'tags', in: 'query', schema: { type: 'array', items: { type: 'string' } } }, 'post'));
expect(manifests).toEqual([]);
expect(errors[0].message).toBe('tags: compound query parameters are outside the supported subset');
});

it('still refuses an operation whose required parameter is compound', () => {
const { manifests, errors } = compileOpenApi(spec({ name: 'ids', in: 'query', required: true, schema: { type: 'array', items: { type: 'string' } } }));
expect(manifests).toEqual([]);
expect(errors[0].message).toBe('ids: compound query parameters are outside the supported subset');
});
});
8 changes: 8 additions & 0 deletions src/core/capabilities/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,16 @@ function collectInputs(
return { error: `parameter location ${String(location)} is unsupported` };
}
const converted = fromJsonSchema(isRecord(parameter.schema) || Array.isArray(parameter.schema) ? parameter.schema : { type: 'string' }, spec);
const rawSchema = deref(spec, parameter.schema);
const rawCompound = isRecord(rawSchema) && (rawSchema.type === 'array' || rawSchema.type === 'object');
const readOnly = method === 'GET' || method === 'HEAD';
if (!converted.ok && readOnly && location !== 'path' && parameter.required !== true && rawCompound) continue;
if (!converted.ok) return { error: `${String(parameter.name)}: ${converted.error}` };
if ((location === 'query' || location === 'header') && (converted.schema.kind === 'object' || converted.schema.kind === 'array')) {
// An optional one can be left out of a read: the operation still
// works without it (Google Calendar's events.list and eventTypes).
// Not of a write, which would add write surface (AGENTS.md stop condition 3).
if (parameter.required !== true && (method === 'GET' || method === 'HEAD')) continue;
return { error: `${String(parameter.name)}: compound ${location} parameters are outside the supported subset` };
}
const name = String(parameter.name);
Expand Down
Loading