Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes/fix-fake-open-object.md
Original file line number Diff line number Diff line change
@@ -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".
13 changes: 13 additions & 0 deletions src/codegen/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading