fix(codegen): preserve equivalent suffixed type references (#48024)

This commit is contained in:
Aiden Cline
2026-09-08 15:21:17 -05:00
committed by GitHub
Unverified
parent a322d7f98f
commit 11aee1b49f
2 changed files with 55 additions and 3 deletions
@@ -143,6 +143,52 @@ describe("HttpApiCodegen.generate", () => {
expect(source).not.toContain("@example/api")
})
test("preserves named Effect references across optional schema occurrences", () => {
const State = Schema.Record(Schema.String, Schema.Unknown).annotate({ identifier: "Message.State" })
const output = emitEffectShape(
compileContract(
api(
HttpApiEndpoint.get("get", "/session", {
success: Schema.Struct({
encoded: Schema.toEncoded(Schema.Struct({ state: Schema.optionalKey(State) })),
optional: Schema.optional(State),
}),
}),
),
),
{
typeReferences: [
{ schema: State, name: "Message.State", import: 'import type { Message } from "@example/schema/message"' },
],
},
)
const source = output.files[0]?.content
expect(source).toContain('readonly "state"?: Message.State')
expect(source).toContain('readonly "optional"?: Message.State | undefined')
})
test("does not reuse named Effect references for different suffixed shapes", () => {
const State = Schema.Record(Schema.String, Schema.Unknown).annotate({ identifier: "Message.State" })
const Different = Schema.Record(Schema.String, Schema.Number).annotate({ identifier: "Message.State" })
const output = emitEffectShape(
compileContract(
api(
HttpApiEndpoint.get("get", "/session", {
success: Schema.Struct({ original: State, different: Different }),
}),
),
),
{
typeReferences: [
{ schema: State, name: "Message.State", import: 'import type { Message } from "@example/schema/message"' },
],
},
)
const source = output.files[0]?.content
expect(source).toContain('readonly "original": Message.State')
expect(source).toContain('readonly "different": ({ readonly [x: string]: number })')
})
test("allows composed Effect outputs to use an authoritative named type", () => {
const output = emitEffectShape(
compileContract(api(HttpApiEndpoint.get("events", "/event", { success: Schema.Unknown }))),