Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Create ValueObjects from simple types by calling from$TYPE for objects and JsonArray properties #2762

Merged
merged 7 commits into from
Mar 25, 2022

Conversation

mficzel
Copy link
Member

@mficzel mficzel commented Mar 24, 2022

When converting from simple types the denormalizer now checks wether the target class has a method from$TYPE and uses this to create the object. Supported method names are fromArray, fromString, fromBool, fromInt and fromFloat.

For example the following controller action would use the methods BirthDate::fromString or BirthDate::fromArray (depending on submitted data) to instantiate the $birthDate argument.

public function indexAction(BirthDate $birthDate) 

In addition the JsonArray Type is extended to support this aswell. The serialization as flow entities takes precedence over the serialization as value object flow objects that implement \JsonSerializable are still serialized as Flow Objects in JsonArray properties. While beeing a Flow feature this allows to store and restore ValueObjects in Neos Node properties aswell.

Note: This is a rebased version of #2703 from @nezaniel

Resolves: #2763
Replaces: #2703

Checklist

  • Code follows the PSR-2 coding style
  • Tests have been created, run and adjusted as needed
  • The PR is created against the lowest maintained branch

@mficzel mficzel changed the title FEATURE: Make the JsonArrayType able to handle ValueObjects FEATURE: Convert ValueObjects from simple types for flow objects and JsonArray properties Mar 24, 2022
@mficzel mficzel changed the title FEATURE: Convert ValueObjects from simple types for flow objects and JsonArray properties FEATURE: Convert ValueObjects from simple types for objects and JsonArray properties Mar 24, 2022
@mficzel mficzel changed the title FEATURE: Convert ValueObjects from simple types for objects and JsonArray properties FEATURE: Create ValueObjects from simple types by calling from$TYPE for objects and JsonArray properties Mar 24, 2022
@mficzel
Copy link
Member Author

mficzel commented Mar 24, 2022

@albe @bwaidelich @mhsdesign as you commented on the original pr and already liked it just not as a bugfix.
It would be great to get this approved for 8.0

@mficzel mficzel marked this pull request as ready for review March 24, 2022 19:10
@mficzel mficzel closed this Mar 24, 2022
@mficzel mficzel reopened this Mar 24, 2022
Copy link
Member

@kdambekalns kdambekalns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, left a "naming question".

@mficzel mficzel closed this Mar 24, 2022
@mficzel mficzel reopened this Mar 24, 2022
to be in line with the type annotations used in php code.
Copy link
Member

@kdambekalns kdambekalns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😘

Approval by reading…

One last question: Do you think this would deserve some documentation? Or should we rely on generated type converter reference?

@mficzel
Copy link
Member Author

mficzel commented Mar 24, 2022

@kdambekalns docs would be great but i am not entirely sure where to put what. Can be done later imho.
I wonder where those psalm errors come from. The mentioned lines are not changed in the pr.

@mficzel
Copy link
Member Author

mficzel commented Mar 24, 2022

Found and fixed the issues psalm detected. All green now.

@nezaniel nezaniel self-requested a review March 25, 2022 00:08
Copy link
Member

@nezaniel nezaniel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still looks good to me :)

…previous takes precedence

This should avoid problems when flow entities implement JsonSerializable. In this case we would like to serialize as an entity.
@mficzel
Copy link
Member Author

mficzel commented Mar 25, 2022

@kdambekalns @nezaniel pushed a final adjustment to handle the value object serialization in JsonArray after the check for flow-objects. That should avoid problems wit flow entities that implement JsonSerializable.

@mficzel
Copy link
Member Author

mficzel commented Mar 25, 2022

After chat with @nezaniel we agreed that the last change makes sense and improves confidence. Will merge with the existing reviews as this is a minor cosmetic change.

@mficzel mficzel merged commit 11f3a85 into neos:master Mar 25, 2022
@bwaidelich
Copy link
Member

bwaidelich commented Mar 25, 2022

Oh no, I was one hour too late..
I really like the change but I'm super sad that we now have fromInt vs fromInteger etc as this will trip folks who were used to using Value Objects with Neos.EventSourcing where we use the full names (see #2703 (comment))

@mficzel
Copy link
Member Author

mficzel commented Mar 25, 2022

@bwaidelich can you decide on this with @kdambekalns i am really not that opinionated on this and until we push release we can still adjust this.

To me the argument to use the same syntax as in typeAnnotations made more sense.

