Skip to content

Commit 4096ac0

Browse files
committed
fix: handle enums with booleans
1 parent 30bd444 commit 4096ac0

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A powerful, highly customizable TypeScript client generator for OpenAPI specific
1111
- **Production-ready**: Used to build enterprise-grade API clients for Atlassian products
1212
- **Flexible schema loading**: Load schemas from YAML or JSON, locally or remotely
1313
- **Modern specification support**: Full support for OpenAPI 3.0 and 3.1
14+
- **Zero runtime dependencies**: Generated code has no external dependencies unless explicitly configured
1415

1516
## Real-world Examples
1617

src/schema-to-typescript/common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
OpenApiSchema,
4747
OpenApiSchemaPrimitiveValue
4848
} from '../schemas/common';
49+
import {cleanupSchema} from '../utils/cleanup-schema';
4950
import {attachJsDocComment, extractJsDoc, JsDocBlock, JsDocRenderConfig, renderJsDoc} from '../utils/jsdoc';
5051
import {applyEntityNameCase} from '../utils/string-utils';
5152
import {simplifyIntersectionTypeIfPossible, simplifyUnionTypeIfPossible} from '../utils/type-utils';
@@ -65,14 +66,15 @@ export interface GenerateSchemaTypeParams {
6566
* @see https://www.asyncapi.com/docs/specifications/2.0.0#dataTypeFormat
6667
*/
6768
export function generateSchemaType({
68-
schema,
69+
schema: originalSchema,
6970
getTypeName,
7071
expand = false,
7172
processJsDoc,
7273
processJsDocPath,
7374
getBinaryType,
7475
jsDocRenderConfig
7576
}: GenerateSchemaTypeParams): TSType {
77+
const schema = cleanupSchema(originalSchema);
7678
const commonSchemaGenerationOptions = {
7779
getTypeName,
7880
getBinaryType,

src/schema-to-typescript/common/validation-providers/zod-validation-provider/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
} from '@babel/types';
3232
import * as R from 'ramda';
3333
import {extendSchema, OpenApiSchema} from '../../../../schemas/common';
34+
import {cleanupSchema} from '../../../../utils/cleanup-schema';
3435
import {addDependencyImport, DependencyImports} from '../../../../utils/dependencies';
3536
import {isJsonMediaType} from '../../../../utils/media-types';
3637
import {objectPropertyKey, valueToAstExpression} from '../../../common';
@@ -121,7 +122,8 @@ export class ZodValidationProvider extends ValidationProvider {
121122
generateSchema(schema: OpenApiSchema, context: ValidationProviderContext): ResultWithDependencyImports<Expression> {
122123
return this.withDependencyImports(this.generateSchemaItem(schema, context));
123124
}
124-
protected generateSchemaItem(schema: OpenApiSchema, context: ValidationProviderContext): Expression {
125+
protected generateSchemaItem(originalSchema: OpenApiSchema, context: ValidationProviderContext): Expression {
126+
const schema = cleanupSchema(originalSchema);
125127
if (schema === true) {
126128
return zCall('unknown', []);
127129
}

src/utils/cleanup-schema.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {OpenApiSchema} from '../schemas/common';
2+
3+
/**
4+
* Sometimes schemas have unnecessary properties that can be removed to make the schema cleaner.
5+
*/
6+
export function cleanupSchema(schema: OpenApiSchema): OpenApiSchema {
7+
if (typeof schema === 'boolean') {
8+
return schema;
9+
}
10+
if ('enum' in schema && schema.enum) {
11+
const {enum: enumValues, ...rest} = schema;
12+
if (enumValues.length === 0) {
13+
return rest;
14+
}
15+
if (enumValues.length === 1) {
16+
return {
17+
...rest,
18+
const: enumValues[0]
19+
};
20+
}
21+
if (enumValues.length === 2 && enumValues.every((v) => typeof v === 'boolean')) {
22+
return {
23+
...rest,
24+
type: 'boolean'
25+
};
26+
}
27+
}
28+
return schema;
29+
}

0 commit comments

Comments
 (0)