Skip to content

PHPLIB-1600 Accept stdClass objects in BSONDocument constructor #1551

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 2 commits into from
Dec 11, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Dec 4, 2024

Fixes PHPLIB-1600
Fixes #1549

Only stdClass objects are allowed, because other types of objects can have private/protected properties and we don't what to define the rule for this properties (keep or skip).

protected string $baz = 'qux';
});
$this->assertSame('bar', $document->foo);
$this->assertObjectNotHasProperty('baz', $document);
Copy link
Member

@jmikola jmikola Dec 4, 2024

Choose a reason for hiding this comment

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

This is essentially a test of ArrayObject itself. I think it'd be preferable to test bsonSerialize() (note: jsonSerialize() has the same implementation), so we can see what would actually get serialized to BSON.

My concern is that we should preserve the same encoding behavior as the extension, which will only encodes public properties.

See: https://3v4l.org/47VCv#v8.4.1

I think it's bit strange that the encoded names for protected and private properties still end up in output from ArrayObject::getArrayCopy(), and are preserved when casting back to an object. It's certainly odd that the stdClass instance resulting from that cast ends up with non-public properties, but that's likely an oddity of PHP itself.

If we can get away with only accepting stdClass|array, that might circumvent this issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I followed your suggestion on allowing only array|stdClass. That covers the request.

@GromNaN GromNaN marked this pull request as ready for review December 10, 2024 19:04
@GromNaN GromNaN requested a review from a team as a code owner December 10, 2024 19:04
@GromNaN GromNaN requested a review from jmikola December 10, 2024 19:04
@GromNaN GromNaN changed the title PHPLIB-1600 Accept object in BSONDocument constructor PHPLIB-1600 Accept stdClass onjects in BSONDocument constructor Dec 10, 2024
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

LGTM although I think you can remove two lines from the test case.

@GromNaN GromNaN changed the title PHPLIB-1600 Accept stdClass onjects in BSONDocument constructor PHPLIB-1600 Accept stdClass objects in BSONDocument constructor Dec 11, 2024
@GromNaN GromNaN enabled auto-merge (squash) December 11, 2024 14:31
@GromNaN GromNaN merged commit 75f9566 into mongodb:v1.x Dec 11, 2024
31 checks passed
@GromNaN GromNaN deleted the PHPLIB-1600 branch December 17, 2024 13:27
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.

BSONDocument doesn't accept an object in its constructor
2 participants