adachsoft / open-api-reader
Open API reader library
Requires
- php: >=8.1
- adachsoft/collection: ^3.0
- cebe/php-openapi: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.4.3
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.0
- rector/rector: ^2.3
This package is not auto-updated.
Last update: 2026-04-03 19:01:08 UTC
README
PHP library for reading and processing OpenAPI 3 specifications (JSON / YAML).
- PHP ">= 8.1"
- Framework‑agnostic
- Supports:
- loading OpenAPI specs from
.jsonand.yaml/.ymlfiles, - listing endpoints with pagination,
- fetching single endpoint details (parameters, request body, responses),
- reading schema definitions from
components.schemas, - searching endpoints by path and summary/description.
- loading OpenAPI specs from
Installation
composer require adachsoft/open-api-reader
Requires PHP 8.1 or higher (as defined in composer.json).
Basic usage
The recommended way to use the library is via the built‑in factory OpenApiReaderFactory:
use AdachSoft\OpenApiReader\Factory\OpenApiReaderFactory;
$factory = OpenApiReaderFactory::createDefault();
// Load specification from JSON or YAML file
$reader = $factory->createFromFile(__DIR__ . '/openapi.yaml');
// 1. List endpoints (pagination)
$summary = $reader->getEndpointsSummary(page: 1, limit: 20);
foreach ($summary as $endpointSummaryDto) {
echo $endpointSummaryDto->method . ' ' . $endpointSummaryDto->path . PHP_EOL;
}
// 2. Get details for a specific endpoint
$detail = $reader->getEndpointDetails('/users/{id}', 'GET');
// 3. Get schema definition
$schema = $reader->getSchema('User');
// 4. Search endpoints by path or description
$results = $reader->search('user');
Supported HTTP methods
When building the endpoint list (getEndpointsSummary), the following HTTP methods are taken into account:
GETPOSTPUTPATCHDELETEOPTIONSHEADTRACE
YAML support
YAML files (*.yaml / *.yml) are supported via the YamlSpecLoader class.
Default behaviour:
- If the
yaml_parse_filefunction (PECLyamlextension) is available, it is used to read and parse the YAML file. - If parsing fails or the
yamlextension is not installed, the loader:- tries to locate a neighbouring JSON file with the same name (e.g.
openapi.yaml→openapi.json), - uses
JsonSpecLoaderas a fallback mechanism.
- tries to locate a neighbouring JSON file with the same name (e.g.
- If neither the YAML file nor the JSON file can be successfully read, a
FileNotReadableExceptionis thrown.
This allows the library to work consistently both with and without the yaml extension.
You can also inject a custom YAML parser (e.g. based on another library) by passing a callback as the second argument to YamlSpecLoader.
Architecture overview
Public contract of the library:
AdachSoft\OpenApiReader\OpenApiReaderInterface
Default implementation:
AdachSoft\OpenApiReader\OpenApiReader- created via
AdachSoft\OpenApiReader\Factory\OpenApiReaderFactory
Main internal components (marked as @internal, usually not used directly by consumers):
- Specification loaders:
Loader\SpecLoaderInterfaceLoader\JsonSpecLoaderLoader\YamlSpecLoader
- Data cleaning:
Cleaner\DataCleanerInterfaceCleaner\EmptyValueDataCleaner(recursively removesnullvalues and empty arrays[])
- Builders:
Builder\EndpointBuilderInterface/Builder\EndpointBuilderBuilder\SchemaBuilderInterface/Builder\SchemaBuilder
- DTOs and collections:
Dto\EndpointSummaryDto,Dto\EndpointDetailDto,Dto\SchemaDefinitionDto,Dto\ParameterDto,Dto\RequestBodyDto,Dto\ResponseDtoCollection\EndpointSummaryCollection,Collection\ParameterCollection,Collection\ResponseCollection,Collection\OpenApiSpecDataCollection
As a consumer, you should typically use:
OpenApiReaderFactoryto create a reader instance,OpenApiReaderInterface/OpenApiReaderas the main API.
Advanced configuration
You can build a custom configuration (e.g. your own cleaner or loaders) and pass it into the factory:
use AdachSoft\OpenApiReader\Builder\EndpointBuilder;
use AdachSoft\OpenApiReader\Builder\SchemaBuilder;
use AdachSoft\OpenApiReader\Cleaner\EmptyValueDataCleaner;
use AdachSoft\OpenApiReader\Factory\OpenApiReaderFactory;
use AdachSoft\OpenApiReader\Loader\JsonSpecLoader;
use AdachSoft\OpenApiReader\Loader\YamlSpecLoader;
$dataCleaner = new EmptyValueDataCleaner();
$jsonLoader = new JsonSpecLoader();
$yamlLoader = new YamlSpecLoader($jsonLoader); // default parser uses yaml_parse_file if available
$factory = new OpenApiReaderFactory(
specLoaders: [$jsonLoader, $yamlLoader],
endpointBuilder: new EndpointBuilder($dataCleaner),
schemaBuilder: new SchemaBuilder($dataCleaner),
);
$reader = $factory->createFromFile(__DIR__ . '/openapi.yaml');
If you need full control over YAML parsing, you can inject your own parser callback:
use AdachSoft\OpenApiReader\Loader\YamlSpecLoader;
$parser = static function (string $filePath): array {
// parse YAML using your preferred library
};
$yamlLoader = new YamlSpecLoader($jsonLoader, $parser);
Tests and quality
The project includes a PHPUnit 13 test suite:
vendor/bin/phpunit
Additional tools:
PHPStan (static analysis):
composer run stanPHP CS Fixer (code style):
composer run cs:check # check only composer run cs:fix # apply fixes
License
This project is released under the MIT license (see composer.json).