schmittjoh / serializer

Library for (de-)serializing data of any complexity (supports JSON, and XML)
http://jmsyst.com/libs/serializer
MIT License
2.31k stars 591 forks source link

Provide better exception message when no metadata file found #993

Open przepompownia opened 5 years ago

przepompownia commented 5 years ago
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

Steps required to reproduce the problem

  1. Create metadata file for some class, check that (de)serialization works using it
  2. Rename this file (or rename the class) and try to (de)serialize.

Expected Result

Avoid using wrong type, and (more important) add related class name (and possibly expected metadata file paths when use Yaml or Xml driver) to exception message when no metadata found.

Actual Result

Currently we get a message

TypeError : Argument 1 passed to JMS\Serializer\Context::pushClassMetadata() must be an instance of JMS\Serializer\Metadata\ClassMetadata, null given, called in /app/vendor/jms/serializer/src/JMS/Serializer/GraphNavigator.php on line 209
 /app/vendor/jms/serializer/src/JMS/Serializer/Context.php:222
 ...

that does not contain needed information. The simplest way is to use conditional (null === $metadata) breakpoint to get more information.

It is also bug due to TypeError ocurrence.

goetas commented 5 years ago

The error you are describing should be related to something else.. but I agree that would be nice to have a better message in case of no meta data... the issue is that the last metadata driver is "annotation" that means that there are "always" metadata info for a class...

przepompownia commented 5 years ago

Indeed, I forgot to emphasize that in this example I have one-piece driver chain.

goetas commented 5 years ago

Why not appending at the end of the driver chain the nulldriver?

przepompownia commented 5 years ago

NullDriver at the end of chain does not resolve the problem of metadata missing by mistake. Maybe it make sense when we want to skip some metadata files. In this case element with no metadata is quietly skipped which (depending on what we want) may be against the need for the information what metadata file not found.