Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
clarkwinkelmann opened this issue May 18, 2021 · 0 comments · Fixed by #3131
Closed

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

clarkwinkelmann opened this issue May 18, 2021 · 0 comments · Fixed by #3131
Milestone

Comments

@clarkwinkelmann
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants