Skip to content

Fix boolean props #2199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2025
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
5 changes: 5 additions & 0 deletions .changeset/clever-worms-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Fix boolean object property schemas
34 changes: 21 additions & 13 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,38 +453,46 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
// properties
if (Object.keys(schemaObject.properties ?? {}).length) {
for (const [k, v] of getEntries(schemaObject.properties ?? {}, options.ctx)) {
if (typeof v !== "object" || Array.isArray(v)) {
if ((typeof v !== "object" && typeof v !== "boolean") || Array.isArray(v)) {
throw new Error(
`${options.path}: invalid property ${k}. Expected Schema Object, got ${
`${options.path}: invalid property ${k}. Expected Schema Object or boolean, got ${
Array.isArray(v) ? "Array" : typeof v
}`,
);
}

const { $ref, readOnly, hasDefault } =
typeof v === "object"
? {
$ref: "$ref" in v && v.$ref,
readOnly: "readOnly" in v && v.readOnly,
hasDefault: "default" in v && v.default !== undefined,
}
: {};

// handle excludeDeprecated option
if (options.ctx.excludeDeprecated) {
const resolved = "$ref" in v ? options.ctx.resolve<SchemaObject>(v.$ref) : v;
if (resolved?.deprecated) {
const resolved = $ref ? options.ctx.resolve<SchemaObject>($ref) : v;
if ((resolved as SchemaObject)?.deprecated) {
continue;
}
}
let optional =
schemaObject.required?.includes(k) ||
(schemaObject.required === undefined && options.ctx.propertiesRequiredByDefault) ||
("default" in v &&
(hasDefault &&
options.ctx.defaultNonNullable &&
!options.path?.includes("parameters") &&
!options.path?.includes("requestBody") &&
!options.path?.includes("requestBodies")) // can’t be required, even with defaults
? undefined
: QUESTION_TOKEN;
let type =
"$ref" in v
? oapiRef(v.$ref)
: transformSchemaObject(v, {
...options,
path: createRef([options.path, k]),
});
let type = $ref
? oapiRef($ref)
: transformSchemaObject(v, {
...options,
path: createRef([options.path, k]),
});

if (typeof options.ctx.transform === "function") {
const result = options.ctx.transform(v as SchemaObject, options);
Expand All @@ -500,7 +508,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor

const property = ts.factory.createPropertySignature(
/* modifiers */ tsModifiers({
readonly: options.ctx.immutable || ("readOnly" in v && !!v.readOnly),
readonly: options.ctx.immutable || readOnly,
}),
/* name */ tsPropertyIndex(k),
/* questionToken */ optional,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ describe("transformSchemaObject > object", () => {
// options: DEFAULT_OPTIONS,
},
],
[
"property > boolean",
{
given: {
type: "object",
required: ["truthy", "falsy"],
properties: {
truthy: true,
falsy: false,
},
},
want: `{
truthy: unknown;
falsy: never;
}`,
},
],
[
"empty",
{
Expand Down