Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): flag a hand type widened to a union in the shape gate

Yudhi Armyndharis4756e648c5d8
diff --git a/scripts/check-contract-shapes.mjs b/scripts/check-contract-shapes.mjs
index 511a5fc8b..64168bed3 100644
--- a/scripts/check-contract-shapes.mjs
+++ b/scripts/check-contract-shapes.mjs
@@ -30,7 +30,8 @@
*
* What one comparison covers, per mapped pair: field-name sets in both directions, required vs
* optional (hand `?` vs the schema's `required` array), and — for fields whose both sides reduce
- * to a simple token (primitive, enum literal set, array of those, null union) — the token itself,
+ * to a simple token (primitive, enum literal set, array of those, null union; on the hand side, any
+ * union too) — the token itself,
* which is what catches `string` widened to `string | number` or a re-ordered enum growing a
* member. Complex/nested fields are compared by presence and optionality only; that limit is
* deliberate (the hand parser stays regular), and the exclusions below record what is known to be
@@ -688,7 +689,8 @@ export function comparePair(handName, handMembers, schemaName, schema, schemas,
}
}
const absorbs = handInfo.absorbsNull && contract === `${hand}|null`;
- if (hand !== contract && !absorbs && isSimpleToken(hand)) {
+ // A union never equals a simple contract token, so it is drift, not a shape too complex to read.
+ if (hand !== contract && !absorbs && (isSimpleToken(hand) || hand.startsWith('union('))) {
diffs.push(`"${field}": hand ${hand}, contract ${contract}`);
}
}
diff --git a/scripts/check-contract-shapes.spec.mjs b/scripts/check-contract-shapes.spec.mjs
index 3c26da3a8..e234c5b7e 100644
--- a/scripts/check-contract-shapes.spec.mjs
+++ b/scripts/check-contract-shapes.spec.mjs
@@ -99,6 +99,19 @@ test('comparePair flags a widened union and an enum mismatch at token level', ()
assert.ok(diffs.some(d => d.includes('"count": hand number|null, contract number')));
});
+test('comparePair flags a primitive widened to a union of primitives', () => {
+ // A two-member union reduces to union(...), which is not a simple token; dropping the diff on that
+ // ground let `string | number` (or Python's `str | int`) stand in for a plain `string` unseen.
+ const hand = {
+ id: { optional: false, token: 'string | number' },
+ direction: { optional: false, token: "'incoming' | 'outgoing'" },
+ count: { optional: false, token: 'str | int' },
+ };
+ const diffs = comparePair('Sample', hand, 'SampleDto', schemas.SampleDto, schemas);
+ assert.ok(diffs.some(d => d.includes('"id": hand union(number,string), contract string')));
+ assert.ok(diffs.some(d => d.includes('"count": hand union(int,str), contract number')));
+});
+
test('parseObjectToken splits nested object tokens without losing members', () => {
const members = parseObjectToken('object(id:string,sub:object(a:number,b?:boolean),tail:string)');
assert.deepEqual(Object.keys(members), ['id', 'sub', 'tail']);