Skip to content

JSON API attribute name conversion when using a custom application serializer #116

Description

@begedin

In our application, we're using ember data with JSON API on the client side, Rails API with Active Model Serializers on the server side.

ActiveModelSerializers, by default, give us attributes in underscored form, so we had to override our application serializer like so:

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); // needed to transform AMS's underscores
  },

  keyForRelationship: function(rawKey) {
    return underscore(rawKey); // needed to transform AMS's underscores
  }
});

However, when we define a factory, we have to use camelized properties:

FactoryGuy.define('user', {
 default: {
   admin: false,
   canTrial: false,
   email: 'johndoe@gmail.com',
   fullName: 'John Doe',
   id: 1
 }
});

From debugging, I'm seeing that what factory guy does is the following:

  1. Get the factory
  2. Run it through the default JSON API serializer to get a dasherized form
  3. Pass it on to the store as an adapter payload
  4. The store then uses the actual application serializer (our custom one), to convert back to the client camelized form.
  5. Since the adapter expects an underscored form as input, the multi-word properties simply get ignored.

We could modify our adapter to account for this behavior and simply support both underscored and dasherized form of attribute keys, but I'm thinking FactoryGuy should use the application serializer anyway. Otherwise, it would prevent us from customizing the way our attribute (or relationship) keys are named.

Am I missing something, or is this really what's going on here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions