Data: JSON API Serializer doesn't handle underscored attributes properly

Created on 28 Jun 2015  路  11Comments  路  Source: emberjs/data

Hi guys,

We are building a new application with a Rails API Backend and a EmberJS front end, and we are trying to use the version 1 of the JSON API standard. So we are using ActiveModelSerializers on the Backend with the json_api adapter, and in the frontend we are using ember-data's JSONAPISerializer and JSONSerializer, but we have found that our underscored_attributes are not serialized correctly into camelCase attributes on EmberJS.

So our question is to know if this is something that will be supported or if we should 'dasherize' our attributes in the backend?

We noticed that the ActiveModelSerializer from Ember-data does support the underscored attributes as explained in its documentation.

We are using:
Front-end:
emberJS: 1.13.2
ember-data: 1.13.4

Back-end:
active-model-serializers: '0.10.0.rc2'
rails: 4.2.3

Cheers

Most helpful comment

Hi @simaob.

As you have noticed the JSONAPISerializer that ships with Ember Data dasherizes the attributes by default. This default was chosen because it matches the examples on http://jsonapi.org/format and the popular JSONAPI::Resources backend.

If you would like your serializer to dasherizes attributes and relationships you can create a custom Serializer by extending keyForAttribute and keyForRelationship (see below) or you can use an existing community serializer that underscores attributes.

import Ember from 'ember';
import DS from 'ember-data';

var underscore = Ember.String.underscore;

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

  keyForRelationship: function(rawKey) {
    return underscore(rawKey);
  }
});

All 11 comments

Hi @simaob.

As you have noticed the JSONAPISerializer that ships with Ember Data dasherizes the attributes by default. This default was chosen because it matches the examples on http://jsonapi.org/format and the popular JSONAPI::Resources backend.

If you would like your serializer to dasherizes attributes and relationships you can create a custom Serializer by extending keyForAttribute and keyForRelationship (see below) or you can use an existing community serializer that underscores attributes.

import Ember from 'ember';
import DS from 'ember-data';

var underscore = Ember.String.underscore;

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

  keyForRelationship: function(rawKey) {
    return underscore(rawKey);
  }
});

Hi @bmac,
Thanks for your answer. It would be cool to add that to the documentation including how to support other alternatives, as you wrote, as both dashes and underscores are supported in the JSON API format, and Rails' and AMS on Rails defaults is to use the underscores. (But maybe this is something that might already be planned :), if not I'm happy to add this info to the docs).

Thanks again and good job with ember-data! =)

Cheers,
Sim茫o

@simaob Docs for JSONAPISerializer's keyForAttribute and keyForRelationship is definitely something we want. Would you like to submit a PR adding them?

We already have docs for these methods in the JSONSerializer that could be used as a base:

  1. keyForAttribute - https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json-serializer.js#L1285-L1308
  2. keyForRelationship - https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json-serializer.js#L1310-L1335

JSON API doesn't explicitly say in the spec that underscores aren't supported, it only recommends dashes. In that case - the serializer should support both cases, right?

@wecc I'll try to have a go at the docs then.

@hhff :+1: . It would be nice to select this through an option, I agree. But at least adding this information to the docs would be useful.

@simaob Awesome, thanks!

@hhff The serializer supports all cases in the sense that we provide hooks that are easily overridden (keyForAttribute / keyForRelationship). The reason we chose dasherized by default was to make sure we follow the recommendations and mimic the examples at jsonapi.org as closely as possible. The docs really has to be improved though.

That said, this is not the first time this question has popped and I think the general consensus in the Ember Data Team is that this would make an awesome addon. Here's even a proposal of how this could work: https://github.com/emberjs/data/issues/3416#issuecomment-114211315

Yup - tbh now I've thought about this - I think as long as the docs have an official example of this, the interface is great. We should keep this open until there's an example or a cookbook or something up somewhere!

:+1: for underscore examples in the docs. It took me quite a bit of time to figure out how to convert my keys from dashes to underscore and vice versa. I understand that JSONAPI::Resources is a popular backend implementation that uses dashes and jsonapi examples are dashes, but from my experience, every Rails app I have interacted with uses underscore for its json keys.

Perhaps a console warning that notifies when an underscore keys are being ignored?

The docs for keyForAttribute and keyForRelationship now shows examples of how to customize keys, thanks to @simaob.

I guess this could be closed since #3531 has been merged and released as a part of Ember Data 1.13.7?

I am closing this issue since a better documentation for the keyForAttribute and keyForRelationship hooks has been added in #3531.

This is a great little library but I honestly cannot understand why the decision was made to even do any transform on the attribute names in the first place. Having a hook to do this is great but It just makes code slower with no benefit to do it by default.

Was this page helpful?
0 / 5 - 0 ratings