Wapiti Swagger Parser
Project Description
The Wapiti Swagger Parser is a Python library designed to parse Swagger (OpenAPI) specifications and extract the necessary information to generate valid HTTP requests. It focuses on providing a clean, programmatic interface for analyzing Swagger files and creating request templates without relying on external tools for OpenAPI processing.
This library is particularly useful for scenarios where only the request generation requirements are of interest, such as:
- Automated testing and validation of APIs.
- Dynamic request generation for web vulnerability scanners (like Wapiti).
- Custom API tooling.
The library is lightweight, relying only on Python's built-in json library and PyYAML for file parsing, and it avoids heavy dependencies on larger OpenAPI frameworks.
Installation
pip install wapiti-swagger
Key Features
- Request Extraction:
- Parses all HTTP requests (methods, paths, parameters) defined in the Swagger file.
- Reusable Parameters and Request Bodies:
- Follows the
$refof parameters (#/parameters/...,#/components/parameters/...) and request bodies (#/components/requestBodies/...) defined once and shared by several operations. - Parameters defined on a path apply to all its operations, an operation can override them.
- References to another file are not followed.
- Follows the
- Schema Handling:
- Resolves
$refreferences in schemas, including handling circular references gracefully.
- Resolves
- Custom Types:
- Identifies and retains custom types (e.g., enumerated values, objects) for enhanced request understanding.
- Request Body Generation:
- Automatically generates example request bodies based on schema definitions.
- Metadata Extraction:
- Captures root-level metadata like
host,basePath,servers, andschemes.
- Captures root-level metadata like
- Supports Swagger 2.0 and OpenAPI 3.x:
- Works with both specification versions seamlessly.
Usage Example
from wapiti_swagger.parser import parse, generate_request_body_from_schema
# Load and parse a Swagger file
parsed = parse("swagger.json")
# List all available requests
for request in parsed.requests:
print(request)
# Generate an example request body for a specific request (here one expecting JSON input)
request_body = generate_request_body_from_schema(
schema=request.parameters[0].schema, # Use the schema of the first parameter
resolved_components=parsed.components
)
print("Example request body:", request_body)
Why Use This Library?
Unlike general-purpose OpenAPI parsers, this library is optimized for specific use cases like generating valid requests for API testing, scanning, or mocking. It is lightweight, customizable, and avoids unnecessary processing of response definitions or additional metadata unrelated to request generation.
Code Organization
| Module | Role |
|---|---|
wapiti_swagger.parser |
Reads the file and extracts the requests, their parameters and the metadata. |
wapiti_swagger.components |
Resolves the $ref references of the reusable schemas and parameters. |
wapiti_swagger.examples |
Generates example values (request bodies) from schemas. |
wapiti_swagger.models |
Classes holding the result: ParsedSwagger, SwaggerRequest, Parameter. |
Every function documented below can be imported from wapiti_swagger.parser.
Methods in parser Module
1. parse(file_path: str) -> ParsedSwagger
Parses a Swagger/OpenAPI specification file and returns a ParsedSwagger object containing the following:
- Requests: List of
SwaggerRequestobjects extracted from paths. - Components: Preprocessed and resolved components (e.g., schemas, parameters).
- Metadata: High-level metadata like
host,basePath, andservers.
Parameters:
file_path(str): Path to the Swagger/OpenAPI file (JSON or YAML).
Returns:
ParsedSwagger: Object containing parsed requests, components, and metadata.
2. extract_requests(data: dict) -> List[SwaggerRequest]
Extracts all HTTP requests from the paths section of the Swagger specification.
Parameters:
data(dict): The full Swagger/OpenAPI specification as a dictionary.
Returns:
List[SwaggerRequest]: A list of requests with details like method, path, parameters, and request bodies.
3. extract_request_body(request_body: dict) -> List[Parameter]
Extracts parameters from the requestBody section of a Swagger path operation.
Handles multiple media types (e.g., application/json, text/json).
Parameters:
request_body(dict): TherequestBodydefinition for a path operation.
Returns:
List[Parameter]: A list of parameters with details like media type, schema, and custom type.
4. extract_parameter(param: dict) -> Parameter
Parses a single parameter from the parameters section of a Swagger path operation.
Parameters:
param(dict): The parameter definition.
Returns:
Parameter: Object representing the parameter with details like name, location, type, and schema.
5. parse_components(components: dict) -> Dict[str, Dict]
Resolves and preprocesses all components (e.g., schemas, parameters) from the components section of the Swagger specification.
Parameters:
components(dict): Thecomponentssection of the Swagger specification.
Returns:
Dict[str, Dict]: Resolved and preprocessed components organized by type (e.g., schemas, parameters).
6. resolve_schema(schema: dict, components: dict) -> Optional[dict]
Recursively resolves $ref references in a single schema while avoiding circular references.
Parameters:
schema(dict): The schema to resolve.components(dict): The raw components, references are looked up in theirschemassection.
Returns:
Optional[dict]: Fully resolved schema, orNoneif the schema is a reference that can't be resolved.
7. extract_metadata(data: dict) -> Dict[str, Any]
Extracts high-level metadata from the root of the Swagger specification, such as host, basePath, and servers.
Parameters:
data(dict): The full Swagger/OpenAPI specification.
Returns:
Dict[str, Any]: Metadata likehost,basePath,schemes, andservers.
8. generate_request_body_from_schema(schema: dict, resolved_components: dict) -> Optional[Union[dict, list, str, int, bool]]
Generates an example request body based on a schema definition.
Parameters:
schema(dict): The schema definition.resolved_components(dict): Resolved components for reference resolution.
Returns:
Optional[Union[dict, list, str, int, bool]]: An example request body.
Metadata
Release files for wapiti-swagger 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wapiti_swagger-0.2.0.tar.gz | 13.4 kB | Without provenance | Details for wapiti_swagger-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wapiti_swagger-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for wapiti_swagger-0.2.0-py3-none-any.whl |
Total release size: 26.2 kB
Release files / wapiti_swagger-0.2.0.tar.gz
| Download URL | wapiti_swagger-0.2.0.tar.gz |
|---|---|
| Size | 13.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ecd6fb3c0f453dca04413126373dba2fa7f554dcfd2803fb866ef6b3f236328c
|
|
BLAKE2b-256 checksum How to use checksums |
ddd82fcf8762c23f46faa5837054d8f78fe3cb17eb7b165bfa4a4a87d5ff045c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.16
|
Release files / wapiti_swagger-0.2.0-py3-none-any.whl
| Download URL | wapiti_swagger-0.2.0-py3-none-any.whl |
|---|---|
| Size | 12.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b360df8ffd313efe78b89ee4042b53e0bc74a315473c71e58978a7943c024b2e
|
|
BLAKE2b-256 checksum How to use checksums |
4ede020aeae81e58f1183807dcca6fa9a41f5f4c4ec7cebd4e63830868c93367
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.16
|
Security for wapiti-swagger 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.