Registry / serialization / py-openapi-schema-to-json-schema

py-openapi-schema-to-json-schema

JSON →
library0.0.3pypypi✓ verified 24d ago

This Python library provides a utility to convert OpenAPI 3.0 Schema Objects into compatible JSON Schema Draft 4 representations. It addresses key discrepancies between the two specifications, such as `nullable` handling and removal of OpenAPI-specific keywords, to enable validation with standard JSON Schema validators. This package is a direct Python port of the JavaScript library `mikunn/openapi-schema-to-json-schema` (v2.1.0).

pip install py-openapi-schema-to-json-schema
INSTALL
IMPORT
SIG · PY-OPENAPI-SCHEMA-
P
py-openapi-schema-to-json-schema
serializationpythonv0.0.3
Install
1.6s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.0.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.006s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

to_json_schema
from openapi_schema_to_json_schema import to_json_schema

Demonstrates converting a simple OpenAPI schema object, including handling `nullable` and an OpenAPI extension field `x-patternProperties`.

import json from openapi_schema_to_json_schema import to_json_schema openapi_schema = { "type": "object", "properties": { "name": { "type": "string", "nullable": True, } }, "x-patternProperties": { "^[a-z]+$": { "type": "number", } } } # Options can be passed to control conversion behavior # For example, to enable conversion of 'x-patternProperties' to 'patternProperties' options = {"supportPatternProperties": True} converted_schema = to_json_schema(openapi_schema, options) print(json.dumps(converted_schema, indent=2))
Debug
Known issues
gotchaThe library does not dereference `$ref` fields within the OpenAPI schema. If your schema uses `$ref`s, you must resolve them using another tool or library before passing the schema to `to_json_schema`.
fix
Use a separate library (e.g., `jsonref` or an OpenAPI parser) to dereference `$ref`s in your OpenAPI schema before conversion.
affects: 0.0.3
gotchaThis library converts OpenAPI 3.0 schemas to JSON Schema Draft 4. It does not support newer JSON Schema drafts (e.g., Draft 2019-09, 2020-12) or OpenAPI 3.1, which aligns with newer JSON Schema versions. Using it for newer standards may lead to incompatible or incomplete conversions.
fix
Be aware of the target JSON Schema draft. If you require conversion to a newer JSON Schema version or are working with OpenAPI 3.1, this library might not be suitable or may require post-processing.
affects: 0.0.3
gotchaBy default, several OpenAPI-specific keywords (e.g., `discriminator`, `readOnly`, `writeOnly`, `xml`, `externalDocs`, `example`, `deprecated`) are removed from the resulting JSON Schema as they are not standard in JSON Schema Draft 4. The `nullable` keyword is converted into a `type` array that includes `"null"`.
fix
Use the `keepNotSupported` option (a list of strings) to explicitly retain specific OpenAPI-only fields if they are critical for your use case and your JSON Schema validator can handle them as extensions.
affects: 0.0.3
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'py_openapi_schema_to_json_schema'
The Python package name for installation (`py-openapi-schema-to-json-schema`) differs from the module name used for importing (`openapi_schema_to_json_schema`).
fix
Use the correct module name in your import statement: `from openapi_schema_to_json_schema import to_json_schema`.
AttributeError: module 'openapi_schema_to_json_schema' has no attribute 'convert'
You are attempting to call a function or access an attribute named `convert` which does not exist in the `openapi_schema_to_json_schema` module; the main conversion function is named `to_json_schema`.
fix
Call the correct conversion function: `from openapi_schema_to_json_schema import to_json_schema` then `to_json_schema(your_schema)`.
TypeError: 'module' object is not callable
You have imported the entire `openapi_schema_to_json_schema` module and are attempting to call the module itself as if it were a function, rather than calling its specific conversion function `to_json_schema`.
fix
Import the specific `to_json_schema` function and call it: `from openapi_schema_to_json_schema import to_json_schema` then `json_schema = to_json_schema(openapi_schema)`.
Upgrade
Version history
0.0.3latest on PyPI · released Jul 25, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
py-openapi-schema-to-json-schema — pip install py-openapi-schema-to-json-schema · libregistry