@mhsdesign
Copy link
Member

mhsdesign commented Mar 25, 2022

i guess there is no right or wrong way - but if we decided to took road fromInteger in the event sourcing already, i think we keep that course - (or we end up like php namings ^^)

@bwaidelich
Copy link
Member

OK, I'll try to rephrase my arguments for the full version.
To recap, it is about fromInt() and fromBool() (this PR) vs fromInteger() and fromBoolean() (Neos.EventSourcing):

IMO...

  • ...method names are less "technical" than type hints. While it makes sense to use short versions for the latter, we usually try to avoid abbreviations in class and method names (and I think for a good reason)
  • ...we use the full versions in our TypeHandling
  • ...the Neos.EventSourcing package comes with a ValueObjectNormalizer that uses the full names

In the end it probably boils down to preference. If you guys agree to my suggestion, I'm happy to create a follow-up PR with adjustments (and provide some documentation on the way).
Otherwise I would at least vote for a version that supports both fromInt() and fromInteger() etc.

@mficzel
Copy link
Member Author

mficzel commented Mar 25, 2022

@bwaidelich you had me at "and provide some documentation on the way"

Not entirely sure about supporting long and short versions but that might be the friendliest way in the end.
Also for me as i do not have to remember which was which.

@mficzel
Copy link
Member Author

mficzel commented Mar 25, 2022

Thinking further i would prefer to add the additional methods as a last minute feature. If we do not get this intuitively noone else will.

@kdambekalns
Copy link
Member

While it makes sense to use short versions for the latter

No, it does not… not anymore than for anything else. And I find it unfortunate that PHP now uses both ways, but not in all places. 🤷‍♂️

we usually try to avoid abbreviations in class and method names (and I think for a good reason)

I repeat my argument about the short form being more "universal" in the PHP context. We do support int in docblocks, too, after all.

But I didn't know about the same methods used in the EventSourcing package already, and I do see value in both ways.

So… whatever. 🙃

@albe
Copy link
Member

albe commented Mar 25, 2022

I agree to what Martin said - having both ways supported is probably the most friendly way to handle this in the core. I do not see any drawbacks from that, since there are no subtle differences in using one or the other. It's just there to remove mental load from developers, which is good. Let's do this @bwaidelich

@bwaidelich
Copy link
Member

There you go: #2766

mhsdesign added a commit to neos/neos-development-collection that referenced this pull request Oct 22, 2023
With neos/flow-development-collection#2762 native support for vo's in various places was introduced.

Also in node property values where the neos ui uses the `DenormalizingObjectConverter` to convert the serialized data to the object.

Unfortunately the reverse currently does NOT use the expected `\JsonSerializable::jsonSerialize` but rather asks the `ArrayFromObjectConverter` for object types when serializing properties for the ui.

That leads to the need of aop like here https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17

This bugfix will make use of the `\JsonSerializable` interface instead directly when serializing properties for the neos ui.
mhsdesign added a commit to sitegeist/Sitegeist.Archaeopteryx that referenced this pull request Oct 23, 2023
With neos/flow-development-collection#2762 native support for vo's in various places was introduced.

Also in node property values where the neos ui uses the `DenormalizingObjectConverter` to convert the serialized data to the object.

Unfortunately the reverse currently does NOT use the expected `\JsonSerializable::jsonSerialize` but rather asks the `ArrayFromObjectConverter` for object types when serializing properties for the ui.

That leads to the need of aop like here https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17

This bugfix will make use of the `\JsonSerializable` interface instead directly when serializing properties for the neos ui.
neos-bot pushed a commit to neos/neos that referenced this pull request Oct 25, 2023
With neos/flow-development-collection#2762 native support for vo's in various places was introduced.

Also in node property values where the neos ui uses the `DenormalizingObjectConverter` to convert the serialized data to the object.

Unfortunately the reverse currently does NOT use the expected `\JsonSerializable::jsonSerialize` but rather asks the `ArrayFromObjectConverter` for object types when serializing properties for the ui.

That leads to the need of aop like here https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17

This bugfix will make use of the `\JsonSerializable` interface instead directly when serializing properties for the neos ui.
mhsdesign added a commit to neos/neos-development-collection that referenced this pull request Feb 1, 2024
With neos/flow-development-collection#2762 native support for vo's in various places was introduced.

