Skip to main content

Generates a valid OpenAPI spec from source spec. Various options for filtering out parts of the spec you don't want.

Project description

Filter OpenAPI Specifications

A command-line tool to filter and modify OpenAPI specification files. This tool is designed to help you tailor OpenAPI specifications for specific use cases by filtering out APIs and other information you do not need. A compliant OpenAPI specification is always produced.

Features

  • Filter Out Paths by Tags: Keep only the API paths that are associated with a specific set of tags. Allows you to remove large groups of API that are organized by tag.
  • Filter Endpoints: For any endpoints/paths left after filtering by tag, you can precisely control which endpoints (and which HTTP methods for those endpoints) are included in the final specification.
  • Remove Unreferenced Schemas: Clean up your OpenAPI document by removing any schemas that are no longer referenced after filtering.
  • Remove x-oaiMeta Properties: Strip out x-oaiMeta properties, which are often specific to internal tooling and not needed for public consumption.
  • Filter Top-Level Tags: Ensure the top-level tags array only contains tags that are present in the filtered paths.

Installation

This project uses Python and its dependencies are listed in pyproject.toml. You can install the dependencies using pip:

pip install pyyaml pydantic

Configuration

The tool's behavior is controlled by a JSON configuration file. You can create your own config.json based on the provided config.example.json.

Here's an overview of the configuration options:

  • allowedTags: An array of strings representing the tags to keep. Any path that does not have at least one of these tags will be removed.

    "allowedTags": [
        "Chat",
        "Completions"
    ]
    
  • pathFilters: An object where each key is a path in the OpenAPI specification (e.g., /chat/completions). The value is an object with an allowedMethods array, specifying which HTTP methods to keep (e.g., "POST"). An empty allowedMethods array will cause the entire path to be removed.

    "pathFilters": {
        "/chat/completions": {
            "allowedMethods": [
                "POST"
            ]
        },
        "/chat/completions/{completion_id}": {
            "allowedMethods": []
        }
    }
    
  • options: An object to control additional features.

    • removeXOaiMeta: Set to true to remove all x-oaiMeta fields from the specification.
    • removeUnreferencedSchemas: Set to true to automatically remove any schemas in components/schemas that are not referenced by any of the remaining endpoints.
    "options": {
        "removeXOaiMeta": true,
        "removeUnreferencedSchemas": true
    }
    

Usage

The tool is run from the command line and accepts the following arguments:

  • input_file: (Required) The path to the input OpenAPI YAML file.
  • -c or --config: (Required) The path to your config.json file.
  • -o or --output: (Optional) The path to the output file. If not provided, the modified YAML will be printed to standard output.
  • -v or --verbose: (Optional) Enable verbose logging.

Example

python -m filter_openapi.main openai.yaml --config config.json --output openapi_filtered.yaml

This command will:

  1. Read the openai.yaml file.
  2. Apply the transformations defined in config.json.
  3. Write the resulting specification to openapi_prepared.yaml.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

filter_openapi-0.1.0.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

filter_openapi-0.1.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file filter_openapi-0.1.0.tar.gz.

File metadata

  • Download URL: filter_openapi-0.1.0.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.11

File hashes

Hashes for filter_openapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3ccfc93973b6e3378648b5659e199e240957316d1041fe3d418f5e36a833b6ab
MD5 cbac99d09f0d64d1ac368c840204c4f5
BLAKE2b-256 2c5fcd5c857d5c48bbe3d9100b518278ee501e73c6fcc6dfb0a3782c0390b407

See more details on using hashes here.

File details

Details for the file filter_openapi-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for filter_openapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 737441f08a1d7bea35f0fd5c165eb74b33b11a3710f40a3c3ed3594416820fb1
MD5 da53f3228fc46fe75ecc0fa507ff9992
BLAKE2b-256 1ad37e6e9d9b90fbd7798a97c7a6fa23201a4d9285fc8ee18f86ae3c23fea89b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page