jane-php/open-api-common
Shared utilities and models used by Jane PHP for OpenAPI/Swagger code generation and runtime support. Provides common components like normalizers, reference handling, and helpers for building OpenAPI-based API clients and servers in PHP.
Installation Add the package via Composer:
composer require jane-php/open-api-common
No publisher or service provider is required—this is a pure utility library.
First Use Case
Use the Jane\OpenApi\Common\OpenApi class to generate or parse OpenAPI specs:
use Jane\OpenApi\Common\OpenApi;
$openApi = new OpenApi();
$openApi->info->title = 'My API';
$openApi->info->version = '1.0.0';
echo $openApi->toJson(); // Outputs JSON representation
Where to Look First
Jane\OpenApi\Common\OpenApi (main spec container), Jane\OpenApi\Common\Components (reusable components).Jane\OpenApi\Common\Factory (for creating spec objects), Jane\OpenApi\Common\Normalizer (for JSON serialization).Generating OpenAPI Specs Dynamically
Use the OpenApi class to build specs programmatically:
$openApi = new OpenApi();
$openApi->info->title = 'User API';
$openApi->servers[] = ['url' => 'https://api.example.com/v1'];
// Add a path
$openApi->paths['/users'] = new \Jane\OpenApi\Common\PathItem();
$openApi->paths['/users']->get = new \Jane\OpenApi\Common\Operation();
$openApi->paths['/users']->get->summary = 'List users';
Reusing Components
Define reusable schemas, responses, or security schemes in components:
$openApi->components->schemas['User'] = new \Jane\OpenApi\Common\Schema();
$openApi->components->schemas['User']->type = 'object';
$openApi->components->schemas['User']->properties['id'] = new \Jane\OpenApi\Common\Property();
$openApi->components->schemas['User']->properties['id']->type = 'integer';
Integration with Laravel
return response()->json($openApi->toArray());
respect/validation or Laravel’s built-in validation to enforce OpenAPI schemas.Parsing Existing Specs Load an existing OpenAPI spec from JSON:
$json = file_get_contents('api-spec.json');
$openApi = OpenApi::fromJson($json);
OpenApi class for dependency injection:
$this->app->bind(OpenApi::class, function () {
return new OpenApi();
});
$spec = Cache::remember('openapi-spec', now()->addHours(1), function () {
return $openApi->toArray();
});
version field in info to manage API versions explicitly.No Built-in Validation
The library does not validate OpenAPI specs against the OpenAPI standard. Use tools like zircote/swagger-php for validation:
composer require zircote/swagger-php
use Zircote\Swagger\Validator;
$validator = new Validator();
$validator->validate($openApi->toArray());
Immutable Properties
Some properties (e.g., OpenApi->info) are objects, not arrays. Direct array assignment won’t work:
// ❌ Wrong
$openApi->info = ['title' => 'API']; // Fails
// ✅ Correct
$openApi->info->title = 'API';
Namespace Conflicts
The package uses Jane\OpenApi\Common namespace. Ensure no naming collisions with other OpenAPI libraries (e.g., darkghosthunter/openapi).
JSON Serialization Quirks
toJson() or toArray() for output. Avoid json_encode($openApi) directly.Jane\OpenApi\Common\Normalizer if needed.dd($openApi->toArray()) to inspect the generated spec.barryvdh/laravel-ide-helper) can highlight missing properties in autocompletion.info) are missing.Custom Normalizers
Extend Jane\OpenApi\Common\Normalizer\NormalizerInterface to modify serialization:
class CustomNormalizer implements NormalizerInterface {
public function normalize($object, $format = null, array $context = []) {
// Custom logic
}
}
Event Listeners Attach listeners to spec generation (e.g., log changes):
$openApi->addListener('postGenerate', function ($spec) {
Log::info('Spec generated:', $spec->toArray());
});
Hybrid Specs Merge dynamic and static specs:
$dynamicSpec = new OpenApi();
$staticSpec = OpenApi::fromJson(file_get_contents('static-spec.json'));
$mergedSpec = $dynamicSpec->merge($staticSpec);
components or paths for complex APIs—it may bloat memory.paths or components) when possible.How can I help you explore Laravel packages today?