Skip to content

Commit d61da0c

Browse files
fix: nested include duplication in smart field search (#863)
1 parent c986333 commit d61da0c

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

src/services/search-builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
8585
try {
8686
await field.search(query, params.search);
8787
} catch (error) {
88-
const errorMessage = `Cannot search properly on Smart Field ${field.field}`;
88+
const errorMessage = `Cannot search properly on Smart Field ${field.field}: `;
8989
Interface.logger.error(errorMessage, error);
9090
}
9191
return Promise.resolve();

src/utils/sequelize-compatibility.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ function removeDuplicateAssociations(model, includeList) {
133133

134134
// Recurse
135135
includeList.forEach((include) => {
136-
if (include.include) {
137-
const association = model.associations[include.as];
136+
const association = model.associations[include.as];
137+
if (include.include && association) {
138138
removeDuplicateAssociations(association.target, include.include);
139139
}
140140
});

test/services/query-options.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('services > query-options', () => {
8484

8585
const options = new QueryOptions(model);
8686
await options.search('search string', null);
87-
expect(loggerErrorSpy).toHaveBeenCalledWith('Cannot search properly on Smart Field smartField', errorThrown);
87+
expect(loggerErrorSpy).toHaveBeenCalledWith('Cannot search properly on Smart Field smartField: ', errorThrown);
8888

8989
loggerErrorSpy.mockClear();
9090
resetSchema();
@@ -122,7 +122,7 @@ describe('services > query-options', () => {
122122

123123
const options = new QueryOptions(model);
124124
await options.search('search string', null);
125-
expect(loggerErrorSpy).toHaveBeenCalledWith('Cannot search properly on Smart Field smartField', errorThrown);
125+
expect(loggerErrorSpy).toHaveBeenCalledWith('Cannot search properly on Smart Field smartField: ', errorThrown);
126126

127127
loggerErrorSpy.mockClear();
128128
resetSchema();

test/utils/sequelize-compatibility.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,26 @@ describe('utils > sequelize-compatibility', () => {
252252
});
253253
});
254254
});
255+
256+
describe('postProcess -> removeDuplicateAssociations', () => {
257+
describe('when include alias is not valid', () => {
258+
it('should not throw an error', () => {
259+
expect.assertions(1);
260+
261+
const include = [{ model: SubModel, as: 'notAValidAlias', include: SubModel }];
262+
263+
expect(() => postProcess(Model, { include })).not.toThrow();
264+
});
265+
});
266+
267+
describe('when the main model is reincluded', () => {
268+
it('should not throw an error', () => {
269+
expect.assertions(1);
270+
271+
const include = [{ model: Model, include: SubSubModel1 }];
272+
273+
expect(() => postProcess(Model, { include })).not.toThrow();
274+
});
275+
});
276+
});
255277
});

0 commit comments

Comments
 (0)