PHPackages                             benmorel/openapi-schema-to-json-schema - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. benmorel/openapi-schema-to-json-schema

ActiveLibrary[API Development](/categories/api)

benmorel/openapi-schema-to-json-schema
======================================

Convert an OpenAPI schema object to a JSON Schema object

0.1.4(5y ago)3183.8k↓60.5%22MITPHPPHP ^7.2 || ^8.0CI failing

Since Apr 4Pushed 2y ago1 watchersCompare

[ Source](https://github.com/BenMorel/openapi-schema-to-json-schema)[ Packagist](https://packagist.org/packages/benmorel/openapi-schema-to-json-schema)[ RSS](/packages/benmorel-openapi-schema-to-json-schema/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (2)

OpenAPI schema to JSON Schema
=============================

[](#openapi-schema-to-json-schema)

A PHP library to convert an OpenAPI schema or parameter object to [JSON Schema](https://json-schema.org/).

[![Build Status](https://github.com/BenMorel/openapi-schema-to-json-schema/workflows/CI/badge.svg)](https://github.com/BenMorel/openapi-schema-to-json-schema/actions)[![Coverage Status](https://camo.githubusercontent.com/8c466264d3f0086d73f13a25dfa76545b4fdb14db5199c72438d938a8397d453/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f42656e4d6f72656c2f6f70656e6170692d736368656d612d746f2d6a736f6e2d736368656d612f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/BenMorel/openapi-schema-to-json-schema?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/e2ca4714ecf5abe94967d7d633de58e86f946c26d43570983a8eb8b72c6b7a71/68747470733a2f2f706f7365722e707567782e6f72672f62656e6d6f72656c2f6f70656e6170692d736368656d612d746f2d6a736f6e2d736368656d612f762f737461626c65)](https://packagist.org/packages/benmorel/openapi-schema-to-json-schema)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](http://opensource.org/licenses/MIT)

This is a port of the [nodejs package](https://www.npmjs.com/package/openapi-schema-to-json-schema) by [@mikunn](https://github.com/mikunn).

It currently converts from [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md) to [JSON Schema Draft 4](http://json-schema.org/specification-links.html#draft-4).

Why?
----

[](#why)

OpenAPI is a specification for describing RESTful APIs. OpenAPI 3.0 allows us to describe the structures of request and response payloads in a detailed manner. This would, theoretically, mean that we should be able to automatically validate request and response payloads. However, at the time of writing there aren't many validators around.

The good news is that there are many validators for JSON Schema, such as [justinrainbow/json-schema](https://github.com/justinrainbow/json-schema). The bad news is that OpenAPI 3.0 is not entirely compatible with JSON Schema. The Schema object of OpenAPI 3.0 is an extended subset of JSON Schema Specification Wright Draft 00 with some differences.

The purpose of this project is to fill the gap by doing the conversion between these two formats.

Features
--------

[](#features)

- converts OpenAPI 3.0 Schema object to JSON Schema Draft 4
- converts OpenAPI 3.0 Parameter object to JSON Schema Draft 4
- deletes `nullable` and adds `"null"` to `type` array if `nullable` is `true`
- supports deep structures with nested `allOf`s etc.
- removes [OpenAPI specific properties](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#fixed-fields-20) such as `discriminator`, `deprecated` etc. unless specified otherwise
- optionally supports `patternProperties` with `x-patternProperties` in the Schema object

**NOTE**: `$ref`s are not dereferenced. Use a dereferencer prior to using this package.

Installation
------------

[](#installation)

This library is installable via [Composer](https://getcomposer.org/):

```
composer require benmorel/openapi-schema-to-json-schema
```

Requirements
------------

[](#requirements)

This library requires:

- PHP 7.2 or later
- the [json](http://php.net/manual/en/book.json.php) extension

There is no dependency on third-party libraries.

Project status &amp; release process
------------------------------------

[](#project-status--release-process)

The current releases are numbered `0.x.y`. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), `y` is incremented.

**When a breaking change is introduced, a new `0.x` version cycle is always started.**

It is therefore safe to lock your project to a given release cycle, such as `0.1.*`.

If you need to upgrade to a newer release cycle, check the [release history](https://github.com/BenMorel/openapi-schema-to-json-schema/releases)for a list of changes introduced by each further `0.x.0` version.

Converting an OpenAPI schema
----------------------------

[](#converting-an-openapi-schema)

Here's a small example to get the idea:

```
use BenMorel\OpenApiSchemaToJsonSchema\Convert;

$schema = json_decode(
