Skip to content

Commit 2a75c30

Browse files
authored
Merge pull request #31487 from nextcloud/enh/account-json-serialization
2 parents ec4be7f + a99d33c commit 2a75c30

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/private/Accounts/Account.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,16 @@ public function getFilteredProperties(string $scope = null, string $verified = n
104104
return $result;
105105
}
106106

107-
/** @return IAccountPropertyCollection[]|IAccountProperty[] */
107+
/** @return array<string, IAccountProperty|array<int, IAccountProperty>> */
108108
public function jsonSerialize(): array {
109-
return $this->properties;
109+
$properties = $this->properties;
110+
foreach ($properties as $propertyName => $propertyObject) {
111+
if ($propertyObject instanceof IAccountPropertyCollection) {
112+
// Override collection serialization to discard duplicate name
113+
$properties[$propertyName] = $propertyObject->jsonSerialize()[$propertyName];
114+
}
115+
}
116+
return $properties;
110117
}
111118

112119
public function getUser(): IUser {

tests/lib/Accounts/AccountTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,26 @@ public function testJsonSerialize() {
120120
$user = $this->createMock(IUser::class);
121121
$properties = [
122122
IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
123-
IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, '[email protected]', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, '')
123+
IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, '[email protected]', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, ''),
124+
IAccountManager::COLLECTION_EMAIL => [
125+
new AccountProperty(IAccountManager::COLLECTION_EMAIL, '[email protected]', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''),
126+
new AccountProperty(IAccountManager::COLLECTION_EMAIL, '[email protected]', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFICATION_IN_PROGRESS, ''),
127+
new AccountProperty(IAccountManager::COLLECTION_EMAIL, '[email protected]', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, ''),
128+
],
124129
];
130+
125131
$account = new Account($user);
126132
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
127133
$account->setProperty(IAccountManager::PROPERTY_EMAIL, '[email protected]', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED);
128134

135+
$col = new AccountPropertyCollection(IAccountManager::COLLECTION_EMAIL);
136+
$col->setProperties([
137+
new AccountProperty($col->getName(), '[email protected]', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''),
138+
new AccountProperty($col->getName(), '[email protected]', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFICATION_IN_PROGRESS, ''),
139+
new AccountProperty($col->getName(), '[email protected]', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, ''),
140+
]);
141+
$account->setPropertyCollection($col);
142+
129143
$this->assertEquals($properties, $account->jsonSerialize());
130144
}
131145
}

0 commit comments

Comments
 (0)