michaelalexeevweb / openapi-php-dto-generator
Generate PHP DTOs from OpenAPI and validate incoming HTTP requests against OpenAPI schema.
Package info
github.com/michaelalexeevweb/openapi-php-dto-generator
pkg:composer/michaelalexeevweb/openapi-php-dto-generator
Fund package maintenance!
Requires
- php: ^8.4
- symfony/console: ^7.4
- symfony/http-foundation: ^7.4
- symfony/mime: ^7.4
- symfony/yaml: ^7.4
- twig/twig: ^3.0
Requires (Dev)
- ergebnis/phpstan-rules: ^2.13
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5
- dev-master
- 2.0.28
- 2.0.27
- 2.0.26
- 2.0.25
- 2.0.24
- 2.0.23
- 2.0.22
- 2.0.21
- 2.0.20
- 2.0.19
- 2.0.18
- 2.0.17
- 2.0.16
- 2.0.15
- 2.0.14
- 2.0.13
- 2.0.12
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2026-04-30 22:48:01 UTC
README
Generate PHP DTOs from OpenAPI and validate incoming HTTP requests against OpenAPI schema.
Stop writing boilerplate PHP data transfer objects by hand. This library reads your OpenAPI 3.x YAML specification and automatically generates strictly-typed, immutable PHP 8.4 DTO classes. On top of that, it provides runtime services to deserialize Symfony Request objects into those DTOs, validate HTTP requests against the original OpenAPI schema rules (OpenAPI request validation), and normalize them back to arrays or JSON — all in one package.
Features
- 🚀 Code generation — generate immutable PHP DTO classes directly from OpenAPI 3.0 / 3.1 YAML specs
- ✅ OpenAPI request validation — validate HTTP requests against OpenAPI constraints (required fields, types, enums, formats, etc.)
- 🔄 Normalization — convert DTOs to plain arrays or JSON, with or without validation
- 📦 Symfony Request support — deserialize Symfony
Requestobjects directly into typed PHP DTOs - 🔒 Immutable by design — all generated classes are read-only value objects
- ⚡ Supports OpenAPI 3.0.x and 3.1.x
Table of Contents
Installation
composer require michaelalexeevweb/openapi-php-dto-generator:^2.0.28
Requirements
- PHP 8.4+
- Symfony 7.4 components (
console,http-foundation,mime,yaml)
Quick Start
- Generate DTOs from your OpenAPI YAML spec
- Deserialize and validate an incoming HTTP request into a generated DTO
- Validate and normalize the DTO for response
use OpenapiPhpDtoGenerator\Service\DtoDeserializer; use OpenapiPhpDtoGenerator\Service\DtoNormalizer; use Symfony\Component\HttpFoundation\Request; use YourApp\Generated\UserPostRequest; // generated DTO from OpenAPI spec use YourApp\Generated\UserViewResponse; // generated DTO from OpenAPI spec $deserializer = new DtoDeserializer(); $normalizer = new DtoNormalizer(); /** @var Request $request */ // request: deserialize -> validate $requestDto = $deserializer->deserialize($request, UserPostRequest::class); // response: validate -> normalize $responseData = $normalizer->validateAndNormalizeToArray($requestDto); // response: normalize without validation for faster response $responseData = $normalizer->toArray(new UserViewResponse(name: 'John', surname: 'Doe'));
Usage
Add script in your project composer.json
{
"scripts": {
"openapi:generate-dto": "php vendor/michaelalexeevweb/openapi-php-dto-generator/bin/console openapi:generate-dto"
}
}
Generate DTO classes from YAML OpenAPI spec
Use one canonical command:
composer openapi:generate-dto -- \ --file=OpenApiExamples/test.yaml \ --directory=generated/test \ --namespace=Generated\\Test \ --dto-generator-directory=Common \ --dto-generator-namespace=Generated\\Common
Parameters:
| Option | Alias | Required | Description |
|---|---|---|---|
--file |
-f |
✅ | Path to OpenAPI YAML spec file |
--directory |
-d |
✅ | Output directory for generated DTOs |
--namespace |
Explicit DTO namespace (derived from --directory if omitted) |
||
--dto-generator-directory |
Copy runtime services into this directory (Common by default) |
||
--dto-generator-namespace |
Explicit namespace for copied runtime services |