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 f02f295954465..8df6d4c1fe2c2 100644 --- a/packages/kbn-openapi-generator/src/template_service/register_helpers.ts +++ b/packages/kbn-openapi-generator/src/template_service/register_helpers.ts @@ -87,4 +87,12 @@ export function registerHelpers(handlebarsInstance: typeof Handlebars) { return circularRefs.has(`#/components/schemas/${schemaName}`); } ); + + /** + * Large Zod schemas might lead to TypeScript type serialization limit. The hotfix applies type hinting for all schemas using `anyOf` or `oneOf`. + */ + handlebarsInstance.registerHelper( + 'shouldCastExplicitly', + ({ anyOf, oneOf }) => anyOf?.length > 2 || oneOf?.length > 2 + ); } 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 fa750c7d1f8ab..80ca766585c03 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,8 +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}} +{{#if (shouldCastExplicitly this)}} +{{!-- We need this temporary type to infer from it below, but in the end we want to export as a casted {{@key}} 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}} = {{@key}}Internal as z.ZodType<{{@key}}>; +{{else}} export type {{@key}} = z.infer; export const {{@key}} = {{> zod_schema_item}}; +{{/if }} {{/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 d7a0e54487783..2678831db3d60 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 @@ -59,15 +59,18 @@ export const ExceptionListItemEntryExists = z.object({ operator: ExceptionListItemEntryOperator, }); -export type ExceptionListItemEntryNestedEntryItem = z.infer< - typeof ExceptionListItemEntryNestedEntryItem ->; -export const ExceptionListItemEntryNestedEntryItem = z.union([ +const ExceptionListItemEntryNestedEntryItemInternal = z.union([ ExceptionListItemEntryMatch, ExceptionListItemEntryMatchAny, ExceptionListItemEntryExists, ]); +export type ExceptionListItemEntryNestedEntryItem = z.infer< + typeof ExceptionListItemEntryNestedEntryItemInternal +>; +export const ExceptionListItemEntryNestedEntryItem = + ExceptionListItemEntryNestedEntryItemInternal as z.ZodType; + export type ExceptionListItemEntryNested = z.infer; export const ExceptionListItemEntryNested = z.object({ type: z.literal('nested'), @@ -85,8 +88,7 @@ export const ExceptionListItemEntryMatchWildcard = z.object({ operator: ExceptionListItemEntryOperator, }); -export type ExceptionListItemEntry = z.infer; -export const ExceptionListItemEntry = z.discriminatedUnion('type', [ +const ExceptionListItemEntryInternal = z.discriminatedUnion('type', [ ExceptionListItemEntryMatch, ExceptionListItemEntryMatchAny, ExceptionListItemEntryList, @@ -95,5 +97,9 @@ export const ExceptionListItemEntry = z.discriminatedUnion('type', [ ExceptionListItemEntryMatchWildcard, ]); +export type ExceptionListItemEntry = z.infer; +export const ExceptionListItemEntry = + ExceptionListItemEntryInternal as z.ZodType; + export type ExceptionListItemEntryArray = z.infer; export const ExceptionListItemEntryArray = z.array(ExceptionListItemEntry); 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 972c0ce519044..c207f86639b73 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 @@ -597,8 +597,7 @@ export const EsqlRuleUpdateProps = SharedUpdateProps.merge(EsqlRuleCreateFields) export type EsqlRulePatchProps = z.infer; export const EsqlRulePatchProps = SharedPatchProps.merge(EsqlRulePatchFields.partial()); -export type TypeSpecificCreateProps = z.infer; -export const TypeSpecificCreateProps = z.discriminatedUnion('type', [ +const TypeSpecificCreatePropsInternal = z.discriminatedUnion('type', [ EqlRuleCreateFields, QueryRuleCreateFields, SavedQueryRuleCreateFields, @@ -609,8 +608,11 @@ export const TypeSpecificCreateProps = z.discriminatedUnion('type', [ EsqlRuleCreateFields, ]); -export type TypeSpecificPatchProps = z.infer; -export const TypeSpecificPatchProps = z.union([ +export type TypeSpecificCreateProps = z.infer; +export const TypeSpecificCreateProps = + TypeSpecificCreatePropsInternal as z.ZodType; + +const TypeSpecificPatchPropsInternal = z.union([ EqlRulePatchFields, QueryRulePatchFields, SavedQueryRulePatchFields, @@ -621,8 +623,11 @@ export const TypeSpecificPatchProps = z.union([ EsqlRulePatchFields, ]); -export type TypeSpecificResponse = z.infer; -export const TypeSpecificResponse = z.discriminatedUnion('type', [ +export type TypeSpecificPatchProps = z.infer; +export const TypeSpecificPatchProps = + TypeSpecificPatchPropsInternal as z.ZodType; + +const TypeSpecificResponseInternal = z.discriminatedUnion('type', [ EqlRuleResponseFields, QueryRuleResponseFields, SavedQueryRuleResponseFields, @@ -633,8 +638,10 @@ export const TypeSpecificResponse = z.discriminatedUnion('type', [ EsqlRuleResponseFields, ]); -export type RuleCreateProps = z.infer; -export const RuleCreateProps = z.discriminatedUnion('type', [ +export type TypeSpecificResponse = z.infer; +export const TypeSpecificResponse = TypeSpecificResponseInternal as z.ZodType; + +const RuleCreatePropsInternal = z.discriminatedUnion('type', [ EqlRuleCreateProps, QueryRuleCreateProps, SavedQueryRuleCreateProps, @@ -645,8 +652,10 @@ export const RuleCreateProps = z.discriminatedUnion('type', [ EsqlRuleCreateProps, ]); -export type RuleUpdateProps = z.infer; -export const RuleUpdateProps = z.discriminatedUnion('type', [ +export type RuleCreateProps = z.infer; +export const RuleCreateProps = RuleCreatePropsInternal as z.ZodType; + +const RuleUpdatePropsInternal = z.discriminatedUnion('type', [ EqlRuleUpdateProps, QueryRuleUpdateProps, SavedQueryRuleUpdateProps, @@ -657,8 +666,10 @@ export const RuleUpdateProps = z.discriminatedUnion('type', [ EsqlRuleUpdateProps, ]); -export type RulePatchProps = z.infer; -export const RulePatchProps = z.union([ +export type RuleUpdateProps = z.infer; +export const RuleUpdateProps = RuleUpdatePropsInternal as z.ZodType; + +const RulePatchPropsInternal = z.union([ EqlRulePatchProps, QueryRulePatchProps, SavedQueryRulePatchProps, @@ -669,8 +680,10 @@ export const RulePatchProps = z.union([ EsqlRulePatchProps, ]); -export type RuleResponse = z.infer; -export const RuleResponse = z.discriminatedUnion('type', [ +export type RulePatchProps = z.infer; +export const RulePatchProps = RulePatchPropsInternal as z.ZodType; + +const RuleResponseInternal = z.discriminatedUnion('type', [ EqlRule, QueryRule, SavedQueryRule, @@ -680,3 +693,6 @@ export const RuleResponse = z.discriminatedUnion('type', [ NewTermsRule, EsqlRule, ]); + +export type RuleResponse = z.infer; +export const RuleResponse = RuleResponseInternal 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 cf8d75576564d..52a52442df42f 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,8 +284,7 @@ export const BulkActionEditPayloadTimeline = z.object({ }), }); -export type BulkActionEditPayload = z.infer; -export const BulkActionEditPayload = z.union([ +const BulkActionEditPayloadInternal = z.union([ BulkActionEditPayloadTags, BulkActionEditPayloadIndexPatterns, BulkActionEditPayloadInvestigationFields, @@ -294,6 +293,10 @@ export const BulkActionEditPayload = z.union([ BulkActionEditPayloadSchedule, ]); +export type BulkActionEditPayload = z.infer; +export const BulkActionEditPayload = + BulkActionEditPayloadInternal as z.ZodType; + export type BulkEditRules = z.infer; export const BulkEditRules = BulkActionBase.merge( z.object({