Skip to main content

A utility to extract JSON Schema from a valid OpenAPI specification

Project description

openapi2jsonschema2

A utility to extract JSON Schema from a valid OpenAPI specification.

Forked and updated from openapi2jsonschema. The original project does not appear to be maintained. All credit to the original authors.

Why

OpenAPI contains a list of type definitions using a superset of JSON Schema. These are used internally by various OpenAPI compatible tools. I found myself however wanting to use those schemas separately, outside existing OpenAPI tooling. Generating separate schemas for types defined in OpenAPI allows for all sorts of indepent tooling to be build which can be easily maintained, because the canonical definition is shared.

Installation

openapi2jsonschema2 is implemented in Python. Assuming you have a Python intepreter and pip installed you should be able to install with:

pip install openapi2jsonschema2

This has not yet been widely tested and is currently in a works on my machine state.

Usage

The simplest usage is to point the openapi2jsonschema2 tool at a URL containing a JSON (or YAML) OpenAPI definition like so:

openapi2jsonschema2 https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json

This will generate a set of schemas in a schemas directory. The tool provides a number of options to modify the output:

$ openapi2jsonschema2 --help
Usage: openapi2jsonschema2 [OPTIONS] SCHEMA

  Converts a valid OpenAPI specification into a set of JSON Schema files

Options:
  -o, --output PATH  Directory to store schema files
  -p, --prefix TEXT  Prefix for JSON references (only for OpenAPI versions
                     before 3.0)
  --stand-alone      Whether or not to de-reference JSON schemas
  --kubernetes       Enable Kubernetes specific processors
  --strict           Prohibits properties not in the schema
                     (additionalProperties: false)
  --help             Show this message and exit.

Example

My specific use-case was being able to validate a Kubernetes configuration file without a Kubernetes client like kubectl and without the server. For that I have a bash script, available here.

The output from running this script can be seen in the accompanying instrumenta/kubernetes-json-schema.

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

openapi2jsonschema2-1.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

openapi2jsonschema2-1.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file openapi2jsonschema2-1.1.0.tar.gz.

File metadata

  • Download URL: openapi2jsonschema2-1.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Darwin/23.5.0

File hashes

Hashes for openapi2jsonschema2-1.1.0.tar.gz
Algorithm Hash digest
SHA256 4a9457c6e5621e11f4cc5672d55e271457106b4f5ce398db54b2874f72237ea1
MD5 9aa616517cd8dbf4c4750679216a607a
BLAKE2b-256 7369d1e43b6ccc3aeddbc09b0ade9799c3a427ab15d41107e0dd8a0026a552b4

See more details on using hashes here.

File details

Details for the file openapi2jsonschema2-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openapi2jsonschema2-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 404cf6037dc6d590fd2479d087de299140d2f8e7e1dfa53e53b0bcfdd55c0138
MD5 22dfa14ab69e8cf65b07e4482768af69
BLAKE2b-256 4af439f10e6b9c6e85f23e18e6c96b73d5f75f525bdf4c6945cf1f4b868a0145

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page