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.1.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.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: filter_openapi-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 78668da9265889779b813dcfc9a8969c878c2eef23e7bc6119eba501623025cb
MD5 4c14c41774b6e1ac313610c89640440e
BLAKE2b-256 119cae9bf6fff831b7478ba93e6dd0db3830254cc2f9f738793b9ebcfa43df9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for filter_openapi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dc32779051d06fb0985ff17087003f42b7fd9299d5fad1fbb9264f004f7b4827
MD5 2e665637af22ae7d9b984891b396c2cc
BLAKE2b-256 04a89f4537b359a15e186aa5b8ac4a9f7219c148e99797adcebe9310ce1ef55a

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