reinfi / openapi-models
Generate PHP models from openapi definition
Installs: 2 437
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 1
Open Issues: 1
Requires
- php: >= 8.2
- marcelthole/openapi-merge: ^2.0
- michaelpetri/typed-input: ^1.3
- nette/php-generator: ^4.1
- ocramius/package-versions: ^2.8
- php-di/php-di: ^7.0
- php-openapi/openapi: ^2.0
- symfony/console: ^6.0 | ^7.0
- webmozart/glob: ^4.6
Requires (Dev)
- dg/bypass-finals: ^1.5
- mikey179/vfsstream: ^1.6
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11.0
- symplify/easy-coding-standard: ^12.1.8
- dev-main
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- v0.7.0
- v0.6.1
- v0.6.0
- v0.5.7
- v0.5.6
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.0
- v0.1.0
- dev-allow-array-of-array
- dev-one-of-array
- dev-fix-junit-formatter
- dev-support-all-of-in-dictionary
- dev-fix-adding-class-before-imports
- dev-ignore-required-as-bool
- dev-fix-import-order
- dev-support-datetime-array
- dev-support-datetime
- dev-seperate-schema-into-own-namespace
- dev-add-tests
This package is auto-updated.
Last update: 2024-10-15 19:39:07 UTC
README
This library does not provide any client or server implementation for your open api definition. It just generates models defined in your schemas.
This library may not be feature complete for all openapi definition. If you miss a feature, just file an issue.
Why?
There are some common open api generators like JanePHP or OpenAPI Generator but they create old PHP Syntax due to their backwards compatibility.
- No readonly objects
- No constructor promoted properties
- No typed properties, except PHPDoc
That is the reason why this library exists to simply generate models with new PHP syntax.
Install
Use composer to install it as development dependency.
composer require --dev reinfi/openapi-models
Usage
Default configuration file is openapi-models.php
.
Your configuration file should return an array with the following settings:
return [ 'paths' => [__DIR__ . '/spec'], # array of path to check for openapi files 'outputPath' => __DIR__ . '/output', # output directory 'namespace' => 'Api', # namespace for generated classes, can be empty 'clearOutputDirectory' => true, # to remove all files in output directory, default is false 'dateTimeAsObject' => false, # date/date-time definition will be `string` otherwise `DateTimeInterface`. 'dateTimeFormat' => 'Y-m-d H:i:s', # format for serialize of date time fields ];
Generate
To generate your files just run php vendor/bin/openapi-models generate
.
If you like to store your configuration somewhere else you need to provide the file name to the command.
php vendor/bin/openapi-models generate --config spec/openapi-models.php
Validate
To validate your files, i.e. if the specification matches the generated files, you can run php vendor/bin/openapi-models validate
.
Make sure you use the same configuration file as used for the generate command.
Types
This library does not support mixed
type from open-api specification.
Therefore, you always have to set a type in your specification.
Date or DateTime
The following schema has date/date-time properties.
components: schemas: Test1: type: object required: - date properties: date: type: string format: date dateTime: type: string format: date-time
The default is to generate the fields as string
-type because this would not require
any logic for serialization of the class.
You can change the configuration dateTimeAsObject
to true
and then these fields will be of type DateTimeInterface
.
A serialization function is added to these classes to support native json_encode
. If you do not use native json_encode you
may need to provide an own implementation to fulfill open api specifications.
The default format for a date
is Y-m-d
, for a date-time
-type is Y-m-d\TH:i:sP
(RFC3339), but you can configure it.
'dateFormat' => 'Y-m-d', 'dateTimeFormat' => 'Y-m-d H:i:s',
This is just for convenience if you do not want to deal with time zones or just need different formats.