bayfrontmedia/openapi-parser

PHP OpenAPI specification parser

v1.1.1 2024-12-23 21:14 UTC

This package is auto-updated.

Last update: 2024-12-23 21:15:12 UTC


README

PHP OpenAPI specification parser.

This library supports OAS 3.1, and resolves internal OpenAPI specification references. The OpenAPI specification can then be parsed into PHP objects.

License

This project is open source and available under the MIT License.

Author

Bayfront Media

Requirements

  • PHP >= 8.0 (Tested up to 8.4)
  • yaml PHP extension

Installation

composer require bayfrontmedia/opanapi-parser

Usage

The OpenApiSpec class is used to parse JSON and YAML files into an array, as well as resolving any internal references.

The resolved specification array can then be used to create an OpenApiObject instance. All OpenAPI object class instances implement ObjectInterface.

The ObjectInterface includes a getObject method to return the entire object as an array, and a validate() method to validate against the OpenAPI specification, but all validation functions are currently rudimentary and should not be relied upon. It is advised to use this library with an OpenAPI specification which has already been tested as valid.

NOTE: The resolve method can be quite slow depending on the size of the OpenAPI specification. It is strongly suggested to save/cache the resolved specification to use in production, or to only resolve parts of the specification as needed.

Example

$object = OpenApiSpec::parseJson(file_get_contents('openapi-specification.json')); // Parse JSON

$openApiObject = new OpenApiObject($object);

$resolved = OpenApiSpec::resolve($openApiObject, $openApiObject->getObject()); // Resolve internal references (this file should be saved/cached)

// Get OperationObject by path and operation
$pathItemObject = $openApiObject->getPath('/user/login');
$operationObject = $path->getOperation($path::OPERATION_POST);

// Get OperationObject by operation ID
$operationObject = $openApiObject->getOperationObjectById('user.login')

To parse from a .yaml file, the yaml PHP extension must be installed to use the yaml_parse function.

Additional documentation coming soon.