diff --git a/packages/codemode/src/tool-schema.ts b/packages/codemode/src/tool-schema.ts index 1acf34152ca..242e9363ba7 100644 --- a/packages/codemode/src/tool-schema.ts +++ b/packages/codemode/src/tool-schema.ts @@ -127,8 +127,8 @@ const renderSchema = ( ]) } if (schema.allOf) { - const members = schema.allOf.map((item) => renderSchema(item, nested, depth + 1, seen)) if (schema.allOf.some((item) => hasUnresolvedRef(item, nested.definitions))) return "unknown" + const members = schema.allOf.map((item) => renderSchema(item, nested, depth + 1, seen)) return intersection([renderSchema({ ...schema, allOf: undefined }, nested, depth + 1, seen), ...members]) } if (Array.isArray(schema.type)) { diff --git a/packages/codemode/test/signature.test.ts b/packages/codemode/test/signature.test.ts index cce387899f7..bde314c5885 100644 --- a/packages/codemode/test/signature.test.ts +++ b/packages/codemode/test/signature.test.ts @@ -344,28 +344,34 @@ describe("union schemas render every alternative", () => { expect(outputTypeScript(tool)).toBe("number | boolean") }) - test("allOf renders intersections with parenthesized union members", () => { + test("allOf keeps siblings and parenthesized union members in order", () => { const schema = { + properties: { common: { type: "boolean" } }, allOf: [{ type: "object", properties: { id: { type: "string" } } }, { type: ["string", "null"] }], } as const - expect(jsonSchemaToTypeScript(schema)).toBe("{ id?: string } & (string | null)") + expect(jsonSchemaToTypeScript(schema)).toBe("{ common?: boolean } & { id?: string } & (string | null)") + expect(jsonSchemaToTypeScript(schema, true)).toBe( + ["{", " common?: boolean,", " } & {", " id?: string,", " } & (string | null)"].join("\n"), + ) }) - test("allOf does not discard an unresolved constraint", () => { - expect(jsonSchemaToTypeScript({ allOf: [{ type: "string" }, { $ref: "https://example.com/external.json" }] })).toBe( - "unknown", - ) + test.each([false, true])("allOf does not discard an unresolved constraint (pretty=%s)", (pretty) => { + for (const $ref of ["#/$defs/Missing", "#/definitions/Missing", "https://example.com/external.json"]) { + expect(jsonSchemaToTypeScript({ allOf: [{ type: "string" }, { $ref }] }, pretty)).toBe("unknown") + expect(jsonSchemaToTypeScript({ allOf: [{ type: "string" }, { allOf: [{ $ref }] }] }, pretty)).toBe("unknown") + expect( + jsonSchemaToTypeScript({ allOf: [{ properties: { nested: { $ref } } }, { type: "string" }] }, pretty), + ).toBe("unknown") + } expect( - jsonSchemaToTypeScript({ - allOf: [{ type: "string" }, { allOf: [{ $ref: "https://example.com/external.json" }] }], - }), - ).toBe("unknown") - expect( - jsonSchemaToTypeScript({ - type: "string", - allOf: [{ $ref: "#/$defs/Constraint" }], - $defs: { Constraint: { description: "TypeScript-neutral constraint" } }, - }), + jsonSchemaToTypeScript( + { + type: "string", + allOf: [{ $ref: "#/$defs/Constraint" }], + $defs: { Constraint: { description: "TypeScript-neutral constraint" } }, + }, + pretty, + ), ).toBe("string") }) })