Decouple property naming from PropertyCollection #53
Conversation
There is currently a bug where when you use a camelCase name for the `SerializedName` attribute which would result in the same snake_case name, that the `RawClassMetadata::renameProperty` method would throw an exception because it thinks we are trying to use the same name again. This test only acts as a reference so we can fix the actual bug.
dbu
left a comment
There was a problem hiding this comment.
thanks for this pull request. seems like a nice cleanup, that static method was indeed rather ugly.
i am slightly worried that when different parsers are given different strategies (e.g. one is forgotten and the default is taken), the parsers would add metadata to different names, leading to confusion. but i don't see a good way around that, i think we just need to document that.
can you please adjust the readme section https://github.com/liip/metadata-parser?tab=readme-ov-file#property-naming-strategy (haha, were we call it strategy when its a global static flag :-P - you actually convert that to the strategy pattern here)
| $serializedName = $this->namingStrategy->getSerializedName($reflProperty->getName()); | ||
| $property = PropertyVariationMetadata::fromReflection($reflProperty); | ||
| $classMetadata->addPropertyVariation($reflProperty->getName(), $property); | ||
| $classMetadata->addPropertyVariation($serializedName, $property); |
There was a problem hiding this comment.
oh, i vaguely remember that we struggled with different representation of the same property.
so your approach is to always use the serialized name in the metadata and never the original name?
There was a problem hiding this comment.
so your approach is to always use the serialized name in the metadata and never the original name?
That was actually already the case, the only difference now is, that the serialized name is created directly in the parser so you have more control over it instead of directly in the PropertyCollection
With this change it is now possible to pass different property naming strategies to the different parsers. This also provides more freedom when it comes to property collection handling, as you don't have to be aware that the collection itself creates the serialized name, but instead it is up to the caller to decide what serialized name should be used.
I thought about this too, and I see a few other directions we could go here
This means in all cases the Personally I think the third option is better, but also a bit weird in case the parser does not need any naming strategy (like the LiipMetadataAttributeParser) What do you think? |
|
i like the third option best. yes, we will pass the strategy even when no naming is needed, but there is no real cost involved as the strategy is injected/created only once. |
|
do you adjust this PR correspondingly? are there other things for version 2.x or should i tag the release once we have these changes? |
Yes, I will adjust this PR accordingly. After this, the last thing I want to add is the discriminator support (which is almost done, at least the metadata-parser side). However, I have an upcoming vacation, so I'm not sure if I can finish it before, but I will definitely finish it this month. |
This ensures that the same strategy is used for all parsers. Using the strategy is completely optional, so parsers can just ignore it if they don't need it.
Done via 2006b3d - Thanks for your feedback on this 🙇 |
dbu
left a comment
There was a problem hiding this comment.
great, thanks a lot! i did some tweaks with the changelog, will try to accept those changes myself and merge.
i am in no hurry to release version 2, i will wait for the discriminator support. it will be easier if its in a major version, and maybe we end up wanting to do BC breaks.
| * Adjusted code to not trigger warnings with PHP 8.4 | ||
| * Removed deprecated `getDeserializeFormat` method from date time | ||
| * Replaced `@Preferred` annotation with the `#[Preferred]` attribute | ||
| * Added a new `PropertyNamingStrategyInterface` which can be used to modify the naming strategy for properties. Previously |
There was a problem hiding this comment.
| * Added a new `PropertyNamingStrategyInterface` which can be used to modify the naming strategy for properties. Previously | |
| * Replaced `PropertyCollection::useIdenticalNamingStrategy` with the `PropertyNamingStrategyInterface`. To change the default strategy, you can now inject it into the parser instead of changing a static property. |
| * Removed deprecated `getDeserializeFormat` method from date time | ||
| * Replaced `@Preferred` annotation with the `#[Preferred]` attribute | ||
| * Added a new `PropertyNamingStrategyInterface` which can be used to modify the naming strategy for properties. Previously | ||
| this could be adjusted via the `PropertyCollection::useIdenticalNamingStrategy` method, but as this was a bit error prone, a separate |
There was a problem hiding this comment.
| this could be adjusted via the `PropertyCollection::useIdenticalNamingStrategy` method, but as this was a bit error prone, a separate |
| * Replaced `@Preferred` annotation with the `#[Preferred]` attribute | ||
| * Added a new `PropertyNamingStrategyInterface` which can be used to modify the naming strategy for properties. Previously | ||
| this could be adjusted via the `PropertyCollection::useIdenticalNamingStrategy` method, but as this was a bit error prone, a separate | ||
| interface was introduced. This new interface also comes with two implementations: |
There was a problem hiding this comment.
| interface was introduced. This new interface also comes with two implementations: | |
| The library provides two implementations for the property naming strategy: |
| this could be adjusted via the `PropertyCollection::useIdenticalNamingStrategy` method, but as this was a bit error prone, a separate | ||
| interface was introduced. This new interface also comes with two implementations: | ||
| * `IdenticalPropertyNamingStrategy` | ||
| * `SnakeCasePropertyNamingStrategy`. |
There was a problem hiding this comment.
| * `SnakeCasePropertyNamingStrategy`. | |
| * `SnakeCasePropertyNamingStrategy` (default). |
|
i applied the changelog changes and merged the branch. thanks a lot for this contribution ! |
While working on discriminator support, I stumbled upon a bug where it was impossible to force a
camelCasename for a property.So using a class like this:
would result in the following error
With the changes I did in this PR, it is now possible to pass different property naming strategies to the different parsers.
Besides fixing the bug at hand, this also provides more freedom when it comes to property collection handling, as you don't have to be aware that the collection itself creates the serialized name, but instead it is up to the caller to decide what serialized name should be used.