<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Antonio Turdo</title>
    <description>The latest articles on DEV Community by Antonio Turdo (@antonioturdo).</description>
    <link>https://dev.to/antonioturdo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3972922%2F742cec2a-6c98-420f-a5f2-a59eb61ac6c2.png</url>
      <title>DEV Community: Antonio Turdo</title>
      <link>https://dev.to/antonioturdo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/antonioturdo"/>
    <language>en</language>
    <item>
      <title>Generating JSON Schema from PHP DTOs with Symfony Serializer awareness</title>
      <dc:creator>Antonio Turdo</dc:creator>
      <pubDate>Sun, 07 Jun 2026 19:08:20 +0000</pubDate>
      <link>https://dev.to/antonioturdo/generating-json-schema-from-php-dtos-with-symfony-serializer-awareness-459o</link>
      <guid>https://dev.to/antonioturdo/generating-json-schema-from-php-dtos-with-symfony-serializer-awareness-459o</guid>
      <description>&lt;p&gt;A PHP project that serializes and deserializes DTOs often needs a JSON Schema for them — for an LLM's structured output, for API documentation, or to validate incoming payloads. &lt;/p&gt;

&lt;p&gt;Writing it by hand works until the code changes. With Symfony Serializer, the JSON shape can shift without the PHP types changing at all — a new serialization group, a &lt;code&gt;#[SerializedName]&lt;/code&gt;, a discriminator — and the hand-written schema no longer matches what the serializer produces.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/antonioturdo/json-schema-extractor" rel="noopener noreferrer"&gt;json-schema-extractor&lt;/a&gt; is a PHP library that generates JSON Schema from DTOs by reading the metadata they already carry — native types, PHPDoc, and serializer configuration — so the schema stays in sync with the code. It supports plain &lt;code&gt;json_encode&lt;/code&gt;/&lt;code&gt;JsonSerializable&lt;/code&gt; and Symfony Serializer; this article focuses on the latter.&lt;/p&gt;




&lt;h2&gt;
  
  
  The problem in concrete terms
&lt;/h2&gt;

&lt;p&gt;Take a simple DTO:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderSummary&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="na"&gt;#[Groups(['public'])]&lt;/span&gt;
        &lt;span class="na"&gt;#[SerializedName('order_id')]&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

        &lt;span class="na"&gt;#[Groups(['public'])]&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;Money&lt;/span&gt; &lt;span class="nv"&gt;$total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

        &lt;span class="na"&gt;#[Groups(['internal'])]&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$internalNote&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When Symfony Serializer renders this with the &lt;code&gt;public&lt;/code&gt; group, the JSON output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="nl"&gt;"order_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ORD-1042"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="nl"&gt;"total"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4999&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"currency"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"EUR"&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;internalNote&lt;/code&gt; is absent. &lt;code&gt;id&lt;/code&gt; is renamed. &lt;code&gt;total&lt;/code&gt; is a nested object. A hand-written schema that matches this needs to know about groups, serialized names, and how &lt;code&gt;Money&lt;/code&gt; is normalized. If any of those change, you update them in two places.&lt;/p&gt;




&lt;h2&gt;
  
  
  How the library works
&lt;/h2&gt;

&lt;p&gt;The extraction pipeline has four phases, each with a clear responsibility:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Discover&lt;/strong&gt; — &lt;code&gt;ReflectionDiscoverer&lt;/code&gt; reads the class with reflection: property names, native PHP types, visibility. No dependencies required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Enrich&lt;/strong&gt; — one or more enrichers augment the PHP model with metadata reflection alone can't see. &lt;code&gt;PhpStanEnricher&lt;/code&gt; and &lt;code&gt;PhpDocumentorEnricher&lt;/code&gt; both read PHPDoc (&lt;code&gt;@var list&amp;lt;string&amp;gt;&lt;/code&gt;, &lt;code&gt;@var array{name: string, age: int}&lt;/code&gt;, generics, descriptions, deprecation), via different parsers. &lt;code&gt;SymfonyValidationEnricher&lt;/code&gt; maps Symfony Validator constraints (&lt;code&gt;NotBlank&lt;/code&gt;, &lt;code&gt;Length&lt;/code&gt;, &lt;code&gt;Range&lt;/code&gt;…) to their JSON Schema equivalents — appropriate when the application actually validates the objects against those constraints, so the schema's guarantees hold for the real data. Enrichers are optional and composable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Project&lt;/strong&gt; — a serialization strategy converts the enriched PHP model into the &lt;em&gt;serialized shape&lt;/em&gt;: the JSON-facing view of the class. The Symfony Serializer support is implemented in this phase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Map&lt;/strong&gt; — &lt;code&gt;StandardJsonSchemaMapper&lt;/code&gt; folds the projected shape into a JSON Schema document, handling &lt;code&gt;$ref&lt;/code&gt;, reusable definitions, dialect (&lt;code&gt;draft-7&lt;/code&gt; or &lt;code&gt;2020-12&lt;/code&gt;), and union semantics.&lt;/p&gt;

