[OAS] Support examples (and more)#212495
Conversation
src/platform/packages/shared/kbn-router-to-openapispec/src/merge_operation_with_schema_file.ts
Outdated
Show resolved
Hide resolved
| } | ||
|
|
||
| export const generateOpenApiDocument = ( | ||
| export const generateOpenApiDocument = async ( |
There was a problem hiding this comment.
Needed to touch other files bc of async poisoning
...ck/platform/plugins/shared/alerting/server/routes/rule/apis/create/create_rule.examples.yaml
Outdated
Show resolved
Hide resolved
x-pack/platform/plugins/shared/alerting/server/routes/rule/apis/create/create_rule_route.ts
Outdated
Show resolved
Hide resolved
|
/ci |
|
/ci |
…t --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules --include-path /api/actions --include-path /api/security/role --include-path /api/spaces --include-path /api/fleet --include-path /api/dashboards --update'
|
/ci |
|
/ci |
|
@jloleysens I know I'm late to this, but did you think about specifying the examples in the typescript code instead of a separate yaml file? This way it could be type-checked automatically |
|
Pinging @elastic/kibana-core (Team:Core) |
|
Pinging @elastic/response-ops (Team:ResponseOps) |
afharo
left a comment
There was a problem hiding this comment.
LGTM! I just added a question regarding the existence of any helpers (or guide) to create a template to the example files. Since they are manual, I'd like to be able to make it a bit more "guided" the part of providing all the fields to fill.
| @@ -0,0 +1,31 @@ | |||
| # Because of where this file is linked to the code you are in | |||
There was a problem hiding this comment.
Q: Is there a way to create the template of these files via a command?
I worry that documenting examples might be too complex that ends up not being used. WDYT?
There was a problem hiding this comment.
Yeah, that's a good question. I'll expand our existing tutorial to guide devs through this. Perhaps some tooling could come later!
lcawl
left a comment
There was a problem hiding this comment.
Docs output LGTM! Thanks for removing the need to accomplish this via overlays, which were much more fragile
There was a problem hiding this comment.
We are in the process of slowly moving the schema of the parameters of each rule type in the @kbn/response-ops-rule-params (src/platform/packages/shared/response-ops/rule_params). The route uses the createBodySchema which in turn imports the schemas from the package (x-pack/platform/plugins/shared/alerting/common/routes/rule/apis/create/schemas/v1.ts). Should we also put the examples in the package (each rule type folder has its example.yml)?
|
@cnasikas , colocating the examples with the most relevant code makes sense... In your case I'm not sure whether that is route (I guess it contains some business logic that inform the examples) or the actual schemas... I'd lean toward colocating with schemas. Would that work with your workflow? |
|
Thanks @jloleysens. Yes, I also lean toward colocating with the schemas (as described in my previous comment), if you don't mind. |
03388fa to
7d3bced
Compare
…t --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules --include-path /api/actions --include-path /api/security/role --include-path /api/spaces --include-path /api/fleet --include-path /api/dashboards --update'
There was a problem hiding this comment.
bundle*.json and output/*yaml snapshot files here are the source of most newlines.
| of your API. See the [bump.sh documentation](https://docs.bump.sh/guides/openapi/specification/v3.1/data-models/examples/) | ||
| for more information on how examples will be shown to end users. | ||
|
|
||
| To add examples to the endpoint we created above you could do the following: |
There was a problem hiding this comment.
Recommending the code-first approach to keep consistent with the rest of the platform provided approach to OAS.
| 'validate' | 'options' | ||
| > & { | ||
| options?: Omit< | ||
| options?: Pick< |
There was a problem hiding this comment.
Not needed for this PR, but converted this type subset selection from blocklist to allowlist for stricter control of the interface we are exposing.
| title: Overlays for the alerting rule examples and parameters | ||
| version: 0.0.1 | ||
| actions: | ||
| # Add some alerting API examples |
There was a problem hiding this comment.
Moved closer to respective ResponseOps code
💚 Build Succeeded
Metrics [docs]Public APIs missing comments
Unknown metric groupsAPI count
History
cc @jloleysens |
## Summary Close elastic#188926 Adds the ability to merge in an arbitrary OAS operation object (search [spec](https://swagger.io/specification/) for "operation object") in code generated OAS. This enables us to write examples next to the code: <img width="306" alt="Screenshot 2025-02-26 at 11 24 00" src="https://github.com/user-attachments/assets/5df92ae9-c8c5-4797-8b7c-764e937e84e9" /> ```ts // Inside the router declaration router.post( { path: `${BASE_ALERTING_API_PATH}/rule/{id?}`, security: DEFAULT_ALERTING_ROUTE_SECURITY, options: { access: 'public', summary: `Create a rule`, tags: ['oas-tag:alerting'], // NEW 👇🏻: this file can contain operation-level OAS and will merge/override anything that we auto generate oasOperationObject: () => path.join(__dirname, 'create_rule.examples.yaml'), // ☝🏻 can also be a TS-checked OpenAPIV3.OperationObject ``` Tested with create rule example overlay ([gist](https://gist.github.com/jloleysens/dc643522a3f58dc2aed3dcef966b42df)) and bump <img width="1236" alt="Screenshot 2025-02-26 at 11 45 57" src="https://github.com/user-attachments/assets/c21b466a-ddab-49ce-b4ba-a04fd0e6c1b7" /> ## Docs Added developer guide docs to https://docs.elastic.dev/kibana-dev-docs/genereating-oas-for-http-apis#2-route-definitions <details> <summary>images</summary> <img width="799" alt="Screenshot 2025-03-13 at 13 02 31" src="https://github.com/user-attachments/assets/e89b2c5a-1984-4672-a40b-b492581e690f" /> <img width="819" alt="Screenshot 2025-03-13 at 13 02 39" src="https://github.com/user-attachments/assets/1375a25a-4d91-46b4-8ce5-42c763657d96" /> </details> ### TO DO - [x] Document the feature in TS - [x] Document feature in dev docs - [x] Add tests --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
## Summary Close elastic#188926 Adds the ability to merge in an arbitrary OAS operation object (search [spec](https://swagger.io/specification/) for "operation object") in code generated OAS. This enables us to write examples next to the code: <img width="306" alt="Screenshot 2025-02-26 at 11 24 00" src="https://github.com/user-attachments/assets/5df92ae9-c8c5-4797-8b7c-764e937e84e9" /> ```ts // Inside the router declaration router.post( { path: `${BASE_ALERTING_API_PATH}/rule/{id?}`, security: DEFAULT_ALERTING_ROUTE_SECURITY, options: { access: 'public', summary: `Create a rule`, tags: ['oas-tag:alerting'], // NEW 👇🏻: this file can contain operation-level OAS and will merge/override anything that we auto generate oasOperationObject: () => path.join(__dirname, 'create_rule.examples.yaml'), // ☝🏻 can also be a TS-checked OpenAPIV3.OperationObject ``` Tested with create rule example overlay ([gist](https://gist.github.com/jloleysens/dc643522a3f58dc2aed3dcef966b42df)) and bump <img width="1236" alt="Screenshot 2025-02-26 at 11 45 57" src="https://github.com/user-attachments/assets/c21b466a-ddab-49ce-b4ba-a04fd0e6c1b7" /> ## Docs Added developer guide docs to https://docs.elastic.dev/kibana-dev-docs/genereating-oas-for-http-apis#2-route-definitions <details> <summary>images</summary> <img width="799" alt="Screenshot 2025-03-13 at 13 02 31" src="https://github.com/user-attachments/assets/e89b2c5a-1984-4672-a40b-b492581e690f" /> <img width="819" alt="Screenshot 2025-03-13 at 13 02 39" src="https://github.com/user-attachments/assets/1375a25a-4d91-46b4-8ce5-42c763657d96" /> </details> ### TO DO - [x] Document the feature in TS - [x] Document feature in dev docs - [x] Add tests --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Summary
Close #188926
Adds the ability to merge in an arbitrary OAS operation object (search spec for "operation object") in code generated OAS.
This enables us to write examples next to the code:
Tested with create rule example overlay (gist) and bump
Docs
Added developer guide docs to https://docs.elastic.dev/kibana-dev-docs/genereating-oas-for-http-apis#2-route-definitions
images
TO DO