Skip to content

Commit de2c426

Browse files
committed
feat: Describe the items of arrays with mixed element types
Until now an array got `items` only if all its elements had the same schema; `[42, "foo"]` got none. Now that `items` is a list, as the spec intends, it can describe mixed arrays by listing each distinct element schema, the way appmap-ruby does: `"items": [{"class": "Number"}, {"class": "String"}]`. To keep AppMaps from bloating on, say, a long array of objects with varying keys, an array with more than 10 distinct item schemas still gets no `items`. Unlike appmap-ruby (first 5 elements), all elements are still examined, as before, so the schema list is complete. Assisted-by: Claude:claude-opus-5-5
1 parent 9da0179 commit de2c426

4 files changed

Lines changed: 105 additions & 11 deletions

File tree

‎src/__tests__/parameter.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ const examples: [unknown, string, string, Partial<AppMap.Parameter>][] = [
1717
[new Klass(42), "Klass", "Klass { value: 42 }", {}],
1818
[[], "Array", "[]", { size: 0 }],
1919
[[42, 43], "Array", "[ 42, 43 ]", { size: 2, items: [{ class: "Number" }] }],
20-
[[42, "foo"], "Array", "[ 42, 'foo' ]", { size: 2 }],
20+
[
21+
[42, "foo", 43],
22+
"Array",
23+
"[ 42, 'foo', 43 ]",
24+
{ size: 3, items: [{ class: "Number" }, { class: "String" }] },
25+
],
2126
[[0, 1], "Array", "[ 0, 1 ]", { size: 2, items: [{ class: "Number" }] }],
2227
[["", "a"], "Array", "[ '', 'a' ]", { size: 2, items: [{ class: "String" }] }],
2328
[
@@ -147,4 +152,17 @@ describe(parameter, () => {
147152
jest.replaceProperty(config(), "valueMaxLength", 0);
148153
expect(parameter("x".repeat(5000)).value).toHaveLength(5002);
149154
});
155+
156+
it("omits items of arrays with too many distinct item schemas", () => {
157+
const value = Array.from({ length: 11 }, (_, i) => ({ [`key${i}`]: i }));
158+
expect(parameter(value)).not.toHaveProperty("items");
159+
expect(parameter(value.slice(1))).toHaveProperty("items.length", 10);
160+
});
161+
162+
it("lists the schema of a repeated object once", () => {
163+
const obj = { foo: 42 };
164+
expect(parameter([obj, obj]).items).toStrictEqual([
165+
{ class: "Object", properties: [{ name: "foo", class: "Number" }] },
166+
]);
167+
});
150168
});

‎src/parameter.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function parameterSchema(
164164

165165
if (value instanceof Array) {
166166
const items = itemsSchema(value, objectsSeen, depth - 1);
167-
if (items) result.items = [items];
167+
if (items) result.items = items;
168168
} else if (isSimpleObject(value))
169169
result.properties = propertiesSchema(value, objectsSeen, depth - 1);
170170
if (value && typeof value === "object") objectsSeen.delete(value);
@@ -181,19 +181,23 @@ function isSimpleObject(value: unknown): value is Record<string, unknown> {
181181
}
182182
}
183183

184+
// Arrays with more distinct item schemas than this get no `items` at all;
185+
// listing them all would bloat the AppMap without telling much.
186+
const maxItemSchemas = 10;
187+
184188
function itemsSchema(
185189
value: unknown[],
186190
objectsSeen?: Set<object>,
187191
depth = defaultDepth,
188-
): AppMap.ParameterSchema | undefined {
189-
if (value.length === 0) return;
190-
const [head, ...tail] = value;
191-
192-
const schema = parameterSchema(head, objectsSeen, depth);
193-
for (const item of tail)
194-
if (!isDeepStrictEqual(schema, parameterSchema(item, objectsSeen, depth))) return;
195-
196-
return schema;
192+
): AppMap.ParameterSchema[] | undefined {
193+
const schemas: AppMap.ParameterSchema[] = [];
194+
for (const item of value) {
195+
const schema = parameterSchema(item, objectsSeen, depth);
196+
if (schemas.some((s) => isDeepStrictEqual(s, schema))) continue;
197+
if (schemas.length === maxItemSchemas) return;
198+
schemas.push(schema);
199+
}
200+
if (schemas.length > 0) return schemas;
197201
}
198202

