Skip to content

LongTermSupport/php-openapi-generator

Repository files navigation

LTS PHP OpenAPI Generator

Hard fork of janephp/janephp with OpenAPI 3.1 support.

A set of libraries to generate PHP models and API clients from OpenAPI 3.1 specifications.

Why this fork?

Jane PHP does not support OpenAPI 3.1 (janephp/janephp#759). OAS 3.1 has been the current standard since February 2021, and many API providers publish 3.1 specs exclusively. The upstream maintainers have indicated they welcome PRs but have no roadmap for 3.1 support.

This fork adds:

  • Full OpenAPI 3.1 support — handles type as array (e.g. ["string", "null"]), nullable via type arrays, const, $schema, and other 3.1 features
  • PHP 8.4 — requires and targets modern PHP
  • PHPUnit 11 — test suite fully updated to PHPUnit 11 conventions
  • Strict OpenAPI spec validation via lts/strict-openapi-validator

Requirements

  • PHP 8.4+

Installation

composer require lts/php-openapi-generator

Usage

Generate a client from an OpenAPI 3.1 spec:

vendor/bin/php-openapi generate --config-file .php-openapi

Example .php-openapi config:

<?php

return [
    'openapi-file' => __DIR__ . '/openapi.yaml',
    'namespace'    => 'MyApp\Client',
    'directory'    => __DIR__ . '/generated',
];

Generated-code visibility (@api / @internal)

The generator stamps a visibility docblock tag on every generated class-like (models, normalizers, endpoints, exceptions, clients, runtime templates) so that tools enforcing an @api/@internal split (e.g. PHPStan rules, Psalm, IDEs) have a marker that survives regeneration.

The default is @internal — the safe choice for a library that publishes generated SDK code without guaranteeing its stability. The consuming project controls this, two config keys:

Key Type Default Effect
api-annotation string 'internal' Default tag for all generated class-likes: 'internal', 'api', or 'none' (stamp nothing).
api-annotation-overrides list<string> [] PCRE patterns matched against each class FQCN; a match is stamped @api regardless of the default.
return [
    'openapi-file' => __DIR__ . '/openapi.yaml',
    'namespace'    => 'MyApp\Client',
    'directory'    => __DIR__ . '/generated',

    // Mark ALL generated code as public API:
    'api-annotation' => 'api',

    // …or keep the @internal default but expose SOME classes as @api:
    // 'api-annotation'           => 'internal',
    // 'api-annotation-overrides' => ['#^MyApp\\\\Client\\\\Model\\\\#'],

    // …or stamp nothing and own the policy yourself:
    // 'api-annotation' => 'none',
];

A class that already carries @api or @internal is left untouched (idempotent).

Symfony Integration

To register the generate command in a Symfony application, add it as a console command in your services config:

// config/services.php
use LongTermSupport\OpenApiGenerator\Component\OpenApiCommon\Console\Command\GenerateCommand;
use LongTermSupport\OpenApiGenerator\Component\OpenApiCommon\Console\Loader\ConfigLoader;
use LongTermSupport\OpenApiGenerator\Component\OpenApiCommon\Console\Loader\OpenApiMatcher;
use LongTermSupport\OpenApiGenerator\Component\OpenApiCommon\Console\Loader\SchemaLoader;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $container): void {
    $container->services()
        ->set('php_openapi.generate_command', GenerateCommand::class)
        ->args([
            new ConfigLoader(),
            new SchemaLoader(),
            new OpenApiMatcher(),
        ])
        ->tag('console.command');
};

Then run:

bin/console php-openapi:generate --config-file .php-openapi

Upstream

Maintained by

Long Term Support Ltdjoseph@ltscommerce.dev

Credits

License

MIT — see LICENSE. Original work copyright © 2016–2017 Joel Wurtz.

About

No description, website, or topics provided.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors