-
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
Automated OpenAPI spec generation #82587
Comments
Pinging @elastic/kibana-platform (Team:Platform) |
Just brainstorming here, haven't given this a lot of thought, but could we generate source code from the OpenAPI spec instead of generating OpenAPI spec from the source code? If you "compile" the spec it would generate a |
Would need to think more about it, but this seems to be a viable option yes. Seems technically way easier (not easy, easier) than the other way around. |
I think in general it is preferable to keep OpenAPI definitions as your source of truth. This makes the solution language agnostic and allows to generate implementations in multiple languages. There are projects like this though - https://tsoa-community.github.io/docs/getting-started.html#defining-our-first-model |
I think deciding the source of truth and direction is as much a team/workflow decision as a technical one. That said, my preference is to use JSON Schema & OpenAPI to spec shape & behavior and then generate TS types, JS clients, and servers from that. What @rudolf describes is essentially the plan for Ingest Manager but ours is currently more manual/gradual Ingest Manager OpenAPI overview
There are many options for generating clients. Many (most?) allow you to choose a transport (nodejs |
This is great -- thanks for detailing this out @pgayvallet! Just want to share this issue we created over on the Security Detections side that captures what an initial PoC could look like from our perspective: #81964 |
I was mostly working with Java/Maven generator, but in any case supplying your own templates is pretty easy, main concern is who would maintain those templates. |
FYI while generating specifications for a handful of Kibana APIs (e.g. cases, ML), it became clear that there need to be two specs for each Kibana API due to the optional space ID parameter in the path. Per https://swagger.io/docs/specification/describing-parameters/, "In OpenAPI, a path parameter is defined using in: path. ...Also remember to add required: true, because path parameters are always required...". Therefore to be correct, we need to have two identical specifications for each endpoint--one with and one without the optional For the purposes of the documentation generated from these specifications, however, I'm leaning toward only including the path that contains the space ID. Otherwise, we're ending up with two pages for each API, where the only difference is one path parameter. If you have questions or feedback, let me know! |
@lcawl that sounds like a pragmatic solution and we could always document if that space id is left out Kibana will use the default space. |
Has there been any progress on this? Currently, @lcawl is manually creating open API specs for the response ops team's HTTP APIs, and the response ops team is having to manually maintain these. This manual process is laborious, error prone, and a significant amount of work. Any automation that could be provided would be incredibly beneficial. |
No, it hasn't be prioritized, and I doubt the team will be able to take the time to find a proper solution anytime soon given our recent change of focus. |
There's been several proposals to try to prioritise this as part of a larger effort like Kibana Persistence Toolkit and Backward Compatible API Architecture but we failed to get buy-in on these. I would really like to see us invest in this and it can create a lot of value with the versioned API layer required for serverless. At a high-level it feels like this space is still somewhat immature so I suspect we'd need a guinea pig team to be an early adopter and find all the kinks. There's a helpful analysis of the landscape here https://blog.simonireilly.com/posts/typescript-openapi On the two sides of the spectrum:
I would lean towards (2) and it feels like something we could incrementally adopt by e.g. starting to just get types for validation schemas as that might cover the biggest surface of where the spec and code starts diverging. |
Linking to @jloleysens space time PoC #156357 |
Another PoC exploring possibilities of generating runtime schemas, API routes implementation, and API client from OpenAPI spec: #157477 |
For reference, you can follow our progress on all tasks related to migrating to the OpenAPI specification on this meta issue: https://github.com/elastic/security-team/issues/6726. |
@jloleysens can you please update? |
With all the work in #180056 completed, this is considered done. |
This issue is here to discuss about the feasibility and the possible technical solutions to generate openAPI specs for Kibana endpoints.
Due to the language we are using, this is far from trivial, and way more difficult than it would be done in a compiled language with proper reflection support.
I won't include the global metadata, common types and such in this discussion, as these are more or less constants and do not (I think) represent any technical challenge.
Just for the record, this is what an OpenAPI
path
spec looks likeTo simplify and focus on the real challenge, we would need, for each defined endpoint, to be able to retrieve, and convert to OpenAPI format:
Possible solutions
Retrieve the information at runtime
One option would be to have a script that starts Kibana, let all plugins registered their handlers, and then work from the routes that have been registered to core's
httpService
. This would require to start Kibana in a 'sandboxed' mode that would stop aftersetup
, and collect the routes from core, and then work from there.The path, method and authentication mode of the endpoint
These would be directly accessible from the route's config
The type of the parameters
We would have access to
body
,params
andquery
's validation schemas. We would 'just' have to be able to interpret and convert them to OA format at runtime. Of course, this is not something that is currently doable, and would requires to expose metadata on all ofkbn/config-schema
types to be able to perform reflect-ish operations on the schema.The type of the response payload
I don't think there is any way to retrieve this information using this approach.
Use AST to parse and retrieve the informations
The second option would be to use AST to parse the code, find all calls to
Router#get|post|XXX
, and work from there (which is already some heavy parsing work tbh). If it's probably more powerful, this also sounds like colossal work to handle.The path, method and authentication mode of the endpoint
We should be able to parse the route's configuration and retrieve these infos from there. Note that we would need to be able to resolve variables to scenarios such as
path: prefix + "/endpoint"
.The type of the parameters
We should be able to retrieve the validation schema nodes, and then use
typeChecker.getTypeAtLocation
to retrieve the schema type. From there, we would need to retrieve the schema's result type, and then convert it to OpenAPI format.The type of the response payload
We should be able to find calls to
res.ok
and usetypeChecker.getTypeAtLocation
to infer the returned type. We would then need to convert the TS type to openAPI format. I see that https://github.com/grantila/ts-to-openapi exists, so it should probably be doable ourselves (or finding a more used/adopted lib)Let the endpoints owners specify most of the OpenAPI metadata.
This is what is done by https://www.npmjs.com/package/express-openapi for example
Not sure this has real value compared to just have solutions teams manually maintain their openAPI spec outside of the raw code.
The text was updated successfully, but these errors were encountered: