Skip to content

Commit

Permalink
Use fields() instead of attributes (#534)
Browse files Browse the repository at this point in the history
* Use fields() instead of attributes

The fields definition returns the actual resource informations

* Update CHANGELOG.md

* tests

* add tests, keep behavior when fields is empty

* Update ActiveRecordToSchema.php

Co-authored-by: Basil <basil@Trudys-MBP.fritz.box>
  • Loading branch information
nadar and Basil authored Jul 24, 2020
1 parent a696819 commit 0d77608
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE

## 3.4.1

+ [#534](https://github.com/luyadev/luya-module-admin/pull/534) Using `fields()` when working with ActiveRecords as it represents the REST resource information.
+ [#533](https://github.com/luyadev/luya-module-admin/pull/533) Fixed a bug where OpenApi property relations won't expand.

## 3.4.0 (21. July 2020)
Expand Down
8 changes: 7 additions & 1 deletion src/openapi/specs/ActiveRecordToSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ public function __construct(BaseSpecs $baseSpecs, Model $activeRecord, $senderAc
public function getProperties($phpDocProperties = true)
{
$properties = [];
foreach ($this->activeRecord->attributes() as $attributeName) {
$fields = array_keys($this->activeRecord->fields());

if (empty($fields)) {
$fields = $this->activeRecord->attributes();
}

foreach ($fields as $attributeName) {
$properties[$attributeName] = $this->createSchema($attributeName);
}

Expand Down
56 changes: 56 additions & 0 deletions tests/admin/openapi/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace luya\admin\tests\admin\openapi;

use admintests\AdminModelTestCase;
use luya\admin\apis\UserController;
use luya\admin\models\Config;
use luya\admin\models\Logger;
use luya\admin\models\NgrestLog;
Expand All @@ -14,7 +15,10 @@
use luya\admin\models\StorageFile;
use luya\admin\models\StorageFilter;
use luya\admin\models\StorageImage;
use luya\admin\models\User;
use luya\admin\openapi\Generator;
use luya\admin\openapi\specs\ActiveRecordToSchema;
use luya\admin\openapi\specs\ControllerSpecs;
use luya\testsuite\fixtures\NgRestModelFixture;
use luya\testsuite\traits\DatabaseTableTrait;
use luya\web\UrlManager;
Expand Down Expand Up @@ -122,4 +126,56 @@ public function testAssignUrlRules()

$this->assertSame(['/this/is/my/pattern'], array_keys($generator->getPaths()));
}

public function testModelResponse()
{
$this->createAdminLangFixture();
$this->createAdminUserFixture();
$this->createAdminGroupFixture(1);
$spec = new ControllerSpecs(new UserController('user', $this->app));

$model = new User();

$ars = new ActiveRecordToSchema($spec, $model);

$props = $ars->getProperties();
$this->assertSame([
'id',
'title',
'firstname',
'lastname',
'email',
'is_deleted',
'is_api_user',
'api_last_activity',
'auth_token',
'is_request_logger_enabled',
'email_verification_token_timestamp',
'login_attempt_lock_expiration',
'login_attempt',
'email_verification_token',
'api_allowed_ips',
'api_rate_limit',
'cookie_token',
'settings',
'force_reload',
'secure_token_timestamp',
'secure_token',
'password',
'password_salt',
'login_2fa_enabled',
'login_2fa_secret',
'login_2fa_backup_key',
'password_verification_token',
'password_verification_token_timestamp',
'setting',
'groups',
], array_keys($props));

$groupModelKeys = array_keys($ars->createSchema('groups')->items->properties);

$this->assertSame([
'id', 'name', 'text', 'is_deleted', 'users',
], $groupModelKeys);
}
}

0 comments on commit 0d77608

Please sign in to comment.