Skip to content

[OAS] Support examples (and more)#212495

Merged
jloleysens merged 29 commits intoelastic:mainfrom
jloleysens:oas/support-examples
Mar 14, 2025
Merged

[OAS] Support examples (and more)#212495
jloleysens merged 29 commits intoelastic:mainfrom
jloleysens:oas/support-examples

Conversation

@jloleysens
Copy link
Contributor

@jloleysens jloleysens commented Feb 26, 2025

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:

Screenshot 2025-02-26 at 11 24 00
// 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) and bump

Screenshot 2025-02-26 at 11 45 57

Docs

Added developer guide docs to https://docs.elastic.dev/kibana-dev-docs/genereating-oas-for-http-apis#2-route-definitions

images Screenshot 2025-03-13 at 13 02 31 Screenshot 2025-03-13 at 13 02 39

TO DO

  • Document the feature in TS
  • Document feature in dev docs
  • Add tests

@jloleysens jloleysens added Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// release_note:skip Skip the PR/issue when compiling release notes backport:skip This PR does not require backporting Team:ResponseOps Platform ResponseOps team (formerly the Cases and Alerting teams) t// Feature:OAS Work or issues related to Core-provided mechanisms for generating OAS labels Feb 26, 2025
@jloleysens jloleysens requested a review from lcawl February 26, 2025 10:26
}

export const generateOpenApiDocument = (
export const generateOpenApiDocument = async (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to touch other files bc of async poisoning

@jloleysens jloleysens changed the title [OAS] Support examples [OAS] Support examples (and more) Feb 26, 2025
@jloleysens
Copy link
Contributor Author

/ci

@jloleysens
Copy link
Contributor Author

/ci

kibanamachine and others added 2 commits February 27, 2025 11:19
…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'
@jloleysens
Copy link
Contributor Author

/ci

@jloleysens
Copy link
Contributor Author

/ci

@flash1293
Copy link
Contributor

@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

@jloleysens jloleysens requested a review from a team as a code owner March 6, 2025 08:24
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

Copy link
Member

@afharo afharo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a good question. I'll expand our existing tutorial to guide devs through this. Perhaps some tooling could come later!

Copy link
Contributor

@lcawl lcawl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs output LGTM! Thanks for removing the need to accomplish this via overlays, which were much more fragile

Copy link
Member

@cnasikas cnasikas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

@jloleysens
Copy link
Contributor Author

@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?

@cnasikas
Copy link
Member

cnasikas commented Mar 13, 2025

Thanks @jloleysens. Yes, I also lean toward colocating with the schemas (as described in my previous comment), if you don't mind.

@jloleysens jloleysens force-pushed the oas/support-examples branch from 03388fa to 7d3bced Compare March 13, 2025 15:43
kibanamachine and others added 4 commits March 13, 2025 15:57
…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'
@jloleysens jloleysens requested a review from a team as a code owner March 14, 2025 09:48
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommending the code-first approach to keep consistent with the rest of the platform provided approach to OAS.

'validate' | 'options'
> & {
options?: Omit<
options?: Pick<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved closer to respective ResponseOps code

@jloleysens jloleysens self-assigned this Mar 14, 2025
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/response-ops-rule-params 8 9 +1
Unknown metric groups

API count

id before after diff
@kbn/core-http-server 571 572 +1
@kbn/response-ops-rule-params 8 9 +1
total +2

History

cc @jloleysens

Copy link
Member

@cnasikas cnasikas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jloleysens jloleysens merged commit 5e90e55 into elastic:main Mar 14, 2025
9 checks passed
@jloleysens jloleysens deleted the oas/support-examples branch March 14, 2025 13:56
clintandrewhall pushed a commit to clintandrewhall/kibana that referenced this pull request Mar 20, 2025
## 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>
CAWilson94 pushed a commit to CAWilson94/kibana that referenced this pull request Mar 22, 2025
## 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:skip This PR does not require backporting Feature:OAS Work or issues related to Core-provided mechanisms for generating OAS release_note:skip Skip the PR/issue when compiling release notes Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// Team:ResponseOps Platform ResponseOps team (formerly the Cases and Alerting teams) t// v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[OAS] Support examples

8 participants