Skip to content

Exclude properties based on semver constraints#1361

Open
W0rma wants to merge 1 commit into
schmittjoh:masterfrom
W0rma:use-composer-semver
Open

Exclude properties based on semver constraints#1361
W0rma wants to merge 1 commit into
schmittjoh:masterfrom
W0rma:use-composer-semver

Conversation

@W0rma
Copy link
Copy Markdown
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
Copy Markdown
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?

Comment thread composer.json Outdated
Comment thread src/Annotation/VersionConstraints.php Outdated
@W0rma W0rma force-pushed the use-composer-semver branch from 8e50c6b to c2a0e19 Compare November 5, 2021 08:28
@W0rma
Copy link
Copy Markdown
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
Copy Markdown
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?

class VersionedObject
{
/**
* @Until("1.0.x")
Copy link
Copy Markdown
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
Copy Markdown
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