our app was created using mostly underscores for model attributes and we are having a very difficult time fully implementing mirage as a result. When we try to include a related model that is referenced as an underscore attribute on the model we receive the following error:
Mirage: You tried to include 'dasherized-attribute-name' with model:example(1) but no association named 'dasherizedAttributeName' is defined on the model
With models/factories setup as such:
app/models/example.js:
import { belongsTo } from 'ember-data/relationships';
import Model from 'ember-data/model';
export default Model.extend({
dasherized_attribute_name: belongsTo('attribute', { inverse: 'examples' }),
});
app/models/attribute.js:
import { hasMany } from 'ember-data/relationships';
import Model from 'ember-data/model';
export default Model.extend({
examples: hasMany('example', {
inverse: 'dasherized_attribute_name',
}),
});
app/mirage/factories/example.js:
import { Factory, association } from 'ember-cli-mirage';
export default Factory.extend({
dasherized_attribute_name: association(),
});
app/mirage/serializers/application.js:
import { JSONAPISerializer } from 'ember-cli-mirage';
import { pluralize } from 'ember-inflector';
import { underscore } from '@ember/string';
export default JSONAPISerializer.extend({
alwaysIncludeLinkageData: true,
keyForAttribute(key) {
return underscore(key);
},
keyForRelationship(key) {
// underscoring this value does nothing
return key;
},
payloadKeyFromModelName(modelName) {
return underscore(pluralize(modelName));
},
});
example test:
import { module, test } from 'qunit';
import hbs from 'htmlbars-inline-precompile';
import { render } from '@ember/test-helpers';
import { setupRenderingTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
module('Integration | Component | example', (hooks) => {
setupRenderingTest(hooks);
setupMirage(hooks);
hooks.beforeEach(async function () {
this.store = this.owner.lookup('service:store');
const serverExample = this.server.create('example');
const example = await this.store.findRecord(
'example',
serverExample.id,
{ include: 'dasherized_attribute_name' }
);
this.set('example', example);
});
test('it renders', async (assert) => {
await render(hbs`
{{component
example=example
}}
`);
assert.dom('[data-test-example]').exists();
});
});
I find this to be extremely confusing and unintuitive. Our attribute is snake case, the error says we are trying to include the attribute dasherized but it is finally looking for a camel cased attribute on the model???. Of course changing the model/factory attributes to be camel cased resolves this issue but that isn't necessarily viable for our app right now given the sheer number of attributes. I have tried every single hook referenced in the serializers docs to try to handle this case but simply cannot find where these transformations are actually taking place. Any help would be greatly appreciated.