mirror of
https://github.com/LukeHagar/prettier-plugin-openapi.git
synced 2025-12-06 04:21:03 +00:00
61b49e4616a02d5c2e38b0d45756d1d8fdb942dc
Prettier Plugin OpenAPI
A Prettier plugin for formatting OpenAPI/Swagger JSON and YAML files with intelligent key sorting and proper indentation.
Features
- 🎯 OpenAPI/Swagger Support: Formats both JSON and YAML OpenAPI specifications
- 🔄 Smart Key Sorting: Automatically sorts OpenAPI keys in the recommended order
- 📝 YAML & JSON: Supports both
.yaml/.ymland.jsonfile formats - 🎨 Consistent Formatting: Applies consistent indentation and line breaks
- ⚡ Fast: Built with performance in mind using modern JavaScript
Installation
npm install --save-dev prettier-plugin-openapi
# or
yarn add --dev prettier-plugin-openapi
# or
bun add --dev prettier-plugin-openapi
Usage
Command Line
# Format a single file
npx prettier --write api.yaml
# Format all OpenAPI files in a directory
npx prettier --write "**/*.{openapi.json,openapi.yaml,swagger.json,swagger.yaml}"
# Format with specific options
npx prettier --write api.yaml --tab-width 4 --print-width 100
Configuration
Add the plugin to your Prettier configuration:
package.json
{
"prettier": {
"plugins": ["prettier-plugin-openapi"]
}
}
.prettierrc
{
"plugins": ["prettier-plugin-openapi"],
"tabWidth": 2,
"printWidth": 80
}
.prettierrc.js
module.exports = {
plugins: ['prettier-plugin-openapi'],
tabWidth: 2,
printWidth: 80,
};
Supported File Extensions
.openapi.json.openapi.yaml.openapi.yml.swagger.json.swagger.yaml.swagger.yml
Key Sorting
The plugin automatically sorts OpenAPI keys in the recommended order:
Top-level keys:
openapiinfoserverspathscomponentssecuritytagsexternalDocs
Info section:
titledescriptionversiontermsOfServicecontactlicense
Components section:
schemasresponsesparametersexamplesrequestBodiesheaderssecuritySchemeslinkscallbacks
Examples
Before (unformatted):
paths:
/users:
get:
responses:
'200':
description: OK
components:
schemas:
User:
type: object
openapi: 3.0.0
info:
version: 1.0.0
title: My API
After (formatted):
openapi: 3.0.0
info:
title: My API
version: 1.0.0
paths:
/users:
get:
responses:
'200':
description: OK
components:
schemas:
User:
type: object
Development
Setup
# Install dependencies
bun install
# Build the plugin
bun run build
# Run tests
bun test
# Run demo
bun run test/demo.ts
Project Structure
src/
index.ts # Main plugin implementation
test/
plugin.test.ts # Unit tests
demo.ts # Demo script
examples/
petstore.yaml # Example OpenAPI file
Configuration Options
The plugin respects standard Prettier options:
tabWidth: Number of spaces for indentation (default: 2)printWidth: Maximum line length (default: 80)useTabs: Use tabs instead of spaces (default: false)
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
License
MIT License - see LICENSE file for details.
Related Projects
- Prettier - The core formatter
- OpenAPI Specification - The OpenAPI specification
- Swagger - API development tools
Description
Languages
TypeScript
77.8%
JavaScript
22.2%