From bf0da1767003cacba9fa46cbd0e8a27a6860e752 Mon Sep 17 00:00:00 2001 From: Tomasz Ciecierski Date: Fri, 30 Aug 2024 13:24:11 +0200 Subject: [PATCH] revert all the changes and use casting --- .../src/template_service/register_helpers.ts | 15 +- .../templates/zod_operation_schema.handlebars | 35 +-- .../model/exception_list_item_entry.gen.ts | 28 +- .../run_type_check_cli.ts | 7 +- .../model/rule_schema/rule_schemas.gen.ts | 257 +++++++++--------- .../bulk_actions/bulk_actions_route.gen.ts | 26 +- 6 files changed, 171 insertions(+), 197 deletions(-) diff --git a/packages/kbn-openapi-generator/src/template_service/register_helpers.ts b/packages/kbn-openapi-generator/src/template_service/register_helpers.ts index 596a5665a52e4..3c2235b780b87 100644 --- a/packages/kbn-openapi-generator/src/template_service/register_helpers.ts +++ b/packages/kbn-openapi-generator/src/template_service/register_helpers.ts @@ -87,21 +87,8 @@ export function registerHelpers(handlebarsInstance: typeof Handlebars) { return circularRefs.has(`#/components/schemas/${schemaName}`); } ); - // Helper to chunk an array of anyOf schemas into smaller arrays of a specified size in order to fix the typescript infer limitation - handlebarsInstance.registerHelper('createBatch', ({ anyOf }, size) => { - if (anyOf) { - const chunks = []; - for (let i = 0; i < anyOf.length; i += size) { - chunks.push(anyOf.slice(i, i + size)); - } - return chunks; - } - }); - handlebarsInstance.registerHelper('generateBatchName', function (title = '', index) { - return `${title}Batch${index + 1}`; - }); - handlebarsInstance.registerHelper('checkIfShouldBatch', function ({ anyOf }, options) { + handlebarsInstance.registerHelper('shouldCastExplicitly', function ({ anyOf }, options) { if (anyOf?.length > 2) { // @ts-expect-error return options.fn(this); diff --git a/packages/kbn-openapi-generator/src/template_service/templates/zod_operation_schema.handlebars b/packages/kbn-openapi-generator/src/template_service/templates/zod_operation_schema.handlebars index 663c957dc3b3f..ddf6156beced0 100644 --- a/packages/kbn-openapi-generator/src/template_service/templates/zod_operation_schema.handlebars +++ b/packages/kbn-openapi-generator/src/template_service/templates/zod_operation_schema.handlebars @@ -31,36 +31,17 @@ export type {{@key}} = {{> ts_type}}; export type {{@key}}Input = {{> ts_input_type }}; export const {{@key}}: z.ZodType<{{@key}}, ZodTypeDef, {{@key}}Input> = {{> zod_schema_item }}; {{else}} -{{#checkIfShouldBatch this}} -{{~#each (createBatch this 2)~}} -const {{generateBatchName ../title @index}} = z.{{#if ../discriminator.propertyName}} - discriminatedUnion('{{../discriminator.propertyName}}', [ -{{else}} - union([ -{{/if}} -{{#each this}} - {{~> zod_schema_item ~}}, -{{/each}} -]); -{{~/each~}} -export type {{@key}} = z.infer< -{{~#each (createBatch this 2)~}} - | typeof {{generateBatchName ../title @index}} -{{~/each~}} ->; -export const {{@key}} = z.{{#if ../discriminator.propertyName}} -discriminatedUnion('{{../discriminator.propertyName}}', [ -{{else}} -union([ -{{/if}} -{{~#each (createBatch this 2)~}} -{{generateBatchName ../title @index}}, -{{~/each~}} -]) as z.ZodType<{{@key}}>; +{{#shouldCastExplicitly this}} +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const {{@key}}Internal = {{> zod_schema_item}}; + +export type {{@key}} = z.infer; +export const {{@key}} = {{> zod_schema_item}} as z.ZodType as z.ZodType<{{@key}}>; {{else}} export type {{@key}} = z.infer; export const {{@key}} = {{> zod_schema_item}}; -{{/checkIfShouldBatch}} +{{/shouldCastExplicitly}} {{/if}} {{#if enum}} {{#unless (isSingle enum)}} diff --git a/packages/kbn-securitysolution-exceptions-common/api/model/exception_list_item_entry.gen.ts b/packages/kbn-securitysolution-exceptions-common/api/model/exception_list_item_entry.gen.ts index a0b59f59bedd8..b9c0e3938c579 100644 --- a/packages/kbn-securitysolution-exceptions-common/api/model/exception_list_item_entry.gen.ts +++ b/packages/kbn-securitysolution-exceptions-common/api/model/exception_list_item_entry.gen.ts @@ -85,28 +85,26 @@ export const ExceptionListItemEntryMatchWildcard = z.object({ operator: ExceptionListItemEntryOperator, }); -const ExceptionListItemEntryBatch1 = z.discriminatedUnion('type', [ +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const ExceptionListItemEntryInternal = z.discriminatedUnion('type', [ ExceptionListItemEntryMatch, ExceptionListItemEntryMatchAny, -]); -const ExceptionListItemEntryBatch2 = z.discriminatedUnion('type', [ ExceptionListItemEntryList, ExceptionListItemEntryExists, -]); -const ExceptionListItemEntryBatch3 = z.discriminatedUnion('type', [ ExceptionListItemEntryNested, ExceptionListItemEntryMatchWildcard, ]); -export type ExceptionListItemEntry = z.infer< - | typeof ExceptionListItemEntryBatch1 - | typeof ExceptionListItemEntryBatch2 - | typeof ExceptionListItemEntryBatch3 ->; -export const ExceptionListItemEntry = z.union([ - ExceptionListItemEntryBatch1, - ExceptionListItemEntryBatch2, - ExceptionListItemEntryBatch3, -]) as z.ZodType; + +export type ExceptionListItemEntry = z.infer; +export const ExceptionListItemEntry = z.discriminatedUnion('type', [ + ExceptionListItemEntryMatch, + ExceptionListItemEntryMatchAny, + ExceptionListItemEntryList, + ExceptionListItemEntryExists, + ExceptionListItemEntryNested, + ExceptionListItemEntryMatchWildcard, +]) as z.ZodType as z.ZodType; export type ExceptionListItemEntryArray = z.infer; export const ExceptionListItemEntryArray = z.array(ExceptionListItemEntry); diff --git a/packages/kbn-ts-type-check-cli/run_type_check_cli.ts b/packages/kbn-ts-type-check-cli/run_type_check_cli.ts index cd2dff8c4d1fd..3f4385118c9c6 100644 --- a/packages/kbn-ts-type-check-cli/run_type_check_cli.ts +++ b/packages/kbn-ts-type-check-cli/run_type_check_cli.ts @@ -118,7 +118,12 @@ run( await procRunner.run('tsc', { cmd: Path.relative(REPO_ROOT, require.resolve('typescript/bin/tsc')), - args: ['-b', relative, '--pretty', '--verbose'], + args: [ + '-b', + relative, + '--pretty', + ...(flagsReader.boolean('verbose') ? ['--verbose'] : []), + ], env: { NODE_OPTIONS: '--max-old-space-size=8192', }, diff --git a/x-pack/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schemas.gen.ts b/x-pack/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schemas.gen.ts index e644d92d5e4e0..ea1bc1d29a176 100644 --- a/x-pack/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schemas.gen.ts +++ b/x-pack/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schemas.gen.ts @@ -599,172 +599,177 @@ export const EsqlRuleUpdateProps = SharedUpdateProps.merge(EsqlRuleCreateFields) export type EsqlRulePatchProps = z.infer; export const EsqlRulePatchProps = SharedPatchProps.merge(EsqlRulePatchFields.partial()); -const TypeSpecificCreatePropsBatch1 = z.discriminatedUnion('type', [ +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const TypeSpecificCreatePropsInternal = z.discriminatedUnion('type', [ EqlRuleCreateFields, QueryRuleCreateFields, -]); -const TypeSpecificCreatePropsBatch2 = z.discriminatedUnion('type', [ SavedQueryRuleCreateFields, ThresholdRuleCreateFields, -]); -const TypeSpecificCreatePropsBatch3 = z.discriminatedUnion('type', [ ThreatMatchRuleCreateFields, MachineLearningRuleCreateFields, -]); -const TypeSpecificCreatePropsBatch4 = z.discriminatedUnion('type', [ NewTermsRuleCreateFields, EsqlRuleCreateFields, ]); -export type TypeSpecificCreateProps = z.infer< - | typeof TypeSpecificCreatePropsBatch1 - | typeof TypeSpecificCreatePropsBatch2 - | typeof TypeSpecificCreatePropsBatch3 - | typeof TypeSpecificCreatePropsBatch4 ->; -export const TypeSpecificCreateProps = z.union([ - TypeSpecificCreatePropsBatch1, - TypeSpecificCreatePropsBatch2, - TypeSpecificCreatePropsBatch3, - TypeSpecificCreatePropsBatch4, -]) as z.ZodType; - -const TypeSpecificPatchPropsBatch1 = z.union([EqlRulePatchFields, QueryRulePatchFields]); -const TypeSpecificPatchPropsBatch2 = z.union([SavedQueryRulePatchFields, ThresholdRulePatchFields]); -const TypeSpecificPatchPropsBatch3 = z.union([ + +export type TypeSpecificCreateProps = z.infer; +export const TypeSpecificCreateProps = z.discriminatedUnion('type', [ + EqlRuleCreateFields, + QueryRuleCreateFields, + SavedQueryRuleCreateFields, + ThresholdRuleCreateFields, + ThreatMatchRuleCreateFields, + MachineLearningRuleCreateFields, + NewTermsRuleCreateFields, + EsqlRuleCreateFields, +]) as z.ZodType as z.ZodType; + +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const TypeSpecificPatchPropsInternal = z.union([ + EqlRulePatchFields, + QueryRulePatchFields, + SavedQueryRulePatchFields, + ThresholdRulePatchFields, ThreatMatchRulePatchFields, MachineLearningRulePatchFields, + NewTermsRulePatchFields, + EsqlRulePatchFields, ]); -const TypeSpecificPatchPropsBatch4 = z.union([NewTermsRulePatchFields, EsqlRulePatchFields]); -export type TypeSpecificPatchProps = z.infer< - | typeof TypeSpecificPatchPropsBatch1 - | typeof TypeSpecificPatchPropsBatch2 - | typeof TypeSpecificPatchPropsBatch3 - | typeof TypeSpecificPatchPropsBatch4 ->; + +export type TypeSpecificPatchProps = z.infer; export const TypeSpecificPatchProps = z.union([ - TypeSpecificPatchPropsBatch1, - TypeSpecificPatchPropsBatch2, - TypeSpecificPatchPropsBatch3, - TypeSpecificPatchPropsBatch4, -]) as z.ZodType; + EqlRulePatchFields, + QueryRulePatchFields, + SavedQueryRulePatchFields, + ThresholdRulePatchFields, + ThreatMatchRulePatchFields, + MachineLearningRulePatchFields, + NewTermsRulePatchFields, + EsqlRulePatchFields, +]) as z.ZodType as z.ZodType; -const TypeSpecificResponseBatch1 = z.discriminatedUnion('type', [ +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const TypeSpecificResponseInternal = z.discriminatedUnion('type', [ EqlRuleResponseFields, QueryRuleResponseFields, -]); -const TypeSpecificResponseBatch2 = z.discriminatedUnion('type', [ SavedQueryRuleResponseFields, ThresholdRuleResponseFields, -]); -const TypeSpecificResponseBatch3 = z.discriminatedUnion('type', [ ThreatMatchRuleResponseFields, MachineLearningRuleResponseFields, -]); -const TypeSpecificResponseBatch4 = z.discriminatedUnion('type', [ NewTermsRuleResponseFields, EsqlRuleResponseFields, ]); -export type TypeSpecificResponse = z.infer< - | typeof TypeSpecificResponseBatch1 - | typeof TypeSpecificResponseBatch2 - | typeof TypeSpecificResponseBatch3 - | typeof TypeSpecificResponseBatch4 ->; -export const TypeSpecificResponse = z.union([ - TypeSpecificResponseBatch1, - TypeSpecificResponseBatch2, - TypeSpecificResponseBatch3, - TypeSpecificResponseBatch4, -]) as z.ZodType; - -const RuleCreatePropsBatch1 = z.discriminatedUnion('type', [ + +export type TypeSpecificResponse = z.infer; +export const TypeSpecificResponse = z.discriminatedUnion('type', [ + EqlRuleResponseFields, + QueryRuleResponseFields, + SavedQueryRuleResponseFields, + ThresholdRuleResponseFields, + ThreatMatchRuleResponseFields, + MachineLearningRuleResponseFields, + NewTermsRuleResponseFields, + EsqlRuleResponseFields, +]) as z.ZodType as z.ZodType; + +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const RuleCreatePropsInternal = z.discriminatedUnion('type', [ EqlRuleCreateProps, QueryRuleCreateProps, -]); -const RuleCreatePropsBatch2 = z.discriminatedUnion('type', [ SavedQueryRuleCreateProps, ThresholdRuleCreateProps, -]); -const RuleCreatePropsBatch3 = z.discriminatedUnion('type', [ ThreatMatchRuleCreateProps, MachineLearningRuleCreateProps, -]); -const RuleCreatePropsBatch4 = z.discriminatedUnion('type', [ NewTermsRuleCreateProps, EsqlRuleCreateProps, ]); -export type RuleCreateProps = z.infer< - | typeof RuleCreatePropsBatch1 - | typeof RuleCreatePropsBatch2 - | typeof RuleCreatePropsBatch3 - | typeof RuleCreatePropsBatch4 ->; -export const RuleCreateProps = z.union([ - RuleCreatePropsBatch1, - RuleCreatePropsBatch2, - RuleCreatePropsBatch3, - RuleCreatePropsBatch4, -]) as z.ZodType; - -const RuleUpdatePropsBatch1 = z.discriminatedUnion('type', [ + +export type RuleCreateProps = z.infer; +export const RuleCreateProps = z.discriminatedUnion('type', [ + EqlRuleCreateProps, + QueryRuleCreateProps, + SavedQueryRuleCreateProps, + ThresholdRuleCreateProps, + ThreatMatchRuleCreateProps, + MachineLearningRuleCreateProps, + NewTermsRuleCreateProps, + EsqlRuleCreateProps, +]) as z.ZodType as z.ZodType; + +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const RuleUpdatePropsInternal = z.discriminatedUnion('type', [ EqlRuleUpdateProps, QueryRuleUpdateProps, -]); -const RuleUpdatePropsBatch2 = z.discriminatedUnion('type', [ SavedQueryRuleUpdateProps, ThresholdRuleUpdateProps, -]); -const RuleUpdatePropsBatch3 = z.discriminatedUnion('type', [ ThreatMatchRuleUpdateProps, MachineLearningRuleUpdateProps, + NewTermsRuleUpdateProps, + EsqlRuleUpdateProps, ]); -const RuleUpdatePropsBatch4 = z.discriminatedUnion('type', [ + +export type RuleUpdateProps = z.infer; +export const RuleUpdateProps = z.discriminatedUnion('type', [ + EqlRuleUpdateProps, + QueryRuleUpdateProps, + SavedQueryRuleUpdateProps, + ThresholdRuleUpdateProps, + ThreatMatchRuleUpdateProps, + MachineLearningRuleUpdateProps, NewTermsRuleUpdateProps, EsqlRuleUpdateProps, +]) as z.ZodType as z.ZodType; + +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const RulePatchPropsInternal = z.union([ + EqlRulePatchProps, + QueryRulePatchProps, + SavedQueryRulePatchProps, + ThresholdRulePatchProps, + ThreatMatchRulePatchProps, + MachineLearningRulePatchProps, + NewTermsRulePatchProps, + EsqlRulePatchProps, ]); -export type RuleUpdateProps = z.infer< - | typeof RuleUpdatePropsBatch1 - | typeof RuleUpdatePropsBatch2 - | typeof RuleUpdatePropsBatch3 - | typeof RuleUpdatePropsBatch4 ->; -export const RuleUpdateProps = z.union([ - RuleUpdatePropsBatch1, - RuleUpdatePropsBatch2, - RuleUpdatePropsBatch3, - RuleUpdatePropsBatch4, -]) as z.ZodType; - -const RulePatchPropsBatch1 = z.union([EqlRulePatchProps, QueryRulePatchProps]); -const RulePatchPropsBatch2 = z.union([SavedQueryRulePatchProps, ThresholdRulePatchProps]); -const RulePatchPropsBatch3 = z.union([ThreatMatchRulePatchProps, MachineLearningRulePatchProps]); -const RulePatchPropsBatch4 = z.union([NewTermsRulePatchProps, EsqlRulePatchProps]); -export type RulePatchProps = z.infer< - | typeof RulePatchPropsBatch1 - | typeof RulePatchPropsBatch2 - | typeof RulePatchPropsBatch3 - | typeof RulePatchPropsBatch4 ->; + +export type RulePatchProps = z.infer; export const RulePatchProps = z.union([ - RulePatchPropsBatch1, - RulePatchPropsBatch2, - RulePatchPropsBatch3, - RulePatchPropsBatch4, -]) as z.ZodType; - -const RuleResponseBatch1 = z.discriminatedUnion('type', [ThresholdRule, EqlRule]); -const RuleResponseBatch2 = z.discriminatedUnion('type', [QueryRule, SavedQueryRule]); -const RuleResponseBatch3 = z.discriminatedUnion('type', [ThreatMatchRule, MachineLearningRule]); -const RuleResponseBatch4 = z.discriminatedUnion('type', [NewTermsRule, EsqlRule]); -export type RuleResponse = z.infer< - | typeof RuleResponseBatch1 - | typeof RuleResponseBatch2 - | typeof RuleResponseBatch3 - | typeof RuleResponseBatch4 ->; -export const RuleResponse = z.union([ - RuleResponseBatch1, - RuleResponseBatch2, - RuleResponseBatch3, - RuleResponseBatch4, -]) as z.ZodType; + EqlRulePatchProps, + QueryRulePatchProps, + SavedQueryRulePatchProps, + ThresholdRulePatchProps, + ThreatMatchRulePatchProps, + MachineLearningRulePatchProps, + NewTermsRulePatchProps, + EsqlRulePatchProps, +]) as z.ZodType as z.ZodType; + +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const RuleResponseInternal = z.discriminatedUnion('type', [ + ThresholdRule, + EqlRule, + QueryRule, + SavedQueryRule, + ThreatMatchRule, + MachineLearningRule, + NewTermsRule, + EsqlRule, +]); + +export type RuleResponse = z.infer; +export const RuleResponse = z.discriminatedUnion('type', [ + ThresholdRule, + EqlRule, + QueryRule, + SavedQueryRule, + ThreatMatchRule, + MachineLearningRule, + NewTermsRule, + EsqlRule, +]) as z.ZodType as z.ZodType; diff --git a/x-pack/plugins/security_solution/common/api/detection_engine/rule_management/bulk_actions/bulk_actions_route.gen.ts b/x-pack/plugins/security_solution/common/api/detection_engine/rule_management/bulk_actions/bulk_actions_route.gen.ts index 19f8cc3724736..2e1f65048a9bc 100644 --- a/x-pack/plugins/security_solution/common/api/detection_engine/rule_management/bulk_actions/bulk_actions_route.gen.ts +++ b/x-pack/plugins/security_solution/common/api/detection_engine/rule_management/bulk_actions/bulk_actions_route.gen.ts @@ -284,28 +284,26 @@ export const BulkActionEditPayloadTimeline = z.object({ }), }); -const BulkActionEditPayloadBatch1 = z.union([ +// We need this type to infer from it below, but in the end we want to export a casted Type +// error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. +const BulkActionEditPayloadInternal = z.union([ BulkActionEditPayloadTags, BulkActionEditPayloadIndexPatterns, -]); -const BulkActionEditPayloadBatch2 = z.union([ BulkActionEditPayloadInvestigationFields, BulkActionEditPayloadTimeline, -]); -const BulkActionEditPayloadBatch3 = z.union([ BulkActionEditPayloadRuleActions, BulkActionEditPayloadSchedule, ]); -export type BulkActionEditPayload = z.infer< - | typeof BulkActionEditPayloadBatch1 - | typeof BulkActionEditPayloadBatch2 - | typeof BulkActionEditPayloadBatch3 ->; + +export type BulkActionEditPayload = z.infer; export const BulkActionEditPayload = z.union([ - BulkActionEditPayloadBatch1, - BulkActionEditPayloadBatch2, - BulkActionEditPayloadBatch3, -]) as z.ZodType; + BulkActionEditPayloadTags, + BulkActionEditPayloadIndexPatterns, + BulkActionEditPayloadInvestigationFields, + BulkActionEditPayloadTimeline, + BulkActionEditPayloadRuleActions, + BulkActionEditPayloadSchedule, +]) as z.ZodType as z.ZodType; export type BulkEditRules = z.infer; export const BulkEditRules = BulkActionBase.merge(