"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > OpenAPI Trimmer Python Tool

OpenAPI Trimmer Python Tool

Published on 2024-11-06
Browse:106

OpenAPI Trimmer Python Tool

Simplifying Your OpenAPI Files with OpenAPI Trimmer

Managing large OpenAPI files can be a hassle, especially when you only need a small portion of the API for specific tasks. This is where OpenAPI Trimmer comes in handy. It's a lightweight tool designed to trim down your OpenAPI files to include only the endpoints and data transfer objects (DTOs) you care about.

How Does It Work?

Let's say you're working with a large OpenAPI file, but you only need the endpoints related to the Quotes API. You can easily extract just those endpoints and remove irrelevant DTOs with a single command:

openapi-trimmer -i openapi.yaml \
  -p /v1/quotes,/v1/users \
  -ec CompanyConfigDto,UpdateCompanyConfigDto

This command will:

  • -i openapi.yaml: Use your existing OpenAPI YAML file as input.
  • -p /v1/quotes,/v1/users: Keep only the endpoints starting with /v1/quotes and /v1/users.
  • -ec CompanyConfigDto,UpdateCompanyConfigDto: Exclude specific components, in this case, the CompanyConfigDto, and UpdateCompanyConfigDto.

The trimmed API definition will be saved as openapi-trimmer.yaml.

Validation

To ensure the integrity of your trimmed OpenAPI file, validate it with:

swagger-cli validate ./openapi-trimmer.yaml

This step helps catch any issues before you deploy or share the trimmed API file.

Installation

You can install OpenAPI Trimmer directly from PyPi:

pip install openapi-trimmer

For more details and the latest updates, visit the OpenAPI Trimmer PyPi page.

GitHub repository: OpenAPI Trimmer on GitHub.

Command-Line Options

The OpenAPI Trimmer offers several options to customize its operation:

  • -h, --help: Show help information.
  • -v, --version: Display the version number.
  • -i INPUT, --input INPUT: Specify the input YAML file (required).
  • -o OUTPUT, --output OUTPUT: Define the output file name (optional; defaults to appending '-trimmed' to the input file).
  • -p PREFIXES, --prefixes PREFIXES: List the paths to retain in the output (comma-separated).
  • -ec EXCLUDE_COMPONENTS, --exclude-components EXCLUDE_COMPONENTS: List the components to exclude (comma-separated).

Conclusion

OpenAPI Trimmer is an essential tool for developers looking to streamline their OpenAPI files, making them more manageable and tailored to specific needs. Whether you're preparing API documentation or simplifying an API for internal use, OpenAPI Trimmer saves you time and effort by focusing only on what matters most to you.

Release Statement This article is reproduced at: https://dev.to/idachev/openapi-trimmer-python-tool-49j4?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3