Skip to content
An object oriented approach to generating OpenAPI specs, implemented in PHP.
Branch: master
Clone or download
matthew-inamdar Feature/no security for operations (#20)
* Added ability to set no security on an operation

* Swapped out PHP_CodeSniffer for PHP-CS-Fixer

* Ran through new code fixer

* Fixed Travis CI tests

* Added updated test invocation to README
Latest commit 473ae1a May 21, 2019

Object Oriented OpenAPI Specification

GitHub stars

GitHub tag (latest SemVer) Build status Packagist PHP from Packagist Dependency count Packagist


An object oriented approach to generating OpenAPI specs, implemented in PHP.

You can build up your API spec using immutable PHP classes, and then export the spec to JSON (or YAML with the help of another package).

This package is dependency free and makes heavy use of PHP 7 features, mainly being type hints and enabling strict types. This should make your life a lot easier when working with a good IDE that can use this information.


You can install the package via composer:

composer require goldspecdigital/oooas


See the code sample below for the most basic usage:

use GoldSpecDigital\ObjectOrientedOAS\Objects\{
    Info, MediaType, Operation, PathItem, Response, Schema, Tag
use GoldSpecDigital\ObjectOrientedOAS\OpenApi;

// Create a tag for all the user endpoints.
$usersTag = Tag::create()
    ->description('All user related endpoints');

// Create the info section.
$info = Info::create()
    ->title('API Specification')
    ->description('For using the Example App API');
// Create the user schema.
$userSchema = Schema::object()
// Create the user response.
$userResponse = Response::create()
// Create the operation for the route (i.e. GET, POST, etc.).
$showUser = Operation::get()
    ->summary('Get an individual user')
// Define the /users path along with the supported operations.
$usersPath = PathItem::create()
// Create the main OpenAPI object composed off everything created above.
$openApi = OpenApi::create()
header('Content-Type: application/json');
echo $openApi->toJson();

YAML output

Using the same code above will output the following YAML:

In this example, the YAML may seem simpler to look at, however once the spec starts to increase in size - the ability to reuse objects and split them into separate files easily will be a massive help.

openapi: 3.0.2
  title: API Specification
  description: For using the Example App API
  version: v1
      - Users
      summary: Get an individual user
          description: OK
                type: object
                    format: uuid
                    type: string
                    type: string
                    type: integer
                    example: 23
                    format: date-time
                    type: string
- name: Users
  description: All user related endpoints

Outputting as JSON or YAML

Built in output to YAML has been omitted on purpose to keep this package dependency free. However, you can easily convert the array to a YAML string using several open source packages. See below for an example of outputting to both JSON and YAML:

use GoldSpecDigital\ObjectOrientedOAS\OpenApi;
use Symfony\Component\Yaml\Yaml;

$openApi = OpenApi::create();

$json = $openApi->toJson();
$array = $openApi->toArray();
$yaml = Yaml::dump($array);


If you want to learn more about the OpenAPI schema, then have a look at the official OpenAPI Specification.

Alternatively, if you would like a quick reference, then check out the OpenAPI Map project created by Arnaud Lauret.

You can use this interactive tool to figure out what objects go where and how they relate to one another.


Setting and unsetting properties

Each object has setter methods for it's supported properties. Most of these methods allow null values which will need to be explicitly passed (see the next example for how to unset using variadic setter methods). This will have the effect of unsetting the property:

$info = Info::create()
    ->title('Example API');

$openApi = OpenApi::create()
// $openApi->toJson() -> '{"info": {"title": "Example API"}}'

$openApi = $openApi->info(null);
// $openApi->toJson() -> '{}'

For variadic setter methods, if you call the method and don't supply any parameters, then this will have the effect of unsetting the property:

$path = PathItem::create()

$openApi = OpenApi::create()
// $openApi->toJson() -> '{"paths": {"/users": []}}'

$openApi = $openApi->paths();
// $openApi->toJson() -> '{}'

Retrieving properties

You can easily retrieve a property using a magic getter. These have been implemented for all properties for every object. DocBlocks have been provided to give better auto-completion in IDEs:

$info = Info::create()->title('Example API');

// $info->title -> 'Example API'

Object ID

Every object has an optional $objectId property which is a string and can either be set in the class constructor or the preferred create() method. This property is used when a parent object needs to use a name for the children.

An example of this in use is when a schema object is composed of other schema properties:

$schema = Schema::create()
  "type": "object",
  "properties": {
    "username": {
      "type": "string"
    "age": {
      "type": "integer"

If an object contains any helper creation methods, then these methods also allow you to specify the $objectId property as a parameter. The code sample below is functionally identical to the one above:

$schema = Schema::object()
  "type": "object",
  "properties": {
    "username": {
      "type": "string"
    "age": {
      "type": "integer"

Running the tests

To run the test suite you can use the following commands:

# To run both style and unit tests.
composer test

# To run only style tests.
composer test:style

# To run only unit tests.
composer test:unit

If you receive any errors from the style tests, you can automatically fix most, if not all of the issues with the following command:

composer fix:style


Please read for details on our code of conduct, and the process for submitting pull requests to us.


We use SemVer for versioning. For the versions available, see the tags on this repository.


See also the list of contributors who participated in this project.


This project is licensed under the MIT License - see the file for details.

You can’t perform that action at this time.