Skip to content

Commit 7533d22

Browse files
authored
Don't return custom attributes as json array. (#14)
By not sending the 'custom' attribute when it is empty, we don't have to deal with trying to return an object rather than array.
1 parent f60a6a5 commit 7533d22

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/LaunchDarkly/EventSerializer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ private function serializeUser($user) {
7070
if (!empty($user->getCustom())) {
7171
$customs = array();
7272
$this->filterAttrs($user->getCustom(), $customs, $userPrivateAttrs, $allPrivateAttrs);
73-
$json['custom'] = $customs;
73+
if ($customs) { // if this is empty, we will return a json array for 'custom' instead of an object
74+
$json['custom'] = $customs;
75+
}
7476
}
7577
if (count($allPrivateAttrs)) {
7678
$pa = array_keys($allPrivateAttrs);

tests/EventSerializerTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ private function getFullUserResult() {
3232
private function getUserResultWithAllAttrsHidden() {
3333
return array(
3434
'key' => 'abc',
35-
'custom' => array(),
3635
'privateAttrs' => array('bizzle', 'dizzle', 'firstName')
3736
);
3837
}
@@ -122,7 +121,14 @@ public function testEmptyCustom() {
122121
$json = $this->getJsonForUserBySerializingEvent($user);
123122
$this->assertFalse(isset($json['custom']));
124123
}
125-
124+
125+
public function testEmptyPrivateCustom() {
126+
$builder = new LDUserBuilder("[email protected]");
127+
$user = $builder->privateCustomAttribute("my-key", "my-value")->build();
128+
$json = $this->getJsonForUserBySerializingEvent($user);
129+
$this->assertFalse(isset($json['custom']));
130+
}
131+
126132
public function testUserSecondary() {
127133
$builder = new LDUserBuilder("[email protected]");
128134
$user = $builder->secondary("secondary")->build();

0 commit comments

Comments
 (0)