PHPackages                             michaelalexeevweb/openapi-php-dto-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. michaelalexeevweb/openapi-php-dto-generator

ActiveLibrary[API Development](/categories/api)

michaelalexeevweb/openapi-php-dto-generator
===========================================

Generate PHP DTOs from OpenAPI and validate incoming HTTP requests against OpenAPI schema.

2.1.4(3w ago)583MITPHPPHP ^8.4CI passing

Since Mar 9Pushed 1w agoCompare

[ Source](https://github.com/michaelalexeevweb/openapi-php-dto-generator)[ Packagist](https://packagist.org/packages/michaelalexeevweb/openapi-php-dto-generator)[ Fund](https://ko-fi.com/michaelalexeevweb)[ RSS](/packages/michaelalexeevweb-openapi-php-dto-generator/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (28)Versions (56)Used By (0)

OpenAPI PHP DTO Generator
=========================

[](#openapi-php-dto-generator)

[![MIT License](https://camo.githubusercontent.com/8a78bee9262773e64eea843c6ce94760fddd8123a5558b4b8a3226784e7babde/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d69636861656c616c65786565767765622f6f70656e6170692d7068702d64746f2d67656e657261746f72)](LICENSE)[![CI](https://github.com/michaelalexeevweb/openapi-php-dto-generator/actions/workflows/ci.yml/badge.svg)](https://github.com/michaelalexeevweb/openapi-php-dto-generator/actions/workflows/ci.yml)[![Latest Version](https://camo.githubusercontent.com/0376ce907e6c4f83aa93f27d249ef7a7814838373b52e14d747edd00c4f6a25f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d69636861656c616c65786565767765622f6f70656e6170692d7068702d64746f2d67656e657261746f72)](https://packagist.org/packages/michaelalexeevweb/openapi-php-dto-generator)[![PHP Version](https://camo.githubusercontent.com/c779822d331074ed5f5051097b2295ccdd8f4f76a1b85f182f5d15f94b6097e0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d69636861656c616c65786565767765622f6f70656e6170692d7068702d64746f2d67656e657261746f72)](https://packagist.org/packages/michaelalexeevweb/openapi-php-dto-generator)[![Total Downloads](https://camo.githubusercontent.com/9b4a5272fec4bde866a2b5f82b764e4bea537be5fdc5e36370feb60e3010e4eb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d69636861656c616c65786565767765622f6f70656e6170692d7068702d64746f2d67656e657261746f72)](https://packagist.org/packages/michaelalexeevweb/openapi-php-dto-generator)

**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
--------

[](#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 `Request` objects 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
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Requirements](#requirements)
- [Quick Start](#quick-start)
- [Generate DTOs](#generate-dto-classes-from-yaml-openapi-spec)
- [Validate &amp; Normalize](#validate-and-normalize-generated-dtos)
- [CLI Commands](#cli-commands)

Installation
------------

[](#installation)

```
composer require michaelalexeevweb/openapi-php-dto-generator:^2.2.9
```

Requirements
------------

[](#requirements)

- PHP 8.4+
- Symfony 7.4 components (`console`, `http-foundation`, `mime`, `yaml`)

Quick Start
-----------

[](#quick-start)

1. **Generate DTOs** from your OpenAPI YAML spec
2. **Deserialize** and **validate** an incoming HTTP request into a generated DTO
3. **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
-----

[](#usage)

### Add script in your project `composer.json`

[](#add-script-in-your-project-composerjson)

```
{
  "scripts": {
    "openapi:generate-dto": "php vendor/michaelalexeevweb/openapi-php-dto-generator/bin/console openapi:generate-dto"
  }
}
```

### Generate DTO classes from YAML OpenAPI spec

[](#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:

OptionAliasRequiredDescription`--file``-f`✅Path to OpenAPI spec file (YAML or JSON)`--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 servicesValidation Notes
----------------

[](#validation-notes)

A few behaviours worth knowing when validating against the schema:

- **`type: array` means a JSON array (list).** A value passes only when it is a PHP list (sequential integer keys from `0`). An associative array is treated as a JSON object, not an array — so a getter returning `array_filter(...)` (which may leave non-contiguous keys) should wrap the result in `array_values(...)`.
- **`oneOf` / `anyOf` pick the first matching branch.** Branches are tried in declaration order and the first one that validates wins. When several branches accept the same input (e.g. `oneOf: [string, integer]` given `"123"`), order your schema branches from most specific to least specific.

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance97

Actively maintained with recent releases

Popularity16

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

54

Last Release

22d ago

Major Versions

1.2.3 → 2.0.02026-04-06

PHP version history (2 changes)1.0.0PHP ^8.3

2.0.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/09ca088fdfe3b6be48755e620884b445563e33347498e381a436a257068e5ff8?d=identicon)[michaelalexeevweb](/maintainers/michaelalexeevweb)

---

Top Contributors

[![michaelalexeevweb](https://avatars.githubusercontent.com/u/39598000?v=4)](https://github.com/michaelalexeevweb "michaelalexeevweb (74 commits)")

---

Tags

apideserializationdtodto-generatoropenapiopenapi-validationphprequest-validationserializersymfonyvalidationphpapisymfonyvalidationopenapiserializerdeserializationdtorequest validationdto-generatoropenapi-validation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/michaelalexeevweb-openapi-php-dto-generator/health.svg)

```
[![Health](https://phpackages.com/badges/michaelalexeevweb-openapi-php-dto-generator/health.svg)](https://phpackages.com/packages/michaelalexeevweb-openapi-php-dto-generator)
```

###  Alternatives

[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M196](/packages/sulu-sulu)[drupal/core

Drupal is an open source content management platform powering millions of websites and applications.

19564.8M1.6k](/packages/drupal-core)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6941.5M396](/packages/drupal-core-recommended)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.4M514](/packages/shopware-core)[open-dxp/opendxp

Content &amp; Product Management Framework (CMS/PIM)

9317.2k55](/packages/open-dxp-opendxp)[chameleon-system/chameleon-base

The Chameleon System core.

1027.9k4](/packages/chameleon-system-chameleon-base)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
