Skip to content

Commit

Permalink
fix(context): disable deep clone of injection metadata
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `metadata` parameter of `@inject` is no longer
cloned deeply. It's still cloned shallowly.
  • Loading branch information
raymondfeng committed Mar 26, 2018
1 parent 544052e commit 7d8a84c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
44 changes: 28 additions & 16 deletions packages/context/src/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,20 @@ export function inject(
// Please note propertyKey is `undefined` for constructor
const paramDecorator: ParameterDecorator = ParameterDecoratorFactory.createDecorator<
Injection
>(PARAMETERS_KEY, {
target,
member,
methodDescriptorOrParameterIndex,
bindingKey,
metadata,
resolve,
});
>(
PARAMETERS_KEY,
{
target,
member,
methodDescriptorOrParameterIndex,
bindingKey,
metadata,
resolve,
},
// Do not deep clone the spec as only metadata is mutable and it's
// shallowly cloned
{cloneInputSpec: false},
);
paramDecorator(target, member!, methodDescriptorOrParameterIndex);
} else if (member) {
// Property or method
Expand All @@ -136,14 +142,20 @@ export function inject(
}
const propDecorator: PropertyDecorator = PropertyDecoratorFactory.createDecorator<
Injection
>(PROPERTIES_KEY, {
target,
member,
methodDescriptorOrParameterIndex,
bindingKey,
metadata,
resolve,
});
>(
PROPERTIES_KEY,
{
target,
member,
methodDescriptorOrParameterIndex,
bindingKey,
metadata,
resolve,
},
// Do not deep clone the spec as only metadata is mutable and it's
// shallowly cloned
{cloneInputSpec: false},
);
propDecorator(target, member!);
} else {
// It won't happen here as `@inject` is not compatible with ClassDecorator
Expand Down
19 changes: 17 additions & 2 deletions packages/context/test/unit/inject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,12 @@ describe('property injection', () => {
class SubTestClass extends TestClass {
@inject('bar') foo: string;
}
const meta = describeInjectedProperties(SubTestClass.prototype);
expect(meta.foo.bindingKey).to.eql('bar');

const base = describeInjectedProperties(TestClass.prototype);
expect(base.foo.bindingKey).to.eql('foo');

const sub = describeInjectedProperties(SubTestClass.prototype);
expect(sub.foo.bindingKey).to.eql('bar');
});

it('supports inherited and own properties', () => {
Expand All @@ -161,4 +165,15 @@ describe('property injection', () => {
expect(meta.foo.bindingKey).to.eql('foo');
expect(meta.bar.bindingKey).to.eql('bar');
});

it('does not clone metadata deeply', () => {
const options = {x: 1};
class TestClass {
@inject('foo', options)
foo: string;
}
const meta = describeInjectedProperties(TestClass.prototype);
expect(meta.foo.metadata).to.be.not.exactly(options);
expect(meta.foo.metadata).to.eql({x: 1, decorator: '@inject'});
});
});

0 comments on commit 7d8a84c

Please sign in to comment.