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

Exclude properties based on semver constraints #1361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

W0rma
Copy link
Contributor

@W0rma W0rma commented Oct 31, 2021

Q A
Bug fix? no
New feature? yes
Doc updated yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #1312
License MIT

This PR adds the possibility to exclude properties from serialization based on version constraints accepted by composer.

Example:

use JMS\Serializer\Annotation\VersionConstraints;

class Author
{
    #[VersionConstraints('^6.1')]
    public function getAge(): int
    {
        return 61;
    }
}

I'm not sure if VersionConstraints is a good name or if there are better alternatives. Suggestions are welcome :)

@W0rma W0rma force-pushed the use-composer-semver branch from 79bc13e to 62d92f6 Compare October 31, 2021 12:39
@W0rma W0rma marked this pull request as ready for review October 31, 2021 12:41
@W0rma W0rma force-pushed the use-composer-semver branch 2 times, most recently from be9c84f to 8e50c6b Compare October 31, 2021 16:52
Copy link
Collaborator

@scyzoryck scyzoryck left a comment

Choose a reason for hiding this comment

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

Thanks @W0rma for this PR! I left two minor comments.
Do you think that we should keep Until and Since attributes or they should be deprecated?

composer.json Outdated Show resolved Hide resolved
src/Annotation/VersionConstraints.php Outdated Show resolved Hide resolved
@W0rma W0rma force-pushed the use-composer-semver branch from 8e50c6b to c2a0e19 Compare November 5, 2021 08:28
@W0rma
Copy link
Contributor Author

W0rma commented Nov 5, 2021

Do you think that we should keep Until and Since attributes or they should be deprecated?

@scyzoryck Good point. I think they should be deprecated since version constraints provide even more flexibility. Do you agree?

@scyzoryck
Copy link
Collaborator

Thanks. Looks good to me.

I think they should be deprecated since version constraints provide even more flexibility. Do you agree?

It was my initial thought - as basically new constraint covers all functionalities provided by older constraints. On the other hand new one requires external dependency, so keeping simpler constraints might made sense too. @goetas - what do you think about it?

@@ -57,20 +57,21 @@ expose them via an API that is consumed by a third-party:
class VersionedObject
{
/**
* @Until("1.0.x")
Copy link
Collaborator

@goetas goetas Nov 8, 2021

Choose a reason for hiding this comment

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

I would not abandon @Until and @Since , mainly for performance reasons. my guess is that composer/semver is way slower that the php's version_compare function, in a large object graph in it can make the difference.

I think that it could be also mentioned in the documentation the potential performance impact.

BTW, i would be curious to see a benchmark about it

{
public function __construct($values = [], ?string $version = null)
{
if (!class_exists(Semver::class)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

what if this check is done in the metadata drivers? so we avoid to call it at runtime each time the annotation is instantiated?

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.

3 participants