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

yii\web\Link remove null property #18469

Closed
donny26utama opened this issue Jan 7, 2021 · 2 comments
Closed

yii\web\Link remove null property #18469

donny26utama opened this issue Jan 7, 2021 · 2 comments
Labels
Milestone

Comments

@donny26utama
Copy link

donny26utama commented Jan 7, 2021

I'm trying to implement yii\web\Linkable to some ActiveRecord, when I'm using yii\web\Link the result show all property. but, if I use yii\web\Link in nested array, the result only show some property what I set.

What steps will reproduce the problem?

on my table class Activerecord, I implement yii\web\Linkable
and I add function getLinks

$managerLink = new yii\web\Link([
     'href' => 'http://example.com/users/4',
     'title' => 'My Manager',
]);
return [
     'self' => 'http://example.com/users/1',
     'friends' => [
            'http://example.com/users/2',
            'http://example.com/users/3',
     ],
     'manager' => $managerLink, // $managerLink is a Link object
     'managers' => [
           'manager1' => $managerLink,
     ]
]

the result,

    . . . 
    "_links": {
        "self": {
            "href": "http://example.com/users/1"
        },
        "friends": [
            {
                "href": "http://example.com/users/2"
            },
            {
                "href": "http://example.com/users/3"
            }
        ],
        "manager": {
            "href": "http://example.com/users/4",
            "name": null,
            "type": null,
            "templated": false,
            "profile": null,
            "title": "My Manager",
            "hreflang": null
        }
    }
}

but if I make nested array (managers) like this,

$managerLink = new yii\web\Link([
     'href' => 'http://example.com/users/4',
     'title' => 'My Manager',
]);
return [
     'self' => 'http://example.com/users/1',
     'friends' => [
            'http://example.com/users/2',
            'http://example.com/users/3',
     ],
     'manager' => $managerLink, // $managerLink is a Link object
     'managers' => [
           'manager1' => $managerLink,
     ]
]

the result to be,

    . . .
    "_links": {
        "self": {
            "href": "http://example.com/users/1"
        },
        "friends": [
            {
                "href": "http://example.com/users/2"
            },
            {
                "href": "http://example.com/users/3"
            }
        ],
        "manager": {
            "href": "http://example.com/users/4",
            "name": null,
            "type": null,
            "templated": false,
            "profile": null,
            "title": "My Manager",
            "hreflang": null
        },
        "managers": {
            "manager1": {
                "href": "http://example.com/users/4",
                "title": "My Manager"
            }
        }
    }
}

What is the expected result?

    . . . 
    "_links": {
        "self": {
            "href": "http://example.com/users/1"
        },
        "friends": [
            {
                "href": "http://example.com/users/2"
            },
            {
                "href": "http://example.com/users/3"
            }
        ],
        "manager": {
            "href": "http://example.com/users/4",
            "title": "My Manager",
        }
    }
}

What do you get instead?

Maybe this is possible bug. But how I can only show some property without make yii\web\Link object in array?

Additional info

I was try to add one condition on yii\web\Link::serialize() like this,

    public static function serialize(array $links)
    {
        foreach ($links as $rel => $link) {
            if (is_array($link)) {
                foreach ($link as $i => $l) {
                    $link[$i] = $l instanceof self ? array_filter((array) $l) : ['href' => $l];
                }
                $links[$rel] = $link;
            } elseif ($link instanceof self) { // this additional my code
                $l = array_filter((array) $link); // this additional my code
                $links[$rel] = $l; // this additional my code
            } elseif (!$link instanceof self) {
                $links[$rel] = ['href' => $link];
            }
        }

        return $links;
    }

and the result would be like expected.

Q A
Yii version 2.0.14
PHP version 7.2.22
Operating system MacOS
@samdark samdark added the type:bug Bug label Jan 11, 2021
@samdark
Copy link
Member

samdark commented Jan 11, 2021

Looks like a bug. Would you please prepare a pull request with test for it?

@donny26utama
Copy link
Author

OK

ggh2e3 added a commit to ggh2e3/yii2 that referenced this issue Oct 20, 2023
ggh2e3 added a commit to ggh2e3/yii2 that referenced this issue Oct 20, 2023
ggh2e3 added a commit to ggh2e3/yii2 that referenced this issue Oct 20, 2023
ggh2e3 added a commit to ggh2e3/yii2 that referenced this issue Oct 31, 2023
ggh2e3 added a commit to ggh2e3/yii2 that referenced this issue Nov 17, 2023
Co-authored-by: Bizley <pawel.bizley@gmail.com>
@samdark samdark added this to the 2.0.50 milestone Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants