Convert OpenAPI 3.1.x specifications to 3.0.x while preserving logic
Project description
OpenAPI Downgrader
Convert OpenAPI 3.1.x specifications to 3.0.x — with logic preservation, support for nullable, const, advanced schema handling, and conditionals via oneOf.
Features
The openapi-downgrade tool provides a robust solution for converting OpenAPI 3.1.x specifications to 3.0.x, ensuring maximum compatibility while preserving the original API's logic and intent. Key features include:
- Safe Conversion: Transforms 3.1.x specifications to 3.0.3, handling differences in schema definitions and structural elements.
nullableKeyword Handling: Converts OpenAPI 3.1'stype: ["string", "null"]syntax to OpenAPI 3.0'stype: "string", nullable: true.consttoenumConversion: Replaces theconstkeyword (OpenAPI 3.1) with a single-valueenum(OpenAPI 3.0) to maintain schema constraints.if/then/elsetooneOfTransformation: Translates complex conditional schemas usingif/then/elseinto equivalentoneOfstructures, ensuring logical integrity.examplesKeyword Adaptation: Converts the OpenAPI 3.1examplesarray to the singleexamplekeyword supported in OpenAPI 3.0, using the first example provided.- Webhook Downgrade: Transforms OpenAPI 3.1
webhooksinto OpenAPI 3.0callbackswhere suitable operations (POST, PUT, PATCH, DELETE) are available. If no suitable operations are found, a warning is issued. - JSON Schema Downgrade: Handles various JSON Schema keywords not directly supported in OpenAPI 3.0 (e.g.,
not,dependentSchemas,unevaluatedProperties) by moving them tox-dropped-vendor extensions and issuing warnings. - Boolean String Fixes: Corrects boolean values represented as strings (e.g.,
"true","false") to actual boolean types (true,false). - Exclusive Minimum/Maximum Fixes: Adjusts
exclusiveMinimumandexclusiveMaximumdefinitions for OpenAPI 3.0 compatibility. - Null Type Fixes: Converts
type: "null"tonullable: truefor better OpenAPI 3.0 representation. - Tuple Emulation: Emulates tuple-style
itemsorprefixItemsin arrays usingminItems,maxItems, and a customx-tuple-itemsvendor extension. - Unsupported Keyword Warnings: Identifies and warns about other OpenAPI 3.1 keywords that are not supported in 3.0.x and cannot be automatically converted.
- Validation: Ensures the converted specification is valid against the OpenAPI 3.0.3 schema.
- Warning System: Provides clear warnings for any unsupported or dropped keywords, or for conversions that require manual review, outputting them to the console and an optional
warnings.txtfile.
⚙️ Conversion Rules in Detail
The conversion process applies a series of rules to ensure compatibility and preserve logic:
-
nullableConversion:- OpenAPI 3.1:
type: ["string", "null"] - OpenAPI 3.0:
type: "string", nullable: true - Purpose: Adapts the way nullability is expressed.
- OpenAPI 3.1:
-
consttoenum:- OpenAPI 3.1:
const: "value" - OpenAPI 3.0:
enum: ["value"] - Purpose: Provides an equivalent constraint using OpenAPI 3.0's
enumkeyword.
- OpenAPI 3.1:
-
if/then/elsetooneOf:- OpenAPI 3.1: Conditional schemas using
if,then, andelse. - OpenAPI 3.0: Transformed into
oneOfstructures with appropriaterequiredandpropertiesto mimic the conditional logic. - Purpose: Emulates complex conditional logic not directly available in OpenAPI 3.0.
- OpenAPI 3.1: Conditional schemas using
-
examplesArray toexample:- OpenAPI 3.1:
examples: ["example1", "example2"] - OpenAPI 3.0:
example: "example1" - Purpose: Adapts to the single
examplefield in OpenAPI 3.0.
- OpenAPI 3.1:
-
Webhook Downgrade:
- OpenAPI 3.1:
webhookssection. - OpenAPI 3.0: Converted to
callbackswithinpathsfor POST, PUT, PATCH, and DELETE operations. A default URL expression{$request.body#/callbackUrl}is used. - Purpose: Provides a compatible mechanism for event-driven API descriptions. Warnings are issued if webhooks cannot be attached to suitable operations.
- OpenAPI 3.1:
-
JSON Schema Downgrade:
- OpenAPI 3.1: Supports a wider range of JSON Schema keywords (e.g.,
not,dependentSchemas,unevaluatedProperties). - OpenAPI 3.0: These keywords are moved to
x-dropped-vendor extensions (e.g.,x-dropped-not) and warnings are generated. - Purpose: Preserves information about unsupported keywords while ensuring schema validity for OpenAPI 3.0.
- OpenAPI 3.1: Supports a wider range of JSON Schema keywords (e.g.,
-
Boolean String Fixes:
- OpenAPI 3.1: Allows boolean values as strings (e.g.,
"true"). - OpenAPI 3.0: Requires actual boolean types (
true). - Purpose: Ensures correct data type representation.
- OpenAPI 3.1: Allows boolean values as strings (e.g.,
-
Exclusive Minimum/Maximum Fixes:
- OpenAPI 3.1:
exclusiveMinimum: 10 - OpenAPI 3.0:
minimum: 10, exclusiveMinimum: true - Purpose: Aligns with OpenAPI 3.0's way of defining exclusive bounds.
- OpenAPI 3.1:
-
Null Type Fixes:
- OpenAPI 3.1:
type: "null" - OpenAPI 3.0:
nullable: true - Purpose: Standardizes null type representation.
- OpenAPI 3.1:
-
Tuple Emulation:
- OpenAPI 3.1:
items: [schema1, schema2]orprefixItems: [schema1, schema2] - OpenAPI 3.0: Emulated using
minItems,maxItems, andx-tuple-itemsvendor extension. - Purpose: Provides a way to represent ordered, fixed-length arrays.
- OpenAPI 3.1:
-
Unsupported Keyword Warnings:
- OpenAPI 3.1: Keywords like
contains,patternProperties,items(in some forms) are not fully supported in OpenAPI 3.0. - OpenAPI 3.0: These keywords are retained but warnings are issued to alert the user about potential compatibility issues.
- Purpose: Informs the user about elements that might behave differently or require manual adjustment.
- OpenAPI 3.1: Keywords like
📦 Installation
You can install the tool from PyPI:
pip install openapi-downgrade
Or, for development, you can clone the repository and install it in editable mode:
git clone https://github.com/RajeshRoy4426/openapi_downgrade_3_0.git
cd openapi_downgrade_3_0
pip install -e .
Usage
The command-line interface allows you to convert an OpenAPI specification from a file or a URL.
Command Syntax
openapi_downgrade <input_path_or_url> <output_path>
To see detailed help and available options, use:
openapi_downgrade --help
Arguments
<input_path_or_url>: The path to your local OpenAPI 3.1.x file or a URL to a raw spec.<output_path>: The file path where the converted 3.0.x spec will be saved.
Example
python -m openapi_downgrade.cli https://testapi334-d4fvgterd3cjd2bf.southeastasia-01.azurewebsites.net/openapi.json openapi_3_0.json
This will download the OpenAPI 3.1.x specification from the provided URL, convert it to OpenAPI 3.0.x, and save the result as openapi_3_0.json in your current directory.
Handling Warnings
During conversion, the tool may encounter elements that are not directly convertible or require manual review. These situations generate warnings:
- Console Output: Warnings are printed directly to your console during the conversion process.
warnings.txtFile: Awarnings.txtfile is automatically generated in the current working directory if any warnings occur. This file contains a detailed list of all warnings, which you should review to ensure the converted specification meets your requirements.
It's crucial to review these warnings and make any necessary manual adjustments to the generated OpenAPI 3.0.x specification.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openapi_downgrade-1.0.1.tar.gz.
File metadata
- Download URL: openapi_downgrade-1.0.1.tar.gz
- Upload date:
- Size: 20.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
432c43a502610fb6b70ca64f67bcf60641d2d248de55fbd4ace8cfb3593ae9a8
|
|
| MD5 |
b3fc15bd0d53d58777a67cd037616431
|
|
| BLAKE2b-256 |
82e3bb38cfd47f956d1e382387065f601e68c124155a405a1bd45eaa3acbd6ca
|
File details
Details for the file openapi_downgrade-1.0.1-py3-none-any.whl.
File metadata
- Download URL: openapi_downgrade-1.0.1-py3-none-any.whl
- Upload date:
- Size: 24.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1afd2017ac6d83441f65ad5fa988d86f54c5707ac5d14510b75622367d639227
|
|
| MD5 |
34d87727135bd9eabeec1208692b4552
|
|
| BLAKE2b-256 |
722b858ae3a29558314518ccdc06884ef2c8fc86ca7064e0ae63c8faf08aeffb
|