199203
function propertiesSchema(

‎test/__snapshots__/httpServer.test.ts.snap‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,14 @@ exports[`mapping Express.js requests 1`] = `
430430
},
431431
{
432432
"class": "Array",
433+
"items": [
434+
{
435+
"class": "Number",
436+
},
437+
{
438+
"class": "String",
439+
},
440+
],
433441
"name": "heterogenous",
434442
"object_id": 10,
435443
"size": 2,
@@ -490,6 +498,14 @@ exports[`mapping Express.js requests 1`] = `
490498
},
491499
{
492500
"class": "Array",
501+
"items": [
502+
{
503+
"class": "Number",
504+
},
505+
{
506+
"class": "String",
507+
},
508+
],
493509
"name": "heterogenous",
494510
},
495511
],
@@ -554,6 +570,14 @@ exports[`mapping Express.js requests 1`] = `
554570
},
555571
{
556572
"class": "Array",
573+
"items": [
574+
{
575+
"class": "Number",
576+
},
577+
{
578+
"class": "String",
579+
},
580+
],
557581
"name": "heterogenous",
558582
},
559583
],
@@ -619,6 +643,14 @@ exports[`mapping Express.js requests 1`] = `
619643
},
620644
{
621645
"class": "Array",
646+
"items": [
647+
{
648+
"class": "Number",
649+
},
650+
{
651+
"class": "String",
652+
},
653+
],
622654
"name": "heterogenous",
623655
},
624656
],
@@ -990,6 +1022,14 @@ exports[`mapping Express.js requests with remote recording 1`] = `
9901022
},
9911023
{
9921024
"class": "Array",
1025+
"items": [
1026+
{
1027+
"class": "Number",
1028+
},
1029+
{
1030+
"class": "String",
1031+
},
1032+
],
9931033
"name": "heterogenous",
9941034
"object_id": 10,
9951035
"size": 2,
@@ -1050,6 +1090,14 @@ exports[`mapping Express.js requests with remote recording 1`] = `
10501090
},
10511091
{
10521092
"class": "Array",
1093+
"items": [
1094+
{
1095+
"class": "Number",
1096+
},
1097+
{
1098+
"class": "String",
1099+
},
1100+
],
10531101
"name": "heterogenous",
10541102
},
10551103
],
@@ -1114,6 +1162,14 @@ exports[`mapping Express.js requests with remote recording 1`] = `
11141162
},
11151163
{
11161164
"class": "Array",
1165+
"items": [
1166+
{
1167+
"class": "Number",
1168+
},
1169+
{
1170+
"class": "String",
1171+
},
1172+
],
11171173
"name": "heterogenous",
11181174
},
11191175
],
@@ -1179,6 +1235,14 @@ exports[`mapping Express.js requests with remote recording 1`] = `
11791235
},
11801236
{
11811237
"class": "Array",
1238+
"items": [
1239+
{
1240+
"class": "Number",
1241+
},
1242+
{
1243+
"class": "String",
1244+
},
1245+
],
11821246
"name": "heterogenous",
11831247
},
11841248
],

‎test/__snapshots__/mysql.test.ts.snap‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ exports[`mapping MySQL tests 1`] = `
346346
},
347347
{
348348
"class": "Array",
349+
"items": [
350+
{
351+
"class": "Number",
352+
},
353+
{
354+
"class": "String",
355+
},
356+
],
349357
"object_id": 6,
350358
"size": 2,
351359
"value": "[ 1, 'ABC' ]",

0 commit comments

Comments
 (0)