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

IBX-6640: Fixed infinite loop when Normalizer returns an object #75

Merged
merged 3 commits into from
Sep 27, 2023

Conversation

Steveb-p
Copy link
Contributor

@Steveb-p Steveb-p commented Sep 26, 2023

Question Answer
JIRA issue IBX-6640
Type bug
Target version Ibexa v4.5
BC breaks no
Tests pass yes
Doc needed no

When trying to fix an incorrect REST response shape, I have found out that the solution expected cannot work, because it relies on object being returned from Normalizer.
The incorrect response is (note the "data" property):

{
    "ActivityLogList": {
        "_media-type": "application\/vnd.ibexa.api.ActivityLogList+json",
        "_href": "\/api\/ibexa\/v2\/activity_log\/view",
        "ActivityLog": [
            {
                "_media-type": "application\/vnd.ibexa.api.ActivityLog+json",
                "object_id": "test_object_id",
                "object_class": "stdClass",
                "action": "test_action",
                "user_id": 10,
                "logged_at": "__LOGGED_AT__",
                "data": []
            },
            {
                "_media-type": "application\/vnd.ibexa.api.ActivityLog+json",
                "object_id": "test_object_id",
                "object_class": "stdClass",
                "action": "test_action",
                "user_id": 10,
                "logged_at": "__LOGGED_AT__",
                "data": {
                    "foo": "bar"
                }
            }
        ]
    }
}

While the expected output would be:

{
    "ActivityLogList": {
        "_media-type": "application\/vnd.ibexa.api.ActivityLogList+json",
        "_href": "\/api\/ibexa\/v2\/activity_log\/view",
        "ActivityLog": [
            {
                "_media-type": "application\/vnd.ibexa.api.ActivityLog+json",
                "object_id": "test_object_id",
                "object_class": "stdClass",
                "action": "test_action",
                "user_id": 10,
                "logged_at": "__LOGGED_AT__",
                "data": {}
            },
            {
                "_media-type": "application\/vnd.ibexa.api.ActivityLog+json",
                "object_id": "test_object_id",
                "object_class": "stdClass",
                "action": "test_action",
                "user_id": 10,
                "logged_at": "__LOGGED_AT__",
                "data": {
                    "foo": "bar"
                }
            }
        ]
    }
}

To fix this, Ibexa\Rest\Output\Generator\Json\FieldTypeHashGenerator::generateObjectValue() cannot call Ibexa\Rest\Output\Generator\Json\FieldTypeHashGenerator::generateValue(), because this results in an infinite loop.

When looking at Ibexa\Rest\Output\Generator\Json\FieldTypeHashGenerator::generateValue() you can notice that it works on 4 data types:

  • null
  • scalar
  • array
  • object

null and scalar work properly, and loop is broken immediately, so we can skip the additional call.

object in current shape becomes an infinite loop. Those two methods keep calling each other.

array has the only special effect among all 4, where it performs a few list related tasks.

See

https://github.com/symfony/symfony/blob/0659364bbc51920ab6475186e40d1bf06a637047/src/Symfony/Component/Serializer/Serializer.php#L166-L173

and

https://github.com/symfony/symfony/blob/0659364bbc51920ab6475186e40d1bf06a637047/src/Symfony/Component/Serializer/Serializer.php#L51-L55

for explanation why objects sometimes are needed after normalization.

TODO:

  • Implement tests.
  • Fix new code according to Coding Standards ($ composer fix-cs).
  • Ask for Code Review.

@Steveb-p Steveb-p changed the base branch from main to 4.5 September 26, 2023 11:44
@Steveb-p Steveb-p requested a review from a team September 26, 2023 12:11
@Steveb-p Steveb-p added Bug Something isn't working Ready for review labels Sep 26, 2023
@alongosz alongosz requested a review from a team September 26, 2023 12:17
@Steveb-p Steveb-p requested a review from alongosz September 26, 2023 12:19
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@alongosz alongosz requested a review from a team September 26, 2023 12:20
@mikadamczyk mikadamczyk requested a review from a team September 26, 2023 14:54
@konradoboza konradoboza requested a review from a team September 27, 2023 06:33
@Steveb-p Steveb-p merged commit 108a074 into 4.5 Sep 27, 2023
@Steveb-p Steveb-p deleted the fix-infinite-loop branch September 27, 2023 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants