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

[EDR Workflows] Explicitly export type in open api autogenerated ZOD types #191830

Merged
merged 9 commits into from
Sep 3, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@ export function registerHelpers(handlebarsInstance: typeof Handlebars) {
return circularRefs.has(`#/components/schemas/${schemaName}`);
}
);

handlebarsInstance.registerHelper('shouldCastExplicitly', ({ anyOf }) => anyOf?.length > 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

oneOf looks like a valid case as well.

nit: Having a comment why casting explicitly is required would be nice here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added support to oneOf for the future 👍 currently no schemas are using the same way as anyOf, I think oneOf is used mostly in the requestBody.

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment reveals implementation details in each generated file. It doesn't look necessary here and may only confuse readers. I recommend to transform it to a handlebars comment or remove completely.

// 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<typeof {{@key}}Internal>;
export const {{@key}} = {{@key}}Internal as z.ZodType<{{@key}}>;
{{else}}
export type {{@key}} = z.infer<typeof {{@key}}>;
export const {{@key}} = {{> zod_schema_item}};
{{/if }}
{{/if}}
{{#if enum}}
{{#unless (isSingle enum)}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ export const ExceptionListItemEntryMatchWildcard = z.object({
operator: ExceptionListItemEntryOperator,
});

export type ExceptionListItemEntry = z.infer<typeof ExceptionListItemEntry>;
export const ExceptionListItemEntry = z.discriminatedUnion('type', [
// We need this temporary type to infer from it below, but in the end we want to export as a casted ExceptionListItemEntry 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,
ExceptionListItemEntryList,
Expand All @@ -95,5 +96,9 @@ export const ExceptionListItemEntry = z.discriminatedUnion('type', [
ExceptionListItemEntryMatchWildcard,
]);

export type ExceptionListItemEntry = z.infer<typeof ExceptionListItemEntryInternal>;
export const ExceptionListItemEntry =
ExceptionListItemEntryInternal as z.ZodType<ExceptionListItemEntry>;

export type ExceptionListItemEntryArray = z.infer<typeof ExceptionListItemEntryArray>;
export const ExceptionListItemEntryArray = z.array(ExceptionListItemEntry);
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ export const EsqlRuleUpdateProps = SharedUpdateProps.merge(EsqlRuleCreateFields)
export type EsqlRulePatchProps = z.infer<typeof EsqlRulePatchProps>;
export const EsqlRulePatchProps = SharedPatchProps.merge(EsqlRulePatchFields.partial());

export type TypeSpecificCreateProps = z.infer<typeof TypeSpecificCreateProps>;
export const TypeSpecificCreateProps = z.discriminatedUnion('type', [
// We need this temporary type to infer from it below, but in the end we want to export as a casted TypeSpecificCreateProps 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,
SavedQueryRuleCreateFields,
Expand All @@ -609,8 +610,13 @@ export const TypeSpecificCreateProps = z.discriminatedUnion('type', [
EsqlRuleCreateFields,
]);

export type TypeSpecificPatchProps = z.infer<typeof TypeSpecificPatchProps>;
export const TypeSpecificPatchProps = z.union([
export type TypeSpecificCreateProps = z.infer<typeof TypeSpecificCreatePropsInternal>;
export const TypeSpecificCreateProps =
TypeSpecificCreatePropsInternal as z.ZodType<TypeSpecificCreateProps>;

// We need this temporary type to infer from it below, but in the end we want to export as a casted TypeSpecificPatchProps 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,
Expand All @@ -621,8 +627,13 @@ export const TypeSpecificPatchProps = z.union([
EsqlRulePatchFields,
]);

export type TypeSpecificResponse = z.infer<typeof TypeSpecificResponse>;
export const TypeSpecificResponse = z.discriminatedUnion('type', [
export type TypeSpecificPatchProps = z.infer<typeof TypeSpecificPatchPropsInternal>;
export const TypeSpecificPatchProps =
TypeSpecificPatchPropsInternal as z.ZodType<TypeSpecificPatchProps>;

// We need this temporary type to infer from it below, but in the end we want to export as a casted TypeSpecificResponse 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,
SavedQueryRuleResponseFields,
Expand All @@ -633,8 +644,12 @@ export const TypeSpecificResponse = z.discriminatedUnion('type', [
EsqlRuleResponseFields,
]);

export type RuleCreateProps = z.infer<typeof RuleCreateProps>;
export const RuleCreateProps = z.discriminatedUnion('type', [
export type TypeSpecificResponse = z.infer<typeof TypeSpecificResponseInternal>;
export const TypeSpecificResponse = TypeSpecificResponseInternal as z.ZodType<TypeSpecificResponse>;

// We need this temporary type to infer from it below, but in the end we want to export as a casted RuleCreateProps 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,
SavedQueryRuleCreateProps,
Expand All @@ -645,8 +660,12 @@ export const RuleCreateProps = z.discriminatedUnion('type', [
EsqlRuleCreateProps,
]);

export type RuleUpdateProps = z.infer<typeof RuleUpdateProps>;
export const RuleUpdateProps = z.discriminatedUnion('type', [
export type RuleCreateProps = z.infer<typeof RuleCreatePropsInternal>;
export const RuleCreateProps = RuleCreatePropsInternal as z.ZodType<RuleCreateProps>;

// We need this temporary type to infer from it below, but in the end we want to export as a casted RuleUpdateProps 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,
SavedQueryRuleUpdateProps,
Expand All @@ -657,8 +676,12 @@ export const RuleUpdateProps = z.discriminatedUnion('type', [
EsqlRuleUpdateProps,
]);

export type RulePatchProps = z.infer<typeof RulePatchProps>;
export const RulePatchProps = z.union([
export type RuleUpdateProps = z.infer<typeof RuleUpdatePropsInternal>;
export const RuleUpdateProps = RuleUpdatePropsInternal as z.ZodType<RuleUpdateProps>;

// We need this temporary type to infer from it below, but in the end we want to export as a casted RulePatchProps 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,
Expand All @@ -669,8 +692,12 @@ export const RulePatchProps = z.union([
EsqlRulePatchProps,
]);

export type RuleResponse = z.infer<typeof RuleResponse>;
export const RuleResponse = z.discriminatedUnion('type', [
export type RulePatchProps = z.infer<typeof RulePatchPropsInternal>;
export const RulePatchProps = RulePatchPropsInternal as z.ZodType<RulePatchProps>;

// We need this temporary type to infer from it below, but in the end we want to export as a casted RuleResponse 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', [
EqlRule,
QueryRule,
SavedQueryRule,
Expand All @@ -680,3 +707,6 @@ export const RuleResponse = z.discriminatedUnion('type', [
NewTermsRule,
EsqlRule,
]);

export type RuleResponse = z.infer<typeof RuleResponseInternal>;
export const RuleResponse = RuleResponseInternal as z.ZodType<RuleResponse>;
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,9 @@ export const BulkActionEditPayloadTimeline = z.object({
}),
});

export type BulkActionEditPayload = z.infer<typeof BulkActionEditPayload>;
export const BulkActionEditPayload = z.union([
// We need this temporary type to infer from it below, but in the end we want to export as a casted BulkActionEditPayload 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,
BulkActionEditPayloadInvestigationFields,
Expand All @@ -294,6 +295,10 @@ export const BulkActionEditPayload = z.union([
BulkActionEditPayloadSchedule,
]);

export type BulkActionEditPayload = z.infer<typeof BulkActionEditPayloadInternal>;
export const BulkActionEditPayload =
BulkActionEditPayloadInternal as z.ZodType<BulkActionEditPayload>;

export type BulkEditRules = z.infer<typeof BulkEditRules>;
export const BulkEditRules = BulkActionBase.merge(
z.object({
Expand Down