Skip to content

[Serializer][RFC] New context option to avoid throw UnexpectedValueException #28358

@Nek-

Description

@Nek-

Let's say this is not a bug report.

Description

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 api-platform/core#1157, api-platform/core#1735 and api-platform/core#1352.

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.

This way you can return all the validation error properly.

Example

$errors = new ErrorBag();
$myObject = $serializer->deserialize($data, MyObject::class, 'json', ['error_bag' => $errors]);
$contraints = $validator->validate($myObject); // And we're happy here

// Then I can foreach on $errors and $contraints to generate a correct & complete output.

POC

I tried to make this feature (it's a far more simple version than what it should actually be). It's here: https://github.com/Nek-/poc-symfony-deserialization-errors

I can implement this feature if feedbacks are positive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRFC = Request For Comments (proposals about features that you want to be discussed)Serializer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy