Skip to content

Commit 0a9bb9f

Browse files
ochafikclaude
andcommitted
fix: reorder schema unions and fix stdio test type errors
- Reorder EnumSchemaSchema union: LegacyTitledEnumSchemaSchema first (preserves enumNames field that would be stripped by UntitledSingleSelectEnumSchema) - Fix stdio test type narrowing for JSONRPCMessage union type 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent dcfeb05 commit 0a9bb9f

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

scripts/generate-schemas.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,24 @@ function postProcess(content: string): string {
705705
console.log(' ✓ Added .passthrough() to ToolSchema.outputSchema');
706706
}
707707

708+
// Reorder PrimitiveSchemaDefinitionSchema union: EnumSchemaSchema must come FIRST
709+
// Otherwise, { type: 'string', enum: [...] } matches StringSchemaSchema and loses the enum field
710+
const primitiveUnionPattern = /PrimitiveSchemaDefinitionSchema\s*=\s*z\s*\n?\s*\.union\(\[StringSchemaSchema,\s*NumberSchemaSchema,\s*BooleanSchemaSchema,\s*EnumSchemaSchema\]\)/;
711+
const reorderedUnion = 'PrimitiveSchemaDefinitionSchema = z\n .union([EnumSchemaSchema, BooleanSchemaSchema, StringSchemaSchema, NumberSchemaSchema])';
712+
if (primitiveUnionPattern.test(content)) {
713+
content = content.replace(primitiveUnionPattern, reorderedUnion);
714+
console.log(' ✓ Reordered PrimitiveSchemaDefinitionSchema union (EnumSchemaSchema first)');
715+
}
716+
717+
// Reorder EnumSchemaSchema union: LegacyTitledEnumSchemaSchema must come FIRST
718+
// Otherwise, { type: 'string', enum: [...], enumNames: [...] } matches UntitledSingleSelectEnumSchema and loses enumNames
719+
const enumUnionPattern = /EnumSchemaSchema\s*=\s*z\.union\(\[SingleSelectEnumSchemaSchema,\s*MultiSelectEnumSchemaSchema,\s*LegacyTitledEnumSchemaSchema\]\)/;
720+
const reorderedEnumUnion = 'EnumSchemaSchema = z.union([LegacyTitledEnumSchemaSchema, SingleSelectEnumSchemaSchema, MultiSelectEnumSchemaSchema])';
721+
if (enumUnionPattern.test(content)) {
722+
content = content.replace(enumUnionPattern, reorderedEnumUnion);
723+
console.log(' ✓ Reordered EnumSchemaSchema union (LegacyTitledEnumSchemaSchema first)');
724+
}
725+
708726
// AST-based transforms using ts-morph
709727
const project = new Project({ useInMemoryFileSystem: true });
710728
const sourceFile = project.createSourceFile('schemas.ts', content);

src/generated/sdk.schemas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ export const LegacyTitledEnumSchemaSchema = z
17841784
* @category `elicitation/create`
17851785
*/
17861786
// Union type for all enum schemas
1787-
export const EnumSchemaSchema = z.union([SingleSelectEnumSchemaSchema, MultiSelectEnumSchemaSchema, LegacyTitledEnumSchemaSchema]);
1787+
export const EnumSchemaSchema = z.union([LegacyTitledEnumSchemaSchema, SingleSelectEnumSchemaSchema, MultiSelectEnumSchemaSchema]);
17881788

17891789
/**
17901790
* @description The client's response to an elicitation request.
@@ -2303,7 +2303,7 @@ without nested objects or arrays.
23032303
* @category `elicitation/create`
23042304
*/
23052305
export const PrimitiveSchemaDefinitionSchema = z
2306-
.union([StringSchemaSchema, NumberSchemaSchema, BooleanSchemaSchema, EnumSchemaSchema])
2306+
.union([EnumSchemaSchema, BooleanSchemaSchema, StringSchemaSchema, NumberSchemaSchema])
23072307
.describe('Restricted schema definitions that only allow primitive types\nwithout nested objects or arrays.');
23082308

23092309
/**

test/client/stdio.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ test('should read messages', async () => {
5454

5555
// Compare message content instead of JSON.stringify (Zod reorders keys)
5656
const msg1 = messages[1];
57-
if (message.method === msg1.method && message.jsonrpc === msg1.jsonrpc) {
57+
if ('method' in message && 'method' in msg1 &&
58+
message.method === msg1.method && message.jsonrpc === msg1.jsonrpc) {
5859
resolve();
5960
}
6061
};

test/server/stdio.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ test('should read multiple messages', async () => {
8989
readMessages.push(message);
9090
// Compare message content instead of JSON.stringify (Zod reorders keys)
9191
const msg1 = messages[1];
92-
if (message.method === msg1.method && message.jsonrpc === msg1.jsonrpc) {
92+
if ('method' in message && 'method' in msg1 &&
93+
message.method === msg1.method && message.jsonrpc === msg1.jsonrpc) {
9394
resolve();
9495
}
9596
};

0 commit comments

Comments
 (0)