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

Uploaded Python 3

File details

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

File metadata

  • Download URL: filter_openapi-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 6a1d87d0d816a0152a3f193085c99b70379cb29ba7c47250b35345f63df9f1ec
MD5 e1f43e98d8f3621f045020338cff522f
BLAKE2b-256 c5cf0512a76559759eb8ca64557b3fd6566ebdce885f6fb8e4cd44c66b6f85be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for filter_openapi-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e18610ac83820c85fedd6841ce322bc540d22dd8bb00c13a18a33e5a74ad4bae
MD5 a7065da68908c1864898f4469fd0d0bb
BLAKE2b-256 a8e17073d735fec34ea7521664bcf7e7a10222f7a991a7b10a3e261514b48046

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