&lt;p&gt;The four phases are wired together by &lt;code&gt;SchemaExtractor&lt;/code&gt;, the entry point you call to produce a schema.&lt;/p&gt;

&lt;p&gt;Each phase is defined by an interface — &lt;code&gt;DiscovererInterface&lt;/code&gt;, &lt;code&gt;EnricherInterface&lt;/code&gt;, &lt;code&gt;SerializationStrategyInterface&lt;/code&gt;, &lt;code&gt;JsonSchemaMapperInterface&lt;/code&gt; — so every component can be swapped or extended. You can plug in your own discoverer, enricher, strategy, or mapper without touching the rest.&lt;/p&gt;




&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Install the library, then add only the optional packages your chosen components need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require zeusi/json-schema-extractor

&lt;span class="c"&gt;# optional, depending on what you enable:&lt;/span&gt;
composer require phpstan/phpdoc-parser              &lt;span class="c"&gt;# for PhpStanEnricher&lt;/span&gt;
composer require phpdocumentor/reflection-docblock  &lt;span class="c"&gt;# for PhpDocumentorEnricher&lt;/span&gt;
composer require symfony/validator                  &lt;span class="c"&gt;# for SymfonyValidationEnricher&lt;/span&gt;
composer require symfony/serializer                 &lt;span class="c"&gt;# for SymfonySerializerStrategy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The core package itself has no mandatory dependencies.&lt;/p&gt;




&lt;h2&gt;
  
  
  A minimal extractor
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Discoverer\ReflectionDiscoverer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Enricher\PhpStanEnricher&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Mapper\StandardJsonSchemaMapper&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\SchemaExtractor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Serialization\JsonEncodeSerializationStrategy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$extractor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SchemaExtractor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReflectionDiscoverer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PhpStanEnricher&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JsonEncodeSerializationStrategy&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StandardJsonSchemaMapper&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$extractor&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderSummary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives you a schema based on native PHP types and PHPDoc. &lt;code&gt;JsonEncodeSerializationStrategy&lt;/code&gt; is the right choice when your JSON is produced by &lt;code&gt;json_encode()&lt;/code&gt; or &lt;code&gt;JsonSerializable&lt;/code&gt; (in that case, the shape is read from &lt;code&gt;jsonSerialize()&lt;/code&gt;'s return type and PHPDoc, since its body is opaque to static analysis).&lt;/p&gt;

&lt;p&gt;For Symfony Serializer, you swap the strategy.&lt;/p&gt;




&lt;h2&gt;
  
  
  Adding Symfony Serializer awareness
&lt;/h2&gt;

&lt;p&gt;To make the schema follow Symfony Serializer instead of &lt;code&gt;json_encode()&lt;/code&gt;, swap in &lt;code&gt;SymfonySerializerStrategy&lt;/code&gt;. Runtime serializer context — serialization groups, for example — is passed to &lt;code&gt;extract()&lt;/code&gt; through an &lt;code&gt;ExtractionContext&lt;/code&gt;, which is optional:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Symfony\Component\Serializer\Mapping\Loader\AttributeLoader&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Context\ExtractionContext&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Context\SymfonySerializerContext&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Serialization\SymfonySerializerStrategy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$strategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SymfonySerializerStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ClassMetadataFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AttributeLoader&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$extractor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SchemaExtractor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReflectionDiscoverer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PhpStanEnricher&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
    &lt;span class="nv"&gt;$strategy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StandardJsonSchemaMapper&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Optional: a context carrying the runtime serializer settings (here, the "public" group).&lt;/span&gt;
&lt;span class="nv"&gt;$context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ExtractionContext&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SymfonySerializerContext&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="s1"&gt;'groups'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'public'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;]));&lt;/span&gt;

