1
0
Fork 0
mirror of synced 2024-05-20 12:42:39 +12:00

Merge pull request #2532 from appwrite/fix-prefs-response

Fix-prefs-response
This commit is contained in:
Torsten Dittmann 2021-12-30 11:07:51 +01:00 committed by GitHub
commit 7740696e36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 18 deletions

View file

@ -4,6 +4,7 @@ namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;
use Utopia\Database\Document;
class User extends Model
{
@ -61,6 +62,24 @@ class User extends Model
;
}
/**
* Get Collection
*
* @return string
*/
public function filter(Document $document): Document
{
$prefs = $document->getAttribute('prefs');
if($prefs instanceof Document) {
$prefs = $prefs->getArrayCopy();
}
if(is_array($prefs) && empty($prefs)) {
$document->setAttribute('prefs', new stdClass);
}
return $document;
}
/**
* Get Name
*

View file

@ -151,10 +151,11 @@ class Client
* @param string $path
* @param array $params
* @param array $headers
* @param bool $decode
* @return array|string
* @throws Exception
*/
public function call(string $method, string $path = '', array $headers = [], array $params = [])
public function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true)
{
$headers = array_merge($this->headers, $headers);
$ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : ''));
@ -216,17 +217,19 @@ class Client
$responseType = $responseHeaders['content-type'] ?? '';
$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch (substr($responseType, 0, strpos($responseType, ';'))) {
case 'application/json':
$json = json_decode($responseBody, true);
if ($json === null) {
throw new Exception('Failed to parse response: '.$responseBody);
}
$responseBody = $json;
$json = null;
break;
if($decode) {
switch (substr($responseType, 0, strpos($responseType, ';'))) {
case 'application/json':
$json = json_decode($responseBody, true);
if ($json === null) {
throw new Exception('Failed to parse response: '.$responseBody);
}
$responseBody = $json;
$json = null;
break;
}
}
if ((curl_errno($ch)/* || 200 != $responseStatus*/)) {

View file

@ -20,13 +20,20 @@ trait UsersBase
'email' => 'cristiano.ronaldo@manchester-united.co.uk',
'password' => 'password',
'name' => 'Cristiano Ronaldo',
]);
], false);
// Test empty prefs is object not array
$bodyString = $user['body'];
$prefs = substr($bodyString, strpos($bodyString, '"prefs":')+8,2);
$this->assertEquals('{}', $prefs);
$body = json_decode($bodyString, true);
$this->assertEquals($user['headers']['status-code'], 201);
$this->assertEquals($user['body']['name'], 'Cristiano Ronaldo');
$this->assertEquals($user['body']['email'], 'cristiano.ronaldo@manchester-united.co.uk');
$this->assertEquals($user['body']['status'], true);
$this->assertGreaterThan(0, $user['body']['registration']);
$this->assertEquals($body['name'], 'Cristiano Ronaldo');
$this->assertEquals($body['email'], 'cristiano.ronaldo@manchester-united.co.uk');
$this->assertEquals($body['status'], true);
$this->assertGreaterThan(0, $body['registration']);
/**
* Test Create with Custom ID for SUCCESS
@ -48,7 +55,7 @@ trait UsersBase
$this->assertEquals(true, $res['body']['status']);
$this->assertGreaterThan(0, $res['body']['registration']);
return ['userId' => $user['body']['$id']];
return ['userId' => $body['$id']];
}
/**