Skip to content

[Serializer] Fix ObjectNormalizer gives warnings on normalizing with public static property #58255

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

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

alaugks
Copy link
Contributor

@alaugks alaugks commented Sep 13, 2024

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #58221
License MIT

The error message has been occurring since version 6.4.11/7.1.2. If the condition is changed to version >=7.1.2, the error message no longer occurs.

The error is thrown with the following ObjectNormalizer configuration:

$class = new class {
    public static string $foo = "hallo";
};

$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()));
$normalizer->normalize($class);

(\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute)) is true and therefore the entire condition is true.

I moved the condition into a method to improve readability and reduce complexity.

All serializer tests are successful.

Condition

For better readability here with breaks.

7.1.1
https://github.com/symfony/symfony/blob/v7.1.1/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php#L180

if (!isset(self::$isReadableCache[$class.$attribute])) {
    self::$isReadableCache[$class.$attribute] =
    $this->propertyInfoExtractor->isReadable($class, $attribute)
    || $this->hasAttributeAccessorMethod($class, $attribute);
}

>=7.2.2
https://github.com/symfony/symfony/blob/v7.1.2/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php#L180

if (!isset(self::$isReadableCache[$class.$attribute])) {
    self::$isReadableCache[$class.$attribute] =
    (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute))
    || $this->propertyInfoExtractor->isReadable($class, $attribute)
    || $this->hasAttributeAccessorMethod($class, $attribute);
}

Fixed condition

if (!isset(self::$isReadableCache[$class.$attribute])) {
    self::$isReadableCache[$class.$attribute] =
    $this->propertyInfoExtractor->isReadable($class, $attribute)
    || $this->hasAttributeAccessorMethod($class, $attribute)
    || (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute));
}

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.2 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "7.1" but it seems your PR description refers to branch ">= 7.1.2".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@xabbuh
Copy link
Member

xabbuh commented Sep 13, 2024

Does this also affect 6.4?

@alaugks
Copy link
Contributor Author

alaugks commented Sep 13, 2024

@xabbuh It also affects 6.4. I have tested it with v6.4.11.

@xabbuh xabbuh modified the milestones: 7.1, 6.4 Sep 13, 2024
@alaugks
Copy link
Contributor Author

alaugks commented Sep 13, 2024

@alexandre-daubois try-catch is removed. However, I have left expectNotToPerformAssertions() in the test so that the test is not marked with "This test did not perform any assertions".

@alexandre-daubois
Copy link
Member

@xabbuh It also affects 6.4. I have tested it with v6.4.11.

You may rebase your PR onto 6.4 and change the target branch of the PR. This way, the fix is available on all (still supported) affected branches

@OskarStark OskarStark changed the title [Serializer] Fix ObjectNormalizer gives warnings on normalizing with static property [Serializer] Fix ObjectNormalizer gives warnings on normalizing with static property Sep 14, 2024
alaugks added a commit to alaugks/symfony that referenced this pull request Sep 14, 2024
@alaugks alaugks changed the title [Serializer] Fix ObjectNormalizer gives warnings on normalizing with static property [Serializer] Fix ObjectNormalizer gives warnings on normalizing with public static property Sep 14, 2024
Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

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

LGTM

@nicolas-grekas nicolas-grekas changed the base branch from 7.1 to 6.4 October 3, 2024 09:58
@nicolas-grekas
Copy link
Member

Thank you @alaugks.

@nicolas-grekas nicolas-grekas merged commit f5a5104 into symfony:6.4 Oct 3, 2024
9 of 10 checks passed
This was referenced Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants