elastic / kibana

Your window into the Elastic Stack
https://www.elastic.co/products/kibana
Other
19.35k stars 7.98k forks source link

[SecuritySolution] OpenAPI generator generates invalid TS source code depending on components schemas order #182928

Open machadoum opened 1 week ago

machadoum commented 1 week ago

The order in which the components are declared might cause TS errors when we use $ref.

Current behavior:

The following yaml:

openapi: 3.0.0
info:
  title: Test components schema Order
  version: '2023-10-31'
paths: {}
components:
  schemas:
    IdOrUndefined:
      $ref: '#/components/schemas/Id'
      nullable: true
    Id:
      type: string

Generates the following code:

export type IdOrUndefined = z.infer<typeof IdOrUndefined>;
export const IdOrUndefined = Id.nullable();

export type Id = z.infer<typeof Id>;
export const Id = z.string();

Which contains the following error:

Block-scoped variable 'Id' used before its declaration.ts(2448)
myTest.gen.ts(23, 14): 'Id' is declared here.
Variable 'Id' is used before being assigned.ts(2454)
const Id: z.ZodString

Expected behavior: It should generate valid TS code for any order in which components are declared.

elasticmachine commented 1 week ago

Pinging @elastic/security-solution (Team: SecuritySolution)

elasticmachine commented 1 week ago

Pinging @elastic/security-detection-rule-management (Team:Detection Rule Management)