Skip to content

Commit c140136

Browse files
author
Will
committed
Don't camelize relationship names when camelizeKeys is false
Signed-off-by: Will <[email protected]>
1 parent 0b5a568 commit c140136

File tree

3 files changed

+134
-15
lines changed

3 files changed

+134
-15
lines changed

src/util/deserialize.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ class Deserializer {
159159

160160
_iterateValidRelationships(instance, relationships, callback) {
161161
for (let key in relationships) {
162-
let relationName = camelize(key);
162+
let relationName = key;
163+
164+
if (instance.klass.camelizeKeys) {
165+
relationName = camelize(key)
166+
}
167+
163168
if (instance.klass.attributeList[relationName]) {
164169
let relationData = relationships[key].data;
165170
if(!relationData) continue; // only links, empty, etc

test/fixtures.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let Author = Person.extend({
3535
jsonapiType: 'authors'
3636
},
3737

38-
nilly: attr(),
38+
nilly: attr(),
3939

4040
multiWords: hasMany('multi_words'),
4141
specialBooks: hasMany('books'),
@@ -45,52 +45,64 @@ let Author = Person.extend({
4545
bio: hasOne('bios')
4646
});
4747

48+
let NonFictionAuthor = Author.extend({
49+
static: {
50+
endpoint: '/v1/non_fiction_authors',
51+
jsonapiType: 'non_fiction_authors',
52+
camelizeKeys: false
53+
},
54+
55+
nilly: attr(),
56+
57+
multi_words: hasMany('multi_words'),
58+
special_books: hasMany('books'),
59+
books: hasMany(),
60+
tags: hasMany(),
61+
genre: belongsTo('genres'),
62+
bio: hasOne('bios')
63+
});
64+
4865
class Book extends ApplicationRecord {
4966
static jsonapiType = 'books';
5067

5168
title: string = attr();
5269

5370
genre = belongsTo('genres');
54-
author = hasOne('authors')
71+
author = hasOne('authors');
5572
}
5673

5774
class Genre extends ApplicationRecord {
5875
static jsonapiType = 'genres';
5976

60-
authors: any = hasMany('authors')
77+
authors: any = hasMany('authors');
6178

6279
name: string = attr();
6380
}
6481

6582
class Bio extends ApplicationRecord {
6683
static jsonapiType = 'bios';
6784

68-
description: string = attr()
85+
description: string = attr();
6986
}
7087

7188
class Tag extends ApplicationRecord {
7289
static jsonapiType = 'tags';
7390

74-
name: string = attr()
91+
name: string = attr();
7592
}
7693

7794
class MultiWord extends ApplicationRecord {
7895
static jsonapiType = 'multi_words';
7996
}
8097

81-
const TestJWTSubclass = ApplicationRecord.extend({
82-
});
98+
const TestJWTSubclass = ApplicationRecord.extend({});
8399

84-
const NonJWTOwner = Model.extend({
85-
});
100+
const NonJWTOwner = Model.extend({});
86101

87102
const configSetup = function(opts = {}) {
88-
opts['jwtOwners'] = [
89-
ApplicationRecord,
90-
TestJWTSubclass
91-
]
103+
opts['jwtOwners'] = [ApplicationRecord, TestJWTSubclass];
92104
Config.setup(opts);
93-
}
105+
};
94106
configSetup();
95107

96108
export {
@@ -99,6 +111,7 @@ export {
99111
TestJWTSubclass,
100112
NonJWTOwner,
101113
Author,
114+
NonFictionAuthor,
102115
Person,
103116
PersonWithExtraAttr,
104117
PersonWithoutCamelizedKeys,

test/integration/relations-test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { expect, fetchMock } from '../test-helper';
2+
import { Author, NonFictionAuthor } from '../fixtures';
3+
4+
let resultData = function(promise) {
5+
return promise.then(function(proxyObject) {
6+
return proxyObject.data;
7+
});
8+
};
9+
10+
let generateMockResponse = function(type: string) {
11+
return {
12+
data: {
13+
id: '1',
14+
type: type,
15+
attributes: {
16+
firstName: 'John'
17+
},
18+
relationships: {
19+
books: {
20+
data: [
21+
{
22+
id: 'book1',
23+
type: 'books'
24+
}
25+
]
26+
},
27+
multi_words: {
28+
data: [
29+
{
30+
id: 'multi_word1',
31+
type: 'multi_words'
32+
}
33+
]
34+
}
35+
}
36+
},
37+
included: [
38+
{
39+
id: 'book1',
40+
type: 'books',
41+
attributes: {
42+
title: 'The Shining'
43+
}
44+
},
45+
{
46+
id: 'multi_word1',
47+
type: 'multi_words',
48+
attributes: {}
49+
}
50+
]
51+
};
52+
};
53+
54+
describe('Relations', function() {
55+
describe('#find()', function() {
56+
beforeEach(function() {
57+
fetchMock.get('http://example.com/api/v1/authors/1?include=books,multi_words', generateMockResponse('authors'));
58+
});
59+
60+
afterEach(fetchMock.restore);
61+
62+
it('correctly includes relationships', function(done) {
63+
resultData(Author.includes(['books', 'multi_words']).find(1)).then(data => {
64+
expect(data.multiWords).to.be.an('array');
65+
expect(data.books).to.be.an('array');
66+
67+
done();
68+
});
69+
});
70+
71+
it('contains the right records for each relationship', function(done) {
72+
resultData(Author.includes(['books', 'multi_words']).find(1)).then(data => {
73+
expect(data.books[0].title).to.eql('The Shining');
74+
expect(data.multiWords[0].id).to.eql('multi_word1');
75+
76+
done();
77+
});
78+
});
79+
});
80+
81+
describe('when camelizeKeys is false', function() {
82+
beforeEach(function() {
83+
fetchMock.get(
84+
'http://example.com/api/v1/non_fiction_authors/1?include=books,multi_words',
85+
generateMockResponse('non_fiction_authors')
86+
);
87+
});
88+
89+
afterEach(fetchMock.restore);
90+
91+
it("Doesn't convert relationships to snake_case if camelization is off", function(done) {
92+
resultData(NonFictionAuthor.includes(['books', 'multi_words']).find(1))
93+
.then(data => {
94+
expect(data.multi_words[0].id).to.eql('multi_word1');
95+
96+
done();
97+
})
98+
.catch(done);
99+
});
100+
});
101+
});

0 commit comments

Comments
 (0)