&lt;span class="nv"&gt;$schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$extractor&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderSummary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$context&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For &lt;code&gt;OrderSummary&lt;/code&gt; with the &lt;code&gt;public&lt;/code&gt; group, the schema matches what Symfony Serializer produces: &lt;code&gt;internalNote&lt;/code&gt; is absent, and &lt;code&gt;id&lt;/code&gt; appears as &lt;code&gt;order_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The strategy reads the same inputs Symfony Serializer reads at runtime — both the attributes declared on the DTO and the options passed in the serializer context (the array shown above, where &lt;code&gt;groups&lt;/code&gt; lives):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;#[SerializedName]&lt;/code&gt; / name converters&lt;/strong&gt; → property keys are renamed in the schema.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serialization groups&lt;/strong&gt; (&lt;code&gt;groups&lt;/code&gt; context option) → only properties in the selected groups appear.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ignored_attributes&lt;/code&gt;&lt;/strong&gt; (context option) → the listed properties are excluded.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;attributes&lt;/code&gt;&lt;/strong&gt; (context option) → restricts the schema to the listed attributes, with per-property nested views for class-backed ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Discriminator maps&lt;/strong&gt; (&lt;code&gt;#[DiscriminatorMap]&lt;/code&gt;) → a base type expands to a &lt;code&gt;oneOf&lt;/code&gt; over its mapped subtypes, each tagged with the discriminator field; a concrete subtype is a single object with that field fixed to its key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Known normalizers&lt;/strong&gt; (by type) → &lt;code&gt;DateTimeInterface&lt;/code&gt; becomes &lt;code&gt;{ type: string, format: date-time }&lt;/code&gt;, Symfony UIDs become &lt;code&gt;{ type: string, format: uuid }&lt;/code&gt;, and so on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;skip_null_values&lt;/code&gt;&lt;/strong&gt; (context option) → nullable properties become optional in the schema.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What it does not model
&lt;/h2&gt;

&lt;p&gt;The strategy reads &lt;strong&gt;static metadata&lt;/strong&gt; — attributes and types — so it cannot mirror everything Symfony Serializer does at runtime. It is not a 1:1 mapping of the serializer's behaviour. The gaps worth knowing about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;preserve_empty_objects&lt;/code&gt;&lt;/strong&gt; (context option) → serializes an empty collection as &lt;code&gt;{}&lt;/code&gt; instead of &lt;code&gt;[]&lt;/code&gt;; whether it applies depends on the runtime value, which static analysis can't see.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;skip_uninitialized_values&lt;/code&gt;&lt;/strong&gt; (context option) → omits typed properties that were never assigned; that's a fact about the object instance at runtime, not about the class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom normalizers&lt;/strong&gt; outside the known set → their output shape is defined in application code, so it can't be inferred.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;max_depth_handler&lt;/code&gt; / &lt;code&gt;circular_reference_handler&lt;/code&gt;&lt;/strong&gt; (context options) → callables that replace a node when the depth limit or a cycle is hit; their output is arbitrary application code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;#[MaxDepth]&lt;/code&gt;&lt;/strong&gt; → not modeled as a tightened schema, and doesn't need to be: recursion is already broken with a &lt;code&gt;$ref&lt;/code&gt;, and a depth-bounded payload is a valid instance of that (looser) recursive schema.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interfaces / polymorphic base types&lt;/strong&gt; → an interface has no single concrete shape, so it can't be resolved on its own. A Symfony &lt;code&gt;#[DiscriminatorMap]&lt;/code&gt; turns it into a &lt;code&gt;oneOf&lt;/code&gt; when the payload carries a type field; otherwise a custom strategy or enricher can supply the concrete shapes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When a case isn't covered, the serialization strategy is the extension point: implement &lt;code&gt;SerializationStrategyInterface&lt;/code&gt;, or decorate &lt;code&gt;SymfonySerializerStrategy&lt;/code&gt;, and handle it there.&lt;/p&gt;