Also in node property values where the neos ui uses the `DenormalizingObjectConverter` to convert the serialized data to the object.

Unfortunately the reverse currently does NOT use the expected `\JsonSerializable::jsonSerialize` but rather asks the `ArrayFromObjectConverter` for object types when serializing properties for the ui.

That leads to the need of aop like here https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17

This bugfix will make use of the `\JsonSerializable` interface instead directly when serializing properties for the neos ui.
mhsdesign added a commit to neos/neos-development-collection that referenced this pull request Feb 1, 2024
With neos/flow-development-collection#2762 native support for vo's in various places was introduced.

Also in node property values where the neos ui uses the `DenormalizingObjectConverter` to convert the serialized data to the object.

Unfortunately the reverse currently does NOT use the expected `\JsonSerializable::jsonSerialize` but rather asks the `ArrayFromObjectConverter` for object types when serializing properties for the ui.

That leads to the need of aop like here https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17

This bugfix will make use of the `\JsonSerializable` interface instead directly when serializing properties for the neos ui.
mhsdesign added a commit to neos/neos-development-collection that referenced this pull request Feb 1, 2024
With neos/flow-development-collection#2762 native support for vo's in various places was introduced.

Also in node property values where the neos ui uses the `DenormalizingObjectConverter` to convert the serialized data to the object.

Unfortunately the reverse currently does NOT use the expected `\JsonSerializable::jsonSerialize` but rather asks the `ArrayFromObjectConverter` for object types when serializing properties for the ui.

That leads to the need of aop like here https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17

This bugfix will make use of the `\JsonSerializable` interface instead directly when serializing properties for the neos ui.
mhsdesign added a commit to neos/neos-development-collection that referenced this pull request Feb 1, 2024
…d directly when serializing properties for the neos ui.

With neos/flow-development-collection#2762 native support for vo's in `flow_json_array` was introduced.

That also allows value object support for node properties, as they can be stored directly the node properties flow_json_array.

Unfortunately the property serialisation for the NeosUi does NOT use the expected `\JsonSerializable::jsonSerialize` but the property mapper and the `ArrayFromObjectConverter` for object types to get an array that will be json encoded.

This works mostly fine but in some cases it fails:
- when your `fromArray` fields and property names values dont match
- when a "object" subtypes the object mapper is no convertable like the `GuzzleHttp\Psr7\Uri`:

```
Too few arguments to function GuzzleHttp\Psr7\Uri::isAbsolute(), 0 passed in /core/neos-manufacture-highest/Packages/Framework/Neos.Utility.ObjectHandling/Classes/ObjectAccess.php on line 151 and exactly 1 expected
```

Current workarounds are aop:
https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17
Or to use a dumb property mapper:
https://github.com/sitegeist/Sitegeist.Archaeopteryx/blob/9322b9cb8e4824bcaf7aaa247c23b1244a2f1167/Classes/LinkToArrayForNeosUiConverter.php#L12C16-L12C78
mhsdesign added a commit to neos/neos-development-collection that referenced this pull request Feb 16, 2024
This bugfix will make use of the \JsonSerializable interface instead directly when serializing properties for the neos ui.

With neos/flow-development-collection#2762 native support for vo's in `flow_json_array` was introduced.

That also allows value object support for node properties, as they can be stored directly the node properties flow_json_array.

Unfortunately the property serialisation for the NeosUi does NOT use the expected `\JsonSerializable::jsonSerialize` but the property mapper and the `ArrayFromObjectConverter` for object types to get an array that will be json encoded.

This works mostly fine but in some cases it fails:
- when your `fromArray` fields and property names values dont match
- when a "object" subtypes the object mapper is no convertable like the `GuzzleHttp\Psr7\Uri`:

```
Too few arguments to function GuzzleHttp\Psr7\Uri::isAbsolute(), 0 passed in /core/neos-manufacture-highest/Packages/Framework/Neos.Utility.ObjectHandling/Classes/ObjectAccess.php on line 151 and exactly 1 expected
```

Current workarounds are aop:
https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17
Or to use a dumb property mapper:
https://github.com/sitegeist/Sitegeist.Archaeopteryx/blob/9322b9cb8e4824bcaf7aaa247c23b1244a2f1167/Classes/LinkToArrayForNeosUiConverter.php#L12C16-L12C78
neos-bot pushed a commit to neos/content-repository that referenced this pull request Feb 16, 2024
This bugfix will make use of the \JsonSerializable interface instead directly when serializing properties for the neos ui.

