Search code examples
ember.jsember-datajson-api

JSONAPISerializer not returning underscore case to API


I am trying to retrieve a single record by returning

singleGroup: this.store.findRecord('individual_group', group_id)

I am then getting this error in the console:

GET http://localhost:4200/api/v1/individual-groups/349 404 (Not Found)

where it seems to be dasherizing individual_groups to individual-groups

Now in the API documentation, an individual group is found by doing a GET request to api/v1/individual_groups/:id

I'm using the JSONAPIAdapter and already have a serializer set up as so:

export default DS.JSONAPISerializer.extend({
    keyForAttribute: function(attr) {
        return Ember.String.underscore(attr);
    },
    keyForRelationship: function(attr) {
        return Ember.String.underscore(attr);
    }
});

but it does not seem to be affecting this particular case.

Is there a way to make sure that when I call this.store.findRecord('individual_group', group_id) it will make the request to the API using

GET http://localhost:4200/api/v1/individual_groups/349

instead of

GET http://localhost:4200/api/v1/individual-groups/349

Solution

  • You should redefine pathForType adapter method. The default implementation uses dasherize:

      //as is
      pathForType: function(modelName) {
        var dasherized = Ember.String.dasherize(modelName);
        return Ember.String.pluralize(dasherized);
      }
    
      //to be
      pathForType: function(modelName) {
        var underscored = Ember.String.underscore(modelName);
        return Ember.String.pluralize(underscored);
      }