diff --git a/changes/fix-fake-open-object.md b/changes/fix-fake-open-object.md new file mode 100644 index 0000000..2b15a0a --- /dev/null +++ b/changes/fix-fake-open-object.md @@ -0,0 +1,4 @@ +### Fixed + +- **A schema's `additionalProperties: true` (or `{}`) stays open.** `adaptOpenApi` dropped it, so `fakeApi` refused + every field of an object the spec leaves open, such as Attio's records filter and its `$or`, as "no such field". diff --git a/src/codegen/openapi.test.ts b/src/codegen/openapi.test.ts index fa6bdfe..cd9f3ab 100644 --- a/src/codegen/openapi.test.ts +++ b/src/codegen/openapi.test.ts @@ -255,6 +255,19 @@ describe("adaptOpenApi reads the OpenAPI 3.1 shapes Notion's file uses", () => { }) }) + it("keeps an object the spec leaves open to any field, and a closed or silent one closed", () => { + const input = document({ + Filter: { type: "object", additionalProperties: true, example: { name: "Ada" } }, + Empty: { type: "object", additionalProperties: {} }, + Closed: { type: "object", properties: { name: { type: "string" } }, additionalProperties: false }, + Silent: { type: "object", properties: { name: { type: "string" } } }, + }) + expect(schema(input, "Filter")?.schema).toMatchObject({ type: "object", additionalProperties: { type: "unknown" } }) + expect(schema(input, "Empty")?.schema).toMatchObject({ additionalProperties: { type: "unknown" } }) + expect(schema(input, "Closed")?.schema).not.toHaveProperty("additionalProperties") + expect(schema(input, "Silent")?.schema).not.toHaveProperty("additionalProperties") + }) + it("distributes an allOf over its union member, inline or behind a reference", () => { const common = { type: "object", diff --git a/src/codegen/openapi.ts b/src/codegen/openapi.ts index 1fcac58..eba1c20 100644 --- a/src/codegen/openapi.ts +++ b/src/codegen/openapi.ts @@ -415,9 +415,12 @@ class Adapter { properties, required: Array.isArray(source.required) ? source.required.map(String) : [], } - // `true` and `{}` say "anything else may appear", which a loose object already allows. + // `true` and `{}` say "anything else may appear": kept as any value, so a strict fake takes a field the spec leaves + // open (Attio's records filter, whose `$or` it lists nowhere). if (isObject(source.additionalProperties) && Object.keys(source.additionalProperties).length > 0) { node.additionalProperties = this.schema(source.additionalProperties, `${where}.*`) + } else if (source.additionalProperties === true || isObject(source.additionalProperties)) { + node.additionalProperties = { type: "unknown" } } return node }