[2.x] Notifications endpoint 400s on an invalid `subject.discussion` default include when a notification subject type has no discussion relationship
Description
NotificationResource adds subject.discussion to the default include for the notifications endpoints whenever more than one notification subject type is registered:
// framework/core/src/Api/Resource/NotificationResource.php
->defaultInclude(array_filter([
'fromUser',
'subject',
$this->initialized && count($this->subjectTypes()) > 1
? 'subject.discussion'
: null,
]))
The include validator (flarum/json-api-server, IncludesData::validateInclude) validates default includes too, and a nested include on the polymorphic subject relationship is only valid if at least one subject type exposes an includable discussion relationship. When the registered subject types are, for example, {discussions, users} (neither of which has a discussion relationship), subject.discussion is invalid and the request is rejected:
{"errors":[{"status":"400","code":"validation_error","title":"Bad Request","detail":"Invalid include [subject.discussion]","source":{"parameter":"include"}}]}
In other words, the framework generates a default include that it then rejects with a 400. Because NotificationListState sends no explicit include and relies on the server default, this breaks the notifications dropdown and page for every user on the affected forum, not just direct API consumers.
Steps to reproduce
On a forum where no registered notification subject type is discussion-bearing (i.e. no Post-subject notification type is present: mentions, subscriptions, likes, flags, etc. all disabled), register any notification type whose subject is a User (or any model without a discussion relationship). A minimal test extender is enough:
use Flarum\Database\AbstractModel;
use Flarum\Extend;
use Flarum\Notification\AlertableInterface;
use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\User\User;
class UserSubjectBlueprint implements BlueprintInterface, AlertableInterface
{
public function getFromUser(): ?User { return null; }
public function getSubject(): ?AbstractModel { return null; }
public function getData(): mixed { return []; }
public static function getType(): string { return 'userSubjectNotification'; }
public static function getSubjectModel(): string { return User::class; }
}
// extend.php
(new Extend\Notification())->type(UserSubjectBlueprint::class, ['alert']),
Then, authenticated as any user:
GET /api/notifications
No data is required. The subject collection now resolves to {discussions, users}, so count(subjectTypes()) > 1 is true and subject.discussion is added to the default include.
Confirmed behavior on a fresh 2.0.0-rc.5 install (MariaDB), varying only the registered subject types:
| Registered notification subject types | GET /api/notifications |
|---|---|
{discussions} (core only) |
200 |
{discussions, users} (add a User-subject type) |
400 Invalid include [subject.discussion] |
{discussions, users, posts} (also add a Post-subject type) |
200 |
Expected behavior
GET /api/notifications returns 200 and the notification list loads. A default include the framework generates for itself should never be invalid.
Actual behavior
GET /api/notifications returns 400 with Invalid include [subject.discussion], so the notifications dropdown/page fails to load for all users on that forum.
Why this is not exotic
User subjects are already a first-class pattern in the bundled extensions: flarum/mentions ships UserMentionedBlueprint (subject User) alongside GroupMentionedBlueprint (subject Group). Mentions only avoids the 400 because it also ships PostMentionedBlueprint (subject Post), and Post has a discussion relationship, which keeps subject.discussion valid for the whole collection. Any forum that registers a User-subject (or Group-subject) notification type without a Post-subject type present hits the invalid include. A default install is safe because mentions and subscriptions are enabled, so this is low-frequency, but the underlying include is still invalid by construction.
Suggested direction
Only add subject.discussion to the default include when at least one subject type actually exposes an includable discussion relationship, rather than gating on count($this->subjectTypes()) > 1. Alternatively, make nested includes on a polymorphic relationship tolerant of member types that lack the nested field (apply where valid, skip where not) so a framework-generated default can never 400.
Environment
- Flarum: reproduced on a fresh
2.0.0-rc.5install (MariaDB); theNotificationResourcedefault-include code is unchanged fromrc.4and is identical on the current2.xbranch. flarum/json-api-server:0.1.2.- Reproduced with a minimal single-type test extender, no third-party extensions involved.