Skip to content

Commit

Permalink
fix: return data of field who have the same name as model (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-moncel authored Jan 31, 2022
1 parent fd0b4e0 commit cee9327
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/services/requested-fields-extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ function extractRequestedFields(requestedFields, modelOrAssociation, schemas) {
))
.flat();

const modelFields = requestedFields[modelOrAssociation.name].split(',')
.filter((fieldName) => !requestedFields[fieldName]);
const modelFields = requestedFields[modelOrAssociation.name]
.split(',')
.filter((fieldName) =>
Object.prototype.hasOwnProperty.call(modelOrAssociation.rawAttributes, fieldName));

const smartFields = extractRequestedSmartField(requestedFields, schemas[modelOrAssociation.name]);

Expand Down
63 changes: 63 additions & 0 deletions test/services/requested-fields-extractor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe('services > requested-fields-extractor', () => {
name: 'user',
primaryKeys: { id: null, uid: null },
associations: {},
rawAttributes: {
id: {},
uid: {},
name: {},
},
};

const schemas = {
Expand Down Expand Up @@ -51,6 +56,11 @@ describe('services > requested-fields-extractor', () => {
name: 'user',
primaryKeys: { id: null, uid: null },
associations: {},
rawAttributes: {
id: {},
uid: {},
name: {},
},
};

const schemas = {
Expand All @@ -68,6 +78,38 @@ describe('services > requested-fields-extractor', () => {
expect(result).toStrictEqual(['id', 'name']);
});

it('should include field with same name as the model', () => {
expect.assertions(1);

const fields = {
user: 'id,user',
};

const model = {
name: 'user',
primaryKeys: { id: null },
associations: {},
rawAttributes: {
id: {},
user: {},
},
};

const schemas = {
user: {
name: 'user',
fields: [{
field: 'user',
isVirtual: false,
}],
},
};

const result = extractRequestedFields(fields, model, schemas);

expect(result).toStrictEqual(['id', 'user']);
});

it('should include all associations\' requested fields', () => {
expect.assertions(1);

Expand All @@ -87,6 +129,11 @@ describe('services > requested-fields-extractor', () => {
},
},
},
rawAttributes: {
id: {},
uid: {},
name: {},
},
};

const schemas = {
Expand Down Expand Up @@ -130,6 +177,12 @@ describe('services > requested-fields-extractor', () => {
},
},
},
rawAttributes: {
id: {},
uid: {},
name: {},
account: {},
},
};

const schemas = {
Expand Down Expand Up @@ -180,6 +233,12 @@ describe('services > requested-fields-extractor', () => {
},
},
},
rawAttributes: {
id: {},
uid: {},
name: {},
account: {},
},
};

const schemas = {
Expand Down Expand Up @@ -222,6 +281,10 @@ describe('services > requested-fields-extractor', () => {
name: 'user',
primaryKeys: { id: null, uid: null },
associations: {},
rawAttributes: {
id: {},
uid: {},
},
};

const schemas = {
Expand Down

0 comments on commit cee9327

Please sign in to comment.