Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
fix(repository): throw INVALID_INCLUSION_FILTER if relation is not pr…
Browse files Browse the repository at this point in the history
…esent

in the inclusionResolver

GH-0
  • Loading branch information
shubhamp-sf committed Jun 12, 2023
1 parent aa0ad39 commit fa3cd07
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/__tests__/integration/repository.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,20 @@ describe('Sequelize CRUD Repository (integration)', function () {
// Confirming the fact that it used inner join behind the scenes
expect(relationRes.body.length).to.be.equal(1);
});

it('throws error if the repository does not have registered resolvers', async () => {
try {
await userRepo.find({
include: ['nonExistingRelation'],
});
} catch (err) {
expect(err.message).to.be.eql(
`Invalid "filter.include" entries: "nonExistingRelation"`,
);
expect(err.statusCode).to.be.eql(400);
expect(err.code).to.be.eql('INVALID_INCLUSION_FILTER');
}
});
});

describe('Connections', () => {
Expand Down
35 changes: 35 additions & 0 deletions src/sequelize/sequelize.repository.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,22 @@ export class SequelizeCrudRepository<
});
}

/**
* Checks if the resolver of the inclusion relation is registered
* in the inclusionResolver of the current repository
*
* @param include - LoopBack Inclusion filter
*/
protected isInclusionAllowed(include: InclusionFilter): boolean {
const relationName =
typeof include === 'string' ? include : include.relation;
if (!relationName) {
return false;
}
const allowed = this.inclusionResolvers.has(relationName);
return allowed;
}

/**
* Build Sequelize compatible `include` filter
* @param inclusionFilters - loopback style `where` condition
Expand All @@ -473,6 +489,25 @@ export class SequelizeCrudRepository<
sourceModel = this.sequelizeModel;
}

if (sourceModel === this.sequelizeModel) {
const invalidInclusions = inclusionFilters.filter(
inclusionFilter => !this.isInclusionAllowed(inclusionFilter),
);
if (invalidInclusions.length) {
const msg =
'Invalid "filter.include" entries: ' +
invalidInclusions
.map(inclusionFilter => JSON.stringify(inclusionFilter))
.join('; ');
const err = new Error(msg);
Object.assign(err, {
code: 'INVALID_INCLUSION_FILTER',
statusCode: 400,
});
throw err;
}
}

const includable: Includeable[] = [];

for (const filter of inclusionFilters) {
Expand Down

0 comments on commit fa3cd07

Please sign in to comment.