Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
314c98f6ec | ||
|
|
b02e1c2452 |
No files matched your search
@@ -191,10 +191,19 @@ describe("compact advertised input schema", () => {
|
||||
const properties = schema.properties as Record<string, unknown>;
|
||||
expect(properties.normalized_result).toEqual({
|
||||
type: ["null", "boolean", "object", "array", "number", "string"],
|
||||
items: {},
|
||||
});
|
||||
expect(properties.literal).toEqual({ const: {} });
|
||||
const validate = new Ajv2020({ strict: false }).compile(schema);
|
||||
for (const normalized_result of [null, true, {}, [], 42, "result"])
|
||||
for (const normalized_result of [
|
||||
null,
|
||||
true,
|
||||
{},
|
||||
[],
|
||||
[[1], "x"],
|
||||
42,
|
||||
"result",
|
||||
])
|
||||
expect(validate({ normalized_result })).toBe(true);
|
||||
});
|
||||
|
||||
@@ -267,6 +276,7 @@ describe("compact input schema budget and reduction", () => {
|
||||
type: "array",
|
||||
items: {
|
||||
type: ["null", "boolean", "object", "array", "number", "string"],
|
||||
items: {},
|
||||
},
|
||||
},
|
||||
label: { type: ["string", "null"] },
|
||||
@@ -335,3 +345,47 @@ describe("compact input schema budget and reduction", () => {
|
||||
expect(serializedBytes(schema)).toBeLessThanOrEqual(13312);
|
||||
});
|
||||
});
|
||||
|
||||
it("gives every advertised array type an item schema for strict clients", () => {
|
||||
const arraysWithoutItems = (value: unknown): number => {
|
||||
if (Array.isArray(value))
|
||||
return value.reduce(
|
||||
(count: number, child) => count + arraysWithoutItems(child),
|
||||
0,
|
||||
);
|
||||
if (typeof value !== "object" || value === null) return 0;
|
||||
const record = value as Record<string, unknown>;
|
||||
const types = Array.isArray(record.type) ? record.type : [record.type];
|
||||
return (
|
||||
(types.includes("array") && record.items === undefined ? 1 : 0) +
|
||||
Object.values(record).reduce(
|
||||
(count: number, child) => count + arraysWithoutItems(child),
|
||||
0,
|
||||
)
|
||||
);
|
||||
};
|
||||
// A large nested property forces the reduced form at 1 KiB while the
|
||||
// shallow properties, including the array, remain advertised.
|
||||
for (const [budget, expected] of [
|
||||
[13312, "inline"],
|
||||
[1024, "reduced"],
|
||||
] as const) {
|
||||
const { schema, presentation } = compactAdvertisedInputSchema(
|
||||
{
|
||||
type: "object",
|
||||
properties: {
|
||||
capture: budget === 1024 ? oversizedPairSchema() : {},
|
||||
value: {},
|
||||
values: { type: "array", items: {} },
|
||||
},
|
||||
},
|
||||
budget,
|
||||
);
|
||||
expect(presentation, String(budget)).toBe(expected);
|
||||
expect(
|
||||
(schema.properties as Record<string, unknown>).values,
|
||||
String(budget),
|
||||
).toMatchObject({ type: "array", items: expect.any(Object) });
|
||||
expect(arraysWithoutItems(schema), String(budget)).toBe(0);
|
||||
}
|
||||
});
|
||||
@@ -23,6 +23,9 @@ const jsonValueTypes = [
|
||||
"number",
|
||||
"string",
|
||||
];
|
||||
// Strict clients such as VS Code reject an array type without `items`, so
|
||||
// the explicit any-value form also accepts every array element.
|
||||
const jsonValueSchema = { type: jsonValueTypes, items: {} };
|
||||
|
||||
export type AdvertisedInputSchemaPresentation = "inline" | "reduced";
|
||||
|
||||
@@ -157,7 +160,7 @@ const inlineWithinBudget = (
|
||||
yield "]";
|
||||
} else if (isSchemaObject(value)) {
|
||||
if (position === "schema" && Object.keys(value).length === 0) {
|
||||
yield JSON.stringify({ type: jsonValueTypes });
|
||||
yield JSON.stringify(jsonValueSchema);
|
||||
return;
|
||||
}
|
||||
if (position === "schema" && typeof value.$ref === "string") {
|
||||
@@ -293,9 +296,7 @@ const reduceToShallowProperties = (
|
||||
...(type === undefined ? {} : { type }),
|
||||
// Reduced inputs intentionally omit nested validation, but clients
|
||||
// still require an item schema to accept an array declaration.
|
||||
...(types?.includes("array")
|
||||
? { items: { type: jsonValueTypes } }
|
||||
: {}),
|
||||
...(types?.includes("array") ? { items: jsonValueSchema } : {}),
|
||||
...description,
|
||||
},
|
||||
];
|
||||
|
||||
Reference in new issue
Block a user