-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Security Solution] Initial OpenAPI codegen implementation #163186
Conversation
9ed0d08
to
9a1b4c9
Compare
Pinging @elastic/security-detections-response (Team:Detections and Resp) |
Pinging @elastic/security-solution (Team: SecuritySolution) |
💛 Build succeeded, but was flaky
Failed CI StepsTest Failures
Metrics [docs]Unknown metric groupsESLint disabled in files
ESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: cc @xcrzx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fantastic! I'm approving it now so we can more easily get this in front of other teams who may want to start experimenting with the code generation. I've listed some some important enhancements below that we should also aim to make in the near future in (IMO) priority order.
- Automatically create
index.ts
files and add generated schema files to thoseindex.ts
files. A first implementation of this could simply be a top-levelindex.ts
file that exports directly from each individual generated schema file. The index file is important to isolate the consumers of API schemas from the internal/api
folder structure in case we decide the domain structure should be changed.
A downside of a mega-index file like this that I encountered while moving schemas into /common/api
is that the public
code does not tree-shake, so public
code that imports from an index.ts
file that contains many unnecessary dependencies can vastly increase the bundle size. (this commit was added just to remove @kbn/config-schema
as a dependency from the timeline schemas so that public
code could import from /common/api/timeline
without importing @kbn/config-schema
and adding 840KB to the chunk size).
If we only import type
from /common/api
into public
then perhaps a mega-index wouldn't be an issue. Alternatively, an index.ts
file per top-level domain could be nice and avoid name clashes between domains.
- It would be great to move the generation script to a package so we can easily reuse it in the
lists
plugin immediately and other plugins in the future. - Run the code generation script and apply any changes in CI so the generated code is guaranteed to stay in sync with the yaml files. The linter auto-committing changes in CI is a good model to follow here. In theory we can add a line here to call a script like this ts_projects script which could then call the
generate
script you built and commit any files that were changed after running the generate script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the overview with the team and for taking the time to answer questions today @xcrzx. LGTM! 👍 🚀 So awesome that we're finally here with codegen!! 🙌
Resolves: https://github.com/elastic/security-team/issues/7134
Summary
Implemented request and response schema generation from OpenAPI specifications.
The code generator script scans the
x-pack/plugins/security_solution/common/api
directory, locates all*.schema.yaml
files, and generates a corresponding*.gen.ts
artifact for each, containingzod
schema definitions.Right now, all generation sources are set to
x-codegen-enabled: false
to prevent the creation of duplicate schemas. Maintaining the oldio-ts
schemas alongside the newzod
ones could potentially lead to confusion among developers. Thus, the recommended migration strategy is to incrementally replace old schema usages with new ones, subsequently removing outdated ones. I'll be implementing this approach in the upcoming PRs.How to use the generator
If you need to test the generator locally, enable several sources and run the generator script to see the results.
Navigate to
x-pack/plugins/security_solution
and runyarn openapi:generate
Important note: if you want to enable route schemas, ensure you also enable all their dependencies, such as common schemas. Failing to do so will result in the generated code importing non-existent files.
Example
Input file (
x-pack/plugins/security_solution/common/api/detection_engine/model/error_schema.schema.yaml
):Generated output file (
x-pack/plugins/security_solution/common/api/detection_engine/model/error_schema.gen.ts
):