Skip to content

Commit fdd319e

Browse files
authored
Fix unsupported object annotations in schema code generation (#10532)
1 parent 9c41784 commit fdd319e

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix `SchemaRepresentation.toCodeDocument` to omit annotations containing unsupported objects instead of emitting incorrect object literals.

‎packages/effect/src/internal/schema/toCodeDocument.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ function renderEmittableAnnotation(input: unknown): string | undefined {
4343
return `[${values.join(", ")}]`
4444
}
4545

46+
const prototype = Object.getPrototypeOf(input)
47+
// Plain objects from other realms also have a prototype whose prototype is null.
48+
if (
49+
prototype !== null &&
50+
prototype !== Object.prototype &&
51+
Object.getPrototypeOf(prototype) !== null
52+
) return undefined
53+
4654
const entries: Array<string> = []
4755
for (const [key, value] of Object.entries(input)) {
4856
const rendered = renderEmittableAnnotation(value)

‎packages/effect/test/schema/representation/toCodeDocument.annotations.test.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assert, describe, it } from "@effect/vitest"
22
import { Schema, SchemaRepresentation } from "effect"
3+
import { runInNewContext } from "node:vm"
34
import { assertInclude, throws } from "../../utils/assert.ts"
45

56
function expectError(thunk: () => void, expected: string | Error): void {
@@ -105,7 +106,9 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
105106
symbol: Symbol.for("shared"),
106107
nan: NaN,
107108
positive: Infinity,
108-
negative: -Infinity
109+
negative: -Infinity,
110+
nullPrototype: Object.create(null),
111+
crossRealm: runInNewContext("({ value: 1 })") as unknown
109112
},
110113
omitted: { value: 1, callback: () => 2 }
111114
}).ast
@@ -117,10 +120,33 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
117120
assertInclude(runtime, `"nan": NaN`)
118121
assertInclude(runtime, `"positive": Infinity`)
119122
assertInclude(runtime, `"negative": -Infinity`)
123+
assertInclude(runtime, `"nullPrototype": {}`)
124+
assertInclude(runtime, `"crossRealm": { "value": 1 }`)
120125
assert.isFalse(runtime.includes("omitted"))
121126
assert.isFalse(runtime.includes("callback"))
122127
})
123128

129+
it("omits unsupported object annotations atomically", () => {
130+
const schema = Schema.String.annotate({
131+
kept: { value: 1 },
132+
map: new Map([["a", 1]]),
133+
set: new Set([1]),
134+
date: new Date(0),
135+
bytes: new Uint8Array([1, 2]),
136+
instance: new (class {
137+
value = 1
138+
})(),
139+
nested: { value: 1, map: new Map([["a", 1]]) },
140+
array: [1, new Set([1])]
141+
})
142+
const document = SchemaRepresentation.toRepresentations([schema.ast])
143+
144+
assert.strictEqual(
145+
SchemaRepresentation.toCodeDocument(document).codes[0].runtime,
146+
`Schema.String.annotate({ "kept": { "value": 1 } })`
147+
)
148+
})
149+
124150
it("preserves top-level and nested __proto__ annotations in generated code", () => {
125151
const value = { ["__proto__"]: { trusted: true } }
126152
const annotations = { ...value, examples: [value] }
@@ -153,7 +179,8 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
153179
type: StringRepresentation,
154180
annotations: {
155181
element: { value: 1 },
156-
omitted: { callback: () => 1 }
182+
omitted: { callback: () => 1 },
183+
map: new Map([["a", 1]])
157184
}
158185
}],
159186
rest: [],
@@ -166,7 +193,7 @@ describe("SchemaRepresentation.toCodeDocument annotations", () => {
166193
type: NumberRepresentation,
167194
isOptional: false,
168195
isMutable: false,
169-
annotations: { property: true }
196+
annotations: { property: true, nested: { set: new Set([1]) } }
170197
}],
171198
indexSignatures: [],
172199
checks: []

0 commit comments

Comments
 (0)