Skip to content

feat: ouput boolean query parameters as booleans, not string enums #1017

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 1 commit into from
Mar 5, 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
3 changes: 1 addition & 2 deletions packages/openapi-generator/src/knownImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,7 @@ export const KNOWN_IMPORTS: KnownImports = {
E.right({ type: 'string', format: 'number', decodedType: 'bigint' }),
BooleanFromNumber: () =>
E.right({ type: 'number', enum: [0, 1], decodedType: 'boolean' }),
BooleanFromString: () =>
E.right({ type: 'string', enum: ['true', 'false'], decodedType: 'boolean' }),
BooleanFromString: () => E.right({ type: 'boolean' }),
DateFromISOString: () =>
E.right({ type: 'string', format: 'date-time', title: 'ISO Date String' }),
DateFromNumber: () =>
Expand Down
8 changes: 8 additions & 0 deletions packages/openapi-generator/src/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ function consolidateUnion(schema: Schema): Schema {
const consolidatableTypes = ['boolean', 'number', 'string'];
const innerSchemas = schema.schemas.map(optimize);

const booleanSchemas = innerSchemas.filter(
(s) => s.type === 'boolean' || s.decodedType === 'boolean',
);

if (booleanSchemas.length === innerSchemas.length && booleanSchemas.length > 0) {
return { type: 'boolean' };
}

const isConsolidatableType = (s: Schema): boolean => {
return (
(s.primitive && consolidatableTypes.includes(s.type)) ||
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-generator/test/openapi/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,3 @@ testCase("route with record types", ROUTE_WITH_RECORD_TYPES, {
}
}
});

8 changes: 4 additions & 4 deletions packages/openapi-generator/test/openapi/union.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ testCase("route with consolidatable union schemas", ROUTE_WITH_CONSOLIDATABLE_UN
required: true,
schema: {
oneOf: [
{ type: 'string', format: 'number' },
{ type: 'string', enum: ['true', 'false'] }
Comment on lines -218 to -219
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: it's really interesting that the order of these array elements swapped 🤔 It seems deterministic, and it is alphabetical too I suppose

{ type: 'boolean' },
{ type: 'string', format: 'number' }
]
}
},
Expand All @@ -227,15 +227,15 @@ testCase("route with consolidatable union schemas", ROUTE_WITH_CONSOLIDATABLE_UN
schema: {
oneOf: [
{ type: 'string' },
{ type: 'string', enum: ['true', 'false'] }
{ type: 'boolean' }
]
}
},
{
name: 'firstNonUnion',
in: 'query',
required: true,
schema: { type: 'string', enum: ['true', 'false'] }
schema: { type: 'boolean' }
},
{
name: 'secondNonUnion',
Expand Down