Skip to content
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

Add how to docs for dealing with x-ms-examples #357

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Changes from all commits
Commits
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
61 changes: 61 additions & 0 deletions docs/howtos/migrate-swagger/x-ms-examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: x-ms-examples
---

The `x-ms-examples` is automatically populated in the generated OpenAPI 2.0 when using the `typespec-autorest` emitter.
The examples must be placed in the `examples-directory` (default to `{project-root}/examples`) and have the `operationdId` property.

:::warn
Do not use `@extension("x-ms-examples", "<value>")`.
:::

## Example structure

Example below assume `example-directory` is `{project-root}/examples`.

- Single version structure

```
main.tsp
examples/
example1.json
example2.json
```

- Multi version structure

```
main.tsp
examples/
2021-01-01/
example1.json
example2.json
2021-01-02/
example1.json
example2.json
```

## Generate the examples

To generate the examples you can use [oav](https://github.com/Azure/oav). You can run that on the generated openapi.json file.

1. Generating basic examples and then manually modify the values. It will generate two examples for each operation: one contains minimal properties set, the other contains the maximal properties set. Since the auto-generated examples consist of random values for most types, you need replace them with meaningful values.

```bash
oav generate-examples openapi.json
```

2. (**Recommended**) Generating high quality examples from API Scenario test. Refer to [API Test section](getstarted/providerhub/step03-api-testing.md). It will validate the API quality and generate Swagger examples from live traffic in API Scenario test.

```bash
oav run <scenario-file> --spec <spec-file> --generateExample
```

Note, latest OAV tool should automatically generate the following. However, if you are generating the examples manually, please ensure you have:

- include `title` field and make sure it is descriptive and unique for each operation.
- include `operationId`. This is used to match with declared operations in TypeSpec and correctly output in swagger.

:::warn
The examples are now in the examples directory relative to the output openapi.json. You must now copy them to the examples directory in the project root. The typespec-autorest emitter will then copy them back to the correct location when generating the OpenAPI 2.0.
:::
Loading