With neos/flow-development-collection#2762 native support for vo's in `flow_json_array` was introduced.

That also allows value object support for node properties, as they can be stored directly the node properties flow_json_array.

Unfortunately the property serialisation for the NeosUi does NOT use the expected `\JsonSerializable::jsonSerialize` but the property mapper and the `ArrayFromObjectConverter` for object types to get an array that will be json encoded.

This works mostly fine but in some cases it fails:
- when your `fromArray` fields and property names values dont match
- when a "object" subtypes the object mapper is no convertable like the `GuzzleHttp\Psr7\Uri`:

```
Too few arguments to function GuzzleHttp\Psr7\Uri::isAbsolute(), 0 passed in /core/neos-manufacture-highest/Packages/Framework/Neos.Utility.ObjectHandling/Classes/ObjectAccess.php on line 151 and exactly 1 expected
```

Current workarounds are aop:
https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17
Or to use a dumb property mapper:
https://github.com/sitegeist/Sitegeist.Archaeopteryx/blob/9322b9cb8e4824bcaf7aaa247c23b1244a2f1167/Classes/LinkToArrayForNeosUiConverter.php#L12C16-L12C78
neos-bot pushed a commit to neos/neos that referenced this pull request Feb 16, 2024
This bugfix will make use of the \JsonSerializable interface instead directly when serializing properties for the neos ui.

With neos/flow-development-collection#2762 native support for vo's in `flow_json_array` was introduced.

That also allows value object support for node properties, as they can be stored directly the node properties flow_json_array.

Unfortunately the property serialisation for the NeosUi does NOT use the expected `\JsonSerializable::jsonSerialize` but the property mapper and the `ArrayFromObjectConverter` for object types to get an array that will be json encoded.

This works mostly fine but in some cases it fails:
- when your `fromArray` fields and property names values dont match
- when a "object" subtypes the object mapper is no convertable like the `GuzzleHttp\Psr7\Uri`:

```
Too few arguments to function GuzzleHttp\Psr7\Uri::isAbsolute(), 0 passed in /core/neos-manufacture-highest/Packages/Framework/Neos.Utility.ObjectHandling/Classes/ObjectAccess.php on line 151 and exactly 1 expected
```

Current workarounds are aop:
https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17
Or to use a dumb property mapper:
https://github.com/sitegeist/Sitegeist.Archaeopteryx/blob/9322b9cb8e4824bcaf7aaa247c23b1244a2f1167/Classes/LinkToArrayForNeosUiConverter.php#L12C16-L12C78
mhsdesign added a commit to neos/neos-ui that referenced this pull request Feb 17, 2024
Ports neos/neos-development-collection#4638 to the Neos Ui, as the class was moved in Neos 9.

Original commit message:

This bugfix will make use of the \JsonSerializable interface instead directly when serializing properties for the neos ui.

With neos/flow-development-collection#2762 native support for vo's in `flow_json_array` was introduced.

That also allows value object support for node properties, as they can be stored directly the node properties flow_json_array.

Unfortunately the property serialisation for the NeosUi does NOT use the expected `\JsonSerializable::jsonSerialize` but the property mapper and the `ArrayFromObjectConverter` for object types to get an array that will be json encoded.

This works mostly fine but in some cases it fails:
- when your `fromArray` fields and property names values dont match
- when a "object" subtypes the object mapper is no convertable like the `GuzzleHttp\Psr7\Uri`:

```
Too few arguments to function GuzzleHttp\Psr7\Uri::isAbsolute(), 0 passed in /core/neos-manufacture-highest/Packages/Framework/Neos.Utility.ObjectHandling/Classes/ObjectAccess.php on line 151 and exactly 1 expected
```

Current workarounds are aop:
https://github.com/sitegeist/Sitegeist.InspectorGadget/blob/78f5f4a206287b1c4bedf5cb88582ed51cb4a311/Classes/Infrastructure/NodeInfo/NodeInfoPostProcessingAspect.php#L17
Or to use a dumb property mapper:
https://github.com/sitegeist/Sitegeist.Archaeopteryx/blob/9322b9cb8e4824bcaf7aaa247c23b1244a2f1167/Classes/LinkToArrayForNeosUiConverter.php#L12C16-L12C78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FEATURE: Make the JsonArrayType able to handle ValueObjects
6 participants