<p dir="auto"><em>Let's say this is not a bug report.</em></p>
<h3 dir="auto"><strong>Description</strong></h3>

<p dir="auto">A common problem with the serializer and the use of the deserialization is that it will throw errors if a wrong value is inside the given data. This stops any possibility of validation because the deserialization will not end. For information, it is a known issue of ApiPlatform. See <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="233430475" data-permission-text="Title is private" data-url="https://github.com/api-platform/core/issues/1157" data-hovercard-type="issue" data-hovercard-url="/api-platform/core/issues/1157/hovercard" href="https://github.com/api-platform/core/issues/1157">api-platform/core#1157</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="300651287" data-permission-text="Title is private" data-url="https://github.com/api-platform/core/issues/1735" data-hovercard-type="issue" data-hovercard-url="/api-platform/core/issues/1735/hovercard" href="https://github.com/api-platform/core/issues/1735">api-platform/core#1735</a> and <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="255706282" data-permission-text="Title is private" data-url="https://github.com/api-platform/core/issues/1352" data-hovercard-type="issue" data-hovercard-url="/api-platform/core/issues/1352/hovercard" href="https://github.com/api-platform/core/issues/1352">api-platform/core#1352</a>.</p>
<p dir="auto">I suggest adding a new context option that will contain an "error bag". Then we could return the object deserialized and ready for validation with the validation component, as well as a list of errors that occured in the deserialization process.</p>
<p dir="auto">This way you can return all the validation error properly.</p>
<h3 dir="auto"><strong>Example</strong></h3>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$errors = new ErrorBag();
$myObject = $serializer-&gt;deserialize($data, MyObject::class, 'json', ['error_bag' =&gt; $errors]);
$contraints = $validator-&gt;validate($myObject); // And we're happy here

// Then I can foreach on $errors and $contraints to generate a correct &amp; complete output."><pre class="notranslate"><span class="pl-s1"><span class="pl-c1">$</span>errors</span> = <span class="pl-k">new</span> <span class="pl-v">ErrorBag</span>();
<span class="pl-s1"><span class="pl-c1">$</span>myObject</span> = <span class="pl-s1"><span class="pl-c1">$</span>serializer</span>-&gt;<span class="pl-en">deserialize</span>(<span class="pl-s1"><span class="pl-c1">$</span>data</span>, <span class="pl-v">MyObject</span>::class, <span class="pl-s">'json'</span>, [<span class="pl-s">'error_bag'</span> =&gt; <span class="pl-s1"><span class="pl-c1">$</span>errors</span>]);
<span class="pl-s1"><span class="pl-c1">$</span>contraints</span> = <span class="pl-s1"><span class="pl-c1">$</span>validator</span>-&gt;<span class="pl-en">validate</span>(<span class="pl-s1"><span class="pl-c1">$</span>myObject</span>); <span class="pl-c">// And we're happy here</span>

<span class="pl-c">// Then I can foreach on $errors and $contraints to generate a correct &amp; complete output.</span></pre></div>
<h3 dir="auto"><strong>POC</strong></h3>
<p dir="auto">I tried to make this feature (it's a far more simple version than what it should actually be). It's here: <a href="https://github.com/Nek-/poc-symfony-deserialization-errors">https://github.com/Nek-/poc-symfony-deserialization-errors</a></p>
<p dir="auto">I can implement this feature if feedbacks are positive.</p>
<p dir="auto"><strong>Description</strong><br>
Add a way to make the Serializer try its best to continue denormalization to the end and collect all failures instead of stopping and throwing an exception at first failure. If anything goes wrong, the thrown exception provides all collected failures, similarly to what the Validator does.</p>
<p dir="auto"><strong>Example</strong><br>
When working on an application that exposes an API (with e.g. API Platform) and uses Value Objects to represent data, a way to process a request payload can be as follows:</p>
<ul dir="auto">
<li>Have a Data Transfer Object class that represents the payload with basic types (<code class="notranslate">int</code>, <code class="notranslate">string</code>, ...)</li>
<li>Use the Serializer to convert the payload into an instance of that DTO class</li>
<li>Use the Validator to assert that all values in the DTO are valid</li>
<li>In case it's not valid, expose the violations to the client in a 400 response</li>
<li>Otherwise, pass the DTO to other parts of the application, converting all basic values into Value Objects as needed.</li>
</ul>
<p dir="auto">I see several drawbacks though:</p>
<ul dir="auto">
<li>most validation rules are written twice: in the Value Objects and in the Validator mapping</li>
<li>it is not really type safe: a method that is passed the DTO has no guarantee that it is indeed valid (maybe it was not actually validated or it was mutated afterwards).</li>
</ul>
<p dir="auto">A possible solution I imagine is to have the Serializer denormalize the values using Value Objects directly instead of basic types. This should only require having a denormalizer for all involved objects to work. This would solve issues mentioned above but would bring another one: since current implementation of the denormalization process stops and throws an exception as soon as it fails denormalizing a single value, the API would not be able to report all violations in the payload anymore: instead it would only report the single value whose denormalization failed.</p>
<p dir="auto">With the proposed feature, the new implementation to process a request payload would be:</p>
<ul dir="auto">
<li>Have a Data Transfer Object that represents the payload with Value Objects and uses a constructor instead of public properties (leveraging <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="430118203" data-permission-text="Title is private" data-url="https://github.com/symfony/symfony/issues/30956" data-hovercard-type="pull_request" data-hovercard-url="/symfony/symfony/pull/30956/hovercard" href="https://github.com/symfony/symfony/pull/30956">#30956</a> maybe)</li>
<li>Use the Serializer to convert the payload into an instance of that DTO class</li>
<li>In case of denormalization failures, expose them to the client in a 400 response</li>
<li>Otherwise, pass the DTO to other parts of the application without any further processing</li>
</ul>