&lt;h2&gt;
  
  
  Reusable definitions and $ref
&lt;/h2&gt;

&lt;p&gt;By default, class-backed nested types are emitted once under &lt;code&gt;definitions&lt;/code&gt; (Draft-7) or &lt;code&gt;$defs&lt;/code&gt; (2020-12) and referenced with &lt;code&gt;$ref&lt;/code&gt; everywhere they are used. If you prefer the nested schemas expanded at the point of use instead, switch to &lt;code&gt;ClassReferenceStrategy::Inline&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Mapper\ClassReferenceStrategy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Mapper\StandardJsonSchemaMapper&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Zeusi\JsonSchemaExtractor\Mapper\StandardJsonSchemaMapperOptions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$mapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StandardJsonSchemaMapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StandardJsonSchemaMapperOptions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;classReferenceStrategy&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ClassReferenceStrategy&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Inline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Circular references are handled automatically either way: a self-referential class produces a &lt;code&gt;$ref&lt;/code&gt; back to the root (&lt;code&gt;#&lt;/code&gt;) or to the relevant definition, rather than an infinite expansion.&lt;/p&gt;




&lt;h2&gt;
  
  
  Symfony Bundle
&lt;/h2&gt;

&lt;p&gt;If you use the Symfony framework, a bundle registers the built-in components as services and wires the extractor into the container. Its main convenience is reusing the &lt;strong&gt;Symfony Serializer and Validator&lt;/strong&gt; services your application already has — so you don't assemble a &lt;code&gt;ClassMetadataFactory&lt;/code&gt;, a validator, and the strategy/enricher wiring by hand.&lt;/p&gt;

&lt;p&gt;You declare one or more extractor pipelines, choosing the strategy that matches how your app serializes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/packages/json_schema_extractor.yaml&lt;/span&gt;
&lt;span class="na"&gt;json_schema_extractor&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;default_extractor&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;api&lt;/span&gt;
  &lt;span class="na"&gt;extractors&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;api&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;enrichers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;json_schema_extractor.enricher.phpstan&lt;/span&gt;
      &lt;span class="na"&gt;serialization&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;json_schema_extractor.serialization.symfony_serializer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;SchemaExtractor&lt;/code&gt; is aliased to the default extractor, so you inject it directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;SchemaExtractor&lt;/span&gt; &lt;span class="nv"&gt;$extractor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="nv"&gt;$schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;extractor&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderSummary&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See the &lt;a href="https://github.com/antonioturdo/json-schema-extractor/blob/main/docs/symfony-bundle.md" rel="noopener noreferrer"&gt;bundle documentation&lt;/a&gt; for multiple pipelines, custom services, and the debug command.&lt;/p&gt;




&lt;h2&gt;
  
  
  Use case: structured output for LLMs
&lt;/h2&gt;

&lt;p&gt;A common use today is structured output from an LLM. Most providers accept a JSON Schema as the contract for the model's response: you generate the schema from a DTO, send it with the request, and deserialize the response back into that same DTO.&lt;/p&gt;

&lt;p&gt;Since the schema comes from the DTO you deserialize into, the two stay in sync as the DTO changes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;The library is open source under the MIT license: &lt;a href="https://github.com/antonioturdo/json-schema-extractor" rel="noopener noreferrer"&gt;github.com/antonioturdo/json-schema-extractor&lt;/a&gt;. The documentation covers all enrichers, serialization strategies, mapper options, and the Symfony bundle in detail.&lt;/p&gt;

&lt;p&gt;If you are building something with it — structured output pipelines, AsyncAPI documentation, API contract testing — feedback and contributions are welcome.&lt;/p&gt;

</description>
      <category>php</category>
      <category>symfony</category>
      <category>jsonschema</category>
    </item>
  </channel>
</rss>
