Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/context/src/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PropertyDecoratorFactory,
MetadataMap,
MetadataAccessor,
InspectionOptions,
} from '@loopback/metadata';
import {BoundValue, ValueOrPromise, resolveList} from './value-promise';
import {Context} from './context';
Expand Down Expand Up @@ -299,10 +300,23 @@ export function describeInjectedArguments(
method?: string,
): Readonly<Injection>[] {
method = method || '';
const options: InspectionOptions = {};
if (method === '') {
// A hacky way to check if an explicit constructor exists
// See https://github.com/strongloop/loopback-next/issues/1565
if (target.toString().match(/\s+constructor\s*\([^\)]*\)\s+\{/m)) {
options.ownMetadataOnly = true;
}
} else if (target.hasOwnProperty(method)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code block covered in a test?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Coveralls, this branch is executed several times (14x) by our test suite. However, I cannot tell if there is a test that would fail if this branch was not present.

@raymondfeng could you please add an explicit test for this branch?

// The method exists in the target, no injections on the super method
// should be honored
options.ownMetadataOnly = true;
}
const meta = MetadataInspector.getAllParameterMetadata<Readonly<Injection>>(
PARAMETERS_KEY,
target,
method,
options,
);
return meta || [];
}
Expand Down
14 changes: 14 additions & 0 deletions packages/context/test/unit/inject.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ describe('function argument injection', () => {
const meta = describeInjectedArguments(SubTestClass);
expect(meta.map(m => m.bindingKey)).to.deepEqual(['bar']);
});

it('supports inheritance with overriding constructor - no args', () => {
class TestClass {
constructor(@inject('foo') foo: string) {}
}

class SubTestClass extends TestClass {
constructor() {
super('foo');
}
}
const meta = describeInjectedArguments(SubTestClass);
expect(meta.map(m => m.bindingKey)).to.deepEqual([]);
});
});

describe('property injection', () => {
Expand Down