Skip to content

Commit 2c82b1c

Browse files
Fix »OAuth Client Table Changes« migration snippet in the upgrade guide (#1859)
* Fix owner columns are not added after the `user_id` as intended * Fix `TypeError` when migrating the `oauth_clients` table TypeError Laravel\Passport\Client::{closure:Laravel\Passport\Client::grantTypes():157}(): Return value must be of type array, null returned This happens because `isset($value) ? $this->fromJson($value) : array_keys(/* … */)` evaluates to `null`. > After adding a non-nullable `grant_types` column with no default values `isset($value)` is `true` because `$value` is an empty string. > As a result `$this->fromJson($value)` is called with an empty string which evaluates to `null` … ```php > $value = ''; isset($value); = true ``` This then violates the return type `get: fn (?string $value): array` … Making the field nullable first fixes the issue: ```php > $value = null; isset($value); = false ``` Once the data is written I set nullable to false because the original migration creates a non-nullable field as well … An alternative to this "workaround" would be updating the `Laravel\Passport\Client::grantTypes()` implementation. * Fix broken polymorphic owner relationship The `config()` call falls back to `null` when the `$client` has on provider. The result is a client with an `owner_id` that has no corresponding `owner_type` set. Adding `\App\Models\User::class` as a fallback seems reasonable. * Replace hardcoded User class-string Co-authored-by: Hafez Divandari <[email protected]> * Leave `grant_types` nullable. --------- Co-authored-by: Hafez Divandari <[email protected]>
1 parent 36af05a commit 2c82b1c

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

UPGRADE.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,20 @@ If you prefer to use the new structure, you may create a migration to apply the
139139

140140
```php
141141
Schema::table('oauth_clients', function (Blueprint $table) {
142-
$table->after('user_id', function (Blueprint $table) {
143-
$table->nullableMorphs('owner');
144-
});
142+
$table->nullableMorphs('owner', after: 'user_id');
145143

146144
$table->after('provider', function (Blueprint $table) {
147145
$table->text('redirect_uris');
148-
$table->text('grant_types');
146+
$table->text('grant_types')->nullable();
149147
});
150148
});
151149

152150
foreach (Passport::client()->cursor() as $client) {
153151
Model::withoutTimestamps(fn () => $client->forceFill([
154152
'owner_id' => $client->user_id,
155-
'owner_type' => $client->user_id ? config('auth.providers.'.$client->provider.'.model') : null,
153+
'owner_type' => $client->user_id
154+
? config('auth.providers.'.($client->provider ?: config('auth.guards.api.provider')).'.model')
155+
: null,
156156
'redirect_uris' => $client->redirect_uris,
157157
'grant_types' => $client->grant_types,
158158
])->save());

0 commit comments

Comments
 (0)