Convert OpenAPI 3.1.x specifications to 3.0.x — with logic preservation, support for nullable, const, advanced schema handling, and conditionals via oneOf.
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.
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
You can install the tool from PyPI:
pip install openapi-downgradeOr, 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 .The command-line interface allows you to convert an OpenAPI specification from a file or a URL.
openapi_downgrade <input_path_or_url> <output_path>To see detailed help and available options, use:
openapi_downgrade --help<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.
python -m openapi_downgrade.cli https://testapi334-d4fvgterd3cjd2bf.southeastasia-01.azurewebsites.net/openapi.json openapi_3_0.jsonThis 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.
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.