mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
[Security Solution] Fix generation of circular types using non-circular types (#187061)
**Relates to:** https://github.com/elastic/kibana/issues/186066, https://github.com/elastic/kibana/pull/186221 ## Summary This PR fixes generated TS files for circular OpenAPI schemas when non circular (internal or external) schema is used. ## Details https://github.com/elastic/kibana/pull/186221 added code generation support for circular schemas. Such schemas have input TS types generated which may depend on the other circular or non circular TS types. The problem appears when a circular schema uses a non circular schema. Generated code expects an input type for used schemas exist but it's not a case non circular schemas. Let's consider a following OpenAPI spec with a self circular schema and a field referencing `NonEmptyString` schema ```yaml ... components: x-codegen-enabled: true schemas: SelfCircular: type: object properties: circularField: $ref: '#/components/schemas/SelfCircular' stringField: $ref: '../model/primitives.schema.yaml#/components/schemas/NonEmptyString' ``` where a generated TS file looks like ```ts import type { ZodTypeDef } from 'zod'; import { z } from 'zod'; import { NonEmptyString } from '../model/primitives.gen'; export interface SelfCircular { circularField?: SelfCircular; stringField?: NonEmptyString; } export interface SelfCircularInput { circularField?: SelfCircularInput; stringField?: NonEmptyStringInput; } export const SelfCircular: z.ZodType<SelfCircular, ZodTypeDef, SelfCircularInput> = z.object({ circularField: z.lazy(() => SelfCircular).optional(), stringField: NonEmptyString.optional(), }); ``` You can notice the generated TS file contains usage of `NonEmptyStringInput` which doesn't exist. **After applying the fix the generated TS file looks like** ```ts import type { ZodTypeDef } from 'zod'; import { z } from 'zod'; import { NonEmptyString } from '../model/primitives.gen'; export interface SelfCircular { circularField?: SelfCircular; stringField?: NonEmptyString; } export interface SelfCircularInput { circularField?: SelfCircularInput; stringField?: NonEmptyString; } export const SelfCircular: z.ZodType<SelfCircular, ZodTypeDef, SelfCircularInput> = z.object({ circularField: z.lazy(() => SelfCircular).optional(), stringField: NonEmptyString.optional(), }); ```
This commit is contained in:
parent
ee80b740fa
commit
a7cea13300
1 changed files with 2 additions and 1 deletions
|
@ -3,7 +3,8 @@
|
|||
{{~/if~}}
|
||||
|
||||
{{~#if $ref~}}
|
||||
{{referenceName}}Input
|
||||
{{referenceName}}
|
||||
{{~#if (isCircularRef $ref)}}Input{{/if~}}
|
||||
{{~#if nullable}} | null {{/if~}}
|
||||
{{~/if~}}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue