Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nikolaydubina/openapi-inline-examples
🌏 Inline OpenAPI JSON examples from filenames
https://github.com/nikolaydubina/openapi-inline-examples
documentation openapi
Last synced: about 2 months ago
JSON representation
🌏 Inline OpenAPI JSON examples from filenames
- Host: GitHub
- URL: https://github.com/nikolaydubina/openapi-inline-examples
- Owner: nikolaydubina
- License: mit
- Created: 2021-04-03T03:11:13.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-22T09:10:15.000Z (5 months ago)
- Last Synced: 2024-11-02T12:26:04.429Z (2 months ago)
- Topics: documentation, openapi
- Language: Go
- Homepage:
- Size: 174 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Citation: CITATION.cff
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
[![codecov](https://codecov.io/gh/nikolaydubina/openapi-inline-examples/branch/main/graph/badge.svg?token=J97ET3LIQA)](https://codecov.io/gh/nikolaydubina/openapi-inline-examples)
[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/nikolaydubina/openapi-inline-examples/badge)](https://securityscorecards.dev/viewer/?uri=github.com/nikolaydubina/openapi-inline-examples)> How do I add JSON examples to `openapi.yaml` from `.json` files?
Add to your `openapi.yaml` annotation `#source ` like
```yaml
openapi: 3.0.0
info:
version: 1.0.0
title: Swagger Marvelstorepaths:
/users:
get:
responses:
'200':
description: A user object.
content:
application/json:
schema:
type: object
properties:
id:
type: integer
format: int64
example: 4
name:
type: string
example: Jessica Smith
examples:
basic:
value: #source testdata/user-basic.json
'400':
description: The specified user ID is invalid (not a number).
content:
application/json:
examples:
basic 400:
value: #source testdata/error-400.json
```.. then run
```
$ go install github.com/nikolaydubina/openapi-inline-examples@latest
$ cat openapi.yaml | openapi-inline-examples > openapi.new.yaml
```.. which will produce
```yaml
openapi: 3.0.0
info:
version: 1.0.0
title: Swagger Marvelstorepaths:
/users:
get:
responses:
'200':
description: A user object.
content:
application/json:
schema:
type: object
properties:
id:
type: integer
format: int64
example: 4
name:
type: string
example: Jessica Smith
examples:
basic:
value: {"id":42,"name":"Nick Fury"} #source testdata/user-basic.json
'400':
description: The specified user ID is invalid (not a number).
content:
application/json:
examples:
basic 400:
value: {"errors":[{"message":"resource not found","status":400,"translation_data":"some translation identifier"}]} #source testdata/error-400.json
```.. which renders nicely as multiple examples that you can select
![example-preview](docs/example.png)
Why would anyone need this?
- Keep your OpenAPI up to date with values you use in tests
- Can run multiple times
- UNIX filter
- Does not corrupt anything if fails at any stage
- 100% test coverage