Skip to content

Model.save() cannot save null hasOne relationship #2876

@clarkwinkelmann

Description

@clarkwinkelmann

Bug Report

Current Behavior
A javascript error is thrown when using Model.save() to save a null value on a hasOne relationship.

Steps to Reproduce
Run:

app.store.all('users')[0].save({relationships: {customHasOne: null}})

(it doesn't matter if the relationship is registered on the model or not since the code never checks if it exists)

See error:

Uncaught TypeError: t is null
    getIdentifier http://beta16.flarum.localhost/assets/forum-31c35670.js:1
    save http://beta16.flarum.localhost/assets/forum-31c35670.js:1
    exports http://beta16.flarum.localhost/assets/forum-31c35670.js:822
    e http://beta16.flarum.localhost/assets/forum-31c35670.js:1

Expected Behavior
It should be possible to pass an empty value for hasOne relationships, just like it's possible to pass an empty array for hasMany relationships.

Environment

  • Flarum version: beta 16

Possible Solution
The problem is here:

https://github.com/flarum/core/blob/a5182a145401f760cf9743fcd2611a85adbdac0c/js/src/common/Model.js#L141

If the relationship value isn't an array, it's passed to Model.getIdentifier without checking if it's null.

Either Model.getIdentifier should pass through null values, or Model.getIdentifier should not be called if the value is null.

Additional Context
I'm using this in a yet unreleased extension. As a workaround I am using a local patch that makes Model.getIdentifier return null if the model argument is null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions