Skip to content

Commit 9f3ed74

Browse files
zuhairkhalid229achrinza
authored andcommitted
fix(context): skip source scan for classes without a base class
describeInjectedArguments reads the class source and runs regular expressions to decide whether constructor injections are inherited. This also runs for a class with no base class, where nothing can be inherited, so applications that inspect many classes pay for it at startup. Check for a base class first and scan only when one exists. A class can be linked to a base class only through its prototype, and Reflect.getMetadata follows that link, so the check covers this case too. Results stay the same for such classes. Fixes #11878 Signed-off-by: zuhairkhalid229 <zuhairkhalid229@gmail.com>
1 parent 6615311 commit 9f3ed74

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎packages/context/src/__tests__/unit/inject.unit.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// License text available at https://opensource.org/licenses/MIT
55

66
import {expect} from '@loopback/testlab';
7+
import {inherits} from 'util';
78
import {
89
Constructor,
910
describeInjectedArguments,
@@ -105,6 +106,48 @@ describe('function argument injection', () => {
105106
expect(meta.map(m => m.bindingSelector)).to.deepEqual([]);
106107
});
107108

109+
it('does not read the source of a class without a base class', () => {
110+
let sourceRead = false;
111+
class TestClass {
112+
static toString() {
113+
sourceRead = true;
114+
return '';
115+
}
116+
117+
constructor(@inject('foo') foo: string) {}
118+
}
119+
120+
const meta = describeInjectedArguments(TestClass);
121+
expect(meta.map(m => m.bindingSelector)).to.deepEqual(['foo']);
122+
expect(sourceRead).to.be.false();
123+
});
124+
125+
it('does not inherit ctor args through a linked prototype chain', () => {
126+
class BaseClass {
127+
constructor(@inject('foo') foo: string) {}
128+
}
129+
130+
class TestClass {
131+
constructor(foo: string) {}
132+
}
133+
Object.setPrototypeOf(TestClass.prototype, BaseClass.prototype);
134+
135+
const meta = describeInjectedArguments(TestClass);
136+
expect(meta).to.deepEqual([]);
137+
});
138+
139+
it('inherits ctor args for a function linked with util.inherits', () => {
140+
class BaseClass {
141+
constructor(@inject('foo') foo: string) {}
142+
}
143+
144+
function testFunction() {}
145+
inherits(testFunction, BaseClass);
146+
147+
const meta = describeInjectedArguments(testFunction);
148+
expect(meta.map(m => m.bindingSelector)).to.deepEqual(['foo']);
149+
});
150+
108151
// https://github.com/loopbackio/loopback-next/issues/2946
109152
it('allows custom decorator that returns a new constructor', () => {
110153
class HelloController {

‎packages/context/src/inject.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,20 @@ function findOrCreateBindingForInjection(
500500
return binding;
501501
}
502502

503+
/**
504+
* Check if the given class can inherit metadata from a base class. Besides
505+
* `extends`, `Reflect.getMetadata` also follows a base class that is linked
506+
* only through the `prototype` of the class.
507+
*
508+
* @param targetClass - Target class
509+
*/
510+
function hasBaseClass(targetClass: Object) {
511+
if (Object.getPrototypeOf(targetClass) !== Function.prototype) return true;
512+
const proto = (targetClass as {prototype?: object}).prototype;
513+
const baseProto = proto && Object.getPrototypeOf(proto);
514+
return baseProto != null && baseProto !== Object.prototype;
515+
}
516+
503517
/**
504518
* Check if constructor injection should be applied to the base class
505519
* of the given target class
@@ -589,7 +603,7 @@ export function describeInjectedArguments(
589603
// Build the description
590604
const options: InspectionOptions = {};
591605
if (method === '') {
592-
if (shouldSkipBaseConstructorInjection(target)) {
606+
if (hasBaseClass(target) && shouldSkipBaseConstructorInjection(target)) {
593607
options.ownMetadataOnly = true;
594608
}
595609
} else if (Object.prototype.hasOwnProperty.call(target, method)) {

0 commit comments

Comments
 (0)