Fix FLEXIAPI-186 Ensure that empty objects are serialized in JSON as objects and not empty arrays

This commit is contained in:
Timothée Jaussoin 2024-06-13 17:15:31 +02:00
parent 9f908c3f7d
commit 47acf6cb9d
4 changed files with 20 additions and 4 deletions

View file

@ -1,7 +1,8 @@
# Flexisip Account Manager Changelog # Flexisip Account Manager Changelog
v1.5 v1.5
---- ---
- Fix FLEXIAPI-186 Ensure that empty objects are serialized in JSON as objects and not empty arrays
- Fix FLEXIAPI-185 Return null if the account dictionary is empty in the API - Fix FLEXIAPI-185 Return null if the account dictionary is empty in the API
- Fix FLEXIAPI-184 Append phone_change_code and email_change_code to the admin /accounts/<id> endpoint if they are available - Fix FLEXIAPI-184 Append phone_change_code and email_change_code to the admin /accounts/<id> endpoint if they are available
- Fix FLEXIAPI-183 Complete the account hooks on the dictionnary actions - Fix FLEXIAPI-183 Complete the account hooks on the dictionnary actions

View file

@ -30,6 +30,7 @@ use Carbon\Carbon;
use Awobaz\Compoships\Compoships; use Awobaz\Compoships\Compoships;
use App\Http\Controllers\Account\AuthenticateController as WebAuthenticateController; use App\Http\Controllers\Account\AuthenticateController as WebAuthenticateController;
use stdClass;
class Account extends Authenticatable class Account extends Authenticatable
{ {
@ -141,9 +142,9 @@ class Account extends Authenticatable
return $this->hasMany(AccountDictionaryEntry::class); return $this->hasMany(AccountDictionaryEntry::class);
} }
public function getDictionaryAttribute(): ?Collection public function getDictionaryAttribute()
{ {
if ($this->dictionaryEntries->isEmpty()) return null; if ($this->dictionaryEntries->isEmpty()) return new stdClass;
return $this->dictionaryEntries->keyBy('key')->map(function ($entry) { return $this->dictionaryEntries->keyBy('key')->map(function ($entry) {
return $entry->value; return $entry->value;

View file

@ -22,6 +22,7 @@ namespace App\Libraries;
use App\Device; use App\Device;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use stdClass;
class FlexisipConnector class FlexisipConnector
{ {
@ -41,6 +42,8 @@ class FlexisipConnector
Log::error('Redis server issue: ' . $th->getMessage()); Log::error('Redis server issue: ' . $th->getMessage());
} }
if ($devices->isEmpty()) return new stdClass;
return $devices->keyBy('uuid'); return $devices->keyBy('uuid');
} }

View file

@ -73,6 +73,17 @@ class ApiAccountTest extends TestCase
]); ]);
} }
public function testEmptyDevices()
{
$account = Account::factory()->create();
$account->generateApiKey();
$this->keyAuthenticated($account)
->get($this->route . '/me/devices')
->assertStatus(200)
->assertSee('{}');
}
public function testUsernameNotPhone() public function testUsernameNotPhone()
{ {
$password = Password::factory()->admin()->create(); $password = Password::factory()->admin()->create();
@ -438,7 +449,7 @@ class ApiAccountTest extends TestCase
$this->keyAuthenticated($admin) $this->keyAuthenticated($admin)
->json('GET', $this->route . '/' . $accountId) ->json('GET', $this->route . '/' . $accountId)
->assertSee(['"dictionary":null'], false) ->assertSee(['"dictionary":{}'], false)
->assertStatus(200); ->assertStatus(200);
} }