Skip to content
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

feat(types): new option to create tuples from fixed length arrays #31

Merged
merged 1 commit into from
Oct 6, 2023
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
41 changes: 40 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('generateOpenAPITypes()', () => {
await generateOpenAPITypes(schema, {
generateRealEnums: true,
exportNamespaces: true,
tuplesFromFixedArraysLengthLimit: 5,
}),
),
).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -237,6 +238,7 @@ describe('generateOpenAPITypes()', () => {
await generateOpenAPITypes(schema, {
brandedTypes: ['TheSchema', 'TheSchemaClone'],
generateRealEnums: true,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: false,
}),
),
Expand Down Expand Up @@ -303,6 +305,7 @@ describe('generateOpenAPITypes()', () => {
toSource(
await generateOpenAPITypes(schema, {
generateRealEnums: true,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: false,
}),
),
Expand All @@ -320,6 +323,7 @@ describe('generateOpenAPITypes()', () => {
filterStatuses: [200, 201, 202, 300],
brandedTypes: 'schemas',
generateRealEnums: false,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: false,
}),
),
Expand All @@ -337,6 +341,7 @@ describe('generateOpenAPITypes()', () => {
baseName: 'AnotherAPI',
generateUnusedSchemas: true,
generateRealEnums: true,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: true,
}),
),
Expand Down Expand Up @@ -373,9 +378,10 @@ describe('generateJSONSchemaTypes()', () => {
expect(
toSource(
await generateJSONSchemaTypes(schema, {
generateRealEnums: true,
brandedTypes: [],
exportNamespaces: false,
generateRealEnums: true,
tuplesFromFixedArraysLengthLimit: 5,
}),
),
).toMatchInlineSnapshot(`
Expand All @@ -399,6 +405,7 @@ describe('generateTypeDeclaration()', () => {
jsonSchemaOptions: {
brandedTypes: [],
generateRealEnums: true,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: true,
},
};
Expand All @@ -423,6 +430,7 @@ describe('generateTypeDeclaration()', () => {
jsonSchemaOptions: {
brandedTypes: [],
generateRealEnums: false,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: false,
},
},
Expand Down Expand Up @@ -1037,6 +1045,35 @@ describe('generateTypeDeclaration()', () => {
`);
});

test('should create tuples from fixed length arrays', async () => {
const schema: JSONSchema7 = {
title: 'FixedArrayToTupleTest',
type: 'object',
additionalProperties: false,
required: ['data'],
properties: {
data: {
type: 'array',
items: { type: 'string' },
minItems: 4,
maxItems: 4,
},
},
};

expect(toSource(await generateTypeDeclaration(context, schema)))
.toMatchInlineSnapshot(`
"export type FixedArrayToTupleTest = NonNullable<{
data: NonNullable<[
NonNullable<string>,
NonNullable<string>,
NonNullable<string>,
NonNullable<string>
]>;
}>;"
`);
});

test('should work with tuples and rest test case schemas', async () => {
const schema: JSONSchema7 = {
title: 'TupleTest',
Expand Down Expand Up @@ -1130,6 +1167,7 @@ describe('generateTypeDeclaration()', () => {
await generateOpenAPITypes(schema, {
camelizeInputs: false,
generateRealEnums: false,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: false,
}),
),
Expand Down Expand Up @@ -1189,6 +1227,7 @@ describe('generateTypeDeclaration()', () => {
await generateOpenAPITypes(schema, {
camelizeInputs: false,
generateRealEnums: false,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: false,
}),
),
Expand Down
32 changes: 25 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const DEFAULT_JSON_SCHEMA_OPTIONS: Required<JSONSchemaOptions> = {
baseName: 'Main',
brandedTypes: [],
generateRealEnums: false,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: false,
};
export const DEFAULT_OPEN_API_OPTIONS: OpenAPITypesGenerationOptions = {
Expand All @@ -94,6 +95,7 @@ export const DEFAULT_OPEN_API_OPTIONS: OpenAPITypesGenerationOptions = {
generateUnusedSchemas: false,
camelizeInputs: true,
generateRealEnums: false,
tuplesFromFixedArraysLengthLimit: 5,
exportNamespaces: false,
requireCleanAPI: false,
};
Expand All @@ -105,6 +107,7 @@ export type OpenAPITypesGenerationOptions = {
camelizeInputs?: boolean;
brandedTypes: string[] | typeof ALL_TYPES | 'schemas';
generateRealEnums: boolean;
tuplesFromFixedArraysLengthLimit: number;
exportNamespaces: boolean;
requireCleanAPI?: boolean;
};
Expand Down Expand Up @@ -132,6 +135,7 @@ export async function generateOpenAPITypes(
camelizeInputs = DEFAULT_OPEN_API_OPTIONS.camelizeInputs,
brandedTypes = DEFAULT_OPEN_API_OPTIONS.brandedTypes,
generateRealEnums = DEFAULT_OPEN_API_OPTIONS.generateRealEnums,
tuplesFromFixedArraysLengthLimit = DEFAULT_OPEN_API_OPTIONS.tuplesFromFixedArraysLengthLimit,
exportNamespaces = DEFAULT_OPEN_API_OPTIONS.exportNamespaces,
requireCleanAPI = DEFAULT_OPEN_API_OPTIONS.requireCleanAPI,
}: Omit<OpenAPITypesGenerationOptions, 'baseName' | 'brandedTypes'> &
Expand Down Expand Up @@ -178,6 +182,7 @@ export async function generateOpenAPITypes(
? brandedTypes
: Object.keys(components.schemas).map(buildIdentifier),
generateRealEnums,
tuplesFromFixedArraysLengthLimit,
exportNamespaces,
},
seenSchemas: {},
Expand Down Expand Up @@ -740,6 +745,7 @@ type JSONSchemaOptions = {
baseName?: string;
brandedTypes: string[] | typeof ALL_TYPES;
generateRealEnums: boolean;
tuplesFromFixedArraysLengthLimit: number;
exportNamespaces: boolean;
};

Expand All @@ -758,6 +764,7 @@ export async function generateJSONSchemaTypes(
baseName = DEFAULT_JSON_SCHEMA_OPTIONS.baseName,
brandedTypes = DEFAULT_JSON_SCHEMA_OPTIONS.brandedTypes,
generateRealEnums = DEFAULT_JSON_SCHEMA_OPTIONS.generateRealEnums,
tuplesFromFixedArraysLengthLimit = DEFAULT_JSON_SCHEMA_OPTIONS.tuplesFromFixedArraysLengthLimit,
exportNamespaces = DEFAULT_JSON_SCHEMA_OPTIONS.exportNamespaces,
}: JSONSchemaOptions = DEFAULT_JSON_SCHEMA_OPTIONS,
): Promise<NodeArray<Statement>> {
Expand All @@ -773,6 +780,7 @@ export async function generateJSONSchemaTypes(
baseName,
brandedTypes,
generateRealEnums,
tuplesFromFixedArraysLengthLimit,
exportNamespaces,
},
seenSchemas: {},
Expand Down Expand Up @@ -1243,13 +1251,6 @@ async function buildArrayTypeNode(
);
}

// We may switch from items arrays to tuples when there is
// allow number of items allowed.
// if (schema.maxItems < 5) {
// const tupleTypes =
// return ast.buildTupleTypeNode(types, minItems, maxItems);
// }

const additionalItems =
schema.additionalItems ||
// Backward compatibility with old JSONSchema behavior
Expand Down Expand Up @@ -1294,6 +1295,23 @@ async function buildArrayTypeNode(
? await schemaToTypes(context, additionalItems)
: [ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)];

// Switch from arrays to tuples for small fixed length arrays
if (
'minItems' in schema &&
'maxItems' in schema &&
typeof schema.minItems === 'number' &&
typeof schema.maxItems === 'number' &&
schema.maxItems === schema.minItems &&
schema.maxItems <
context.jsonSchemaOptions.tuplesFromFixedArraysLengthLimit
) {
return ts.factory.createTupleTypeNode(
new Array(schema.minItems).fill(
types.length > 1 ? ts.factory.createUnionTypeNode(types) : types[0],
),
);
}

return ts.factory.createArrayTypeNode(
types.length > 1 ? ts.factory.createUnionTypeNode(types) : types[0],
);
Expand Down