nicklandgrebe / active-resource.js

ActiveResource.js - API resource relational mapping in JavaScript
https://active-resource.js.org
MIT License
133 stars 20 forks source link

Multi-word relationship names have wrong case during request #50

Open CharlieIGG opened 5 years ago

CharlieIGG commented 5 years ago

I have the following relationship:

class Thing extends resourceLibrary.Base {
        this.belongsTo('multiWord')
}

class MultiWord extends resourceLibrary.Base {
        this.hasMany('things')
}

MultiWord's relationship on my backend is obviously declared with snake case (belongs_to :multi_word) since I'm using Rails.

If I try to create or update a Thing with its nested MultiWord, JSON:API will expect the corresponding key in the relationships hash to be kebab-case, but ActiveResourceJS automatically converts it to snake_case when making the request:

Thing.build().assignMultiWord(multiWordInstance).save()
// yields ---> relationships: { multi_word: {...} }

Which results in a 400 response, with an error code "105: Param multi_word is not allowed".

Bypassing ActiveResourceJS for these requests and putting in relationships: { multi-word: {...} } makes everything work...

I've tried other formats for declaring the relationship, none of which seem to work:

class Thing extends resourceLibrary.Base {
        this.belongsTo('multi_word')
}
// doesn't work

class Thing extends resourceLibrary.Base {
        this.belongsTo('multi-word')
}
// can't even assign the association anymore
CharlieIGG commented 5 years ago

Any help on this @nicklandgrebe ?