Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b20a42b
attempt 1 at a layout for OAS examples
jloleysens Feb 26, 2025
1e5af2a
added support for merging files
jloleysens Feb 26, 2025
277169c
comment out the rules create overlay
jloleysens Feb 26, 2025
1232c10
await
jloleysens Feb 26, 2025
c23f4ef
add new type
jloleysens Feb 26, 2025
58a9e60
link to file from create rule
jloleysens Feb 26, 2025
bb15822
added @apidevtools/json-schema-ref-parser
jloleysens Feb 26, 2025
74c53ba
woops
jloleysens Feb 26, 2025
01805e1
[CI] Auto-commit changed files from 'node scripts/notice'
kibanamachine Feb 26, 2025
f9c1241
[CI] Auto-commit changed files from 'node scripts/capture_oas_snapsho…
kibanamachine Feb 27, 2025
bc1c1d9
Merge branch 'main' into oas/support-examples
jloleysens Mar 3, 2025
3e1ddb7
[CI] Auto-commit changed files from 'make api-docs'
kibanamachine Mar 3, 2025
5071537
Merge branch 'main' into oas/support-examples
jloleysens Mar 4, 2025
e392720
update renovate.json
jloleysens Mar 4, 2025
2571ecb
support operation object too
jloleysens Mar 4, 2025
ecaba38
remove commented out lines
jloleysens Mar 4, 2025
c7e27c4
remove trailing comma for now
jloleysens Mar 4, 2025
af666a6
rename function
jloleysens Mar 4, 2025
cf7f624
added unit test
jloleysens Mar 4, 2025
9a9e0c0
clean up comment
jloleysens Mar 4, 2025
62b8249
Merge branch 'main' into oas/support-examples
jloleysens Mar 13, 2025
09c745a
update docs
jloleysens Mar 13, 2025
fce5f75
fix where the examples are linked in for the versioned router
jloleysens Mar 13, 2025
7d3bced
move response ops files
jloleysens Mar 13, 2025
68554f8
[CI] Auto-commit changed files from 'node scripts/notice'
kibanamachine Mar 13, 2025
53efeee
[CI] Auto-commit changed files from 'node scripts/capture_oas_snapsho…
kibanamachine Mar 13, 2025
10043fd
[CI] Auto-commit changed files from 'make api-docs'
kibanamachine Mar 13, 2025
e8b733b
clean up code, types and comments
jloleysens Mar 14, 2025
a707aee
Merge branch 'main' into oas/support-examples
jloleysens Mar 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 120 additions & 2 deletions dev_docs/tutorials/generating_oas_for_http_apis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import { fooResource } from '../../schemas/v1';
// Note: this response schema is instantiated lazily to avoid creating schemas that are not needed in most cases!
const fooResourceResponse = () => {
return schema.object({
id: schema.string({
id: schema.string({
maxLength: 20,
meta: { description: 'Add a description.' }
}),
Expand Down Expand Up @@ -129,7 +129,7 @@ function registerFooRoute(router: IRouter, docLinks: DoclinksStart) {
version: '2023-10-31',
validate: {
request: {
body: ,
body: fooResource,
},
response: {
200: {
Expand All @@ -155,6 +155,124 @@ function registerFooRoute(router: IRouter, docLinks: DoclinksStart) {
}
```

##### Adding examples

Beyond the schema of requests and responses, it is **very useful** to provide
concrete requests and responses as examples. Examples go beyond defaults and
provide a more intuitive understanding for end users in learning the behaviour
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
Copy Markdown
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.


```typescript
// ...
.addVersion({
version: '2023-10-31',
options: {
// Be sure and lazily instantiate this value. It's only used at dev time!
oasOperationObject: () => ({
requestBody: {
content: {
'application/json': {
examples: {
fooExample1: {
summary: 'An example foo request',
value: {
name: 'Cool foo!',
} as FooResource,
},
},
},
},
},
responses: {
200: {
content: {
'application/json': {
examples: {
/* Put your 200 response examples here */
},
},
},
},
},
}),
},
validate: {
request: {
body: fooResource,
},
response: {
200: {
body: fooResourceResponse,
},
},
},
},
// ...
```
The strength of this approach is your examples are captured in code and type
checked at dev time. So any shape errors should be caught as you author.

<details>

<summary>I have prexisting YAML based examples I'd like to use!</summary>

If you pre-existing examples created in YAML that you would like
to use the following approach:

```typescript
import path from 'node:path';

const oasOperationObject: () => path.join(__dirname, 'foo.examples.yaml'),

// ...
.addVersion({
version: '2023-10-31',
options: {
oasOperationObject,
},
validate: {
request: {
body: fooResource,
},
response: {
200: {
body: fooResourceResponse,
},
},
},
},
// ...
```

Where the contents of `foo.examples.yaml` are:

```yaml
requestBody:
content:
application/json:
examples: # Make sure to use the examples array, example (singular) has been deprecated
fooExample:
summary: Foo example
description: >
An example request of creating foo.
value:
name: 'Cool foo!'
fooExampleRef:
# You can use JSONSchema $refs to organize this file further
$ref: "./examples/foo_example_i_factored_out_of_this_file.yaml"
responses:
200:
content:
application/json:
examples:
# Apply a similar pattern to writing examples here
```

</details>

#### 3. Generating OAS

See <a href="#how-do-i-see-my-http-apis-oas">this section</a> about viewing your HTTP APIs OAS.
Expand Down
Loading