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

Serialization of objects with missing attribute errors #1525

Closed boesing closed 7 months ago

boesing commented 7 months ago
Q A
Bug report? yes
BC Break report? yes

With 3.28.0, something has changed so that we now end up having an error at runtime due to missing attributes. We do use OpenAPI attributes to generate API documentation during CI and since we do not need these attributes on production, we do have these in require-dev section of composer. That worked pretty well until 3.27.0 and stopped working with 3.28.0.

The problem is, that there is no check if an attribute actually exists:

https://github.com/schmittjoh/serializer/blob/f24027583f8ad3c1789bff412b039013132316ac/src/Metadata/Driver/AnnotationOrAttributeDriver.php#L331C3-L336

We had something similar with cuyz/valinor (https://github.com/CuyZ/Valinor/issues/393), but that was only related to cache files and was not affecting runtime (without caches). That was due to the fact that cuyz/valinor already kinda handled missing attributes:

https://github.com/romm/Valinor/blob/af98753ba93c2a1ff8b717a5ecfbe3424a41b99f/src/Definition/NativeAttributes.php#L34-L41

Steps required to reproduce the problem

  1. Create an object to serialize
  2. Add an attribute to the class or to a property which does not exist (I've used #[Test])
  3. Serialize that object

Expected Result

Actual Result

scyzoryck commented 7 months ago

Hi! Thanks for information :) Looks like some edge case issues that was not spotted before. Feel free to create PR with the fix. Let me know if you'd need any help with that.

Best, scyzoryck.

boesing commented 7 months ago

I'll provide a patch for this, just need to find some time. 🤙🏼