Skip to content

Commit

Permalink
fix: 修复循环引用bug (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurten authored Mar 12, 2020
1 parent f0f3895 commit 8852c6c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
12 changes: 10 additions & 2 deletions packages/midway-core/src/context/managedResolverFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,17 @@ export class ManagedResolverFactory {
return true;
}
for (const key of keys) {
let subDefinition = this.context.registry.getDefinition(key);
let iden = key;
const ref: ManagedReference = definition.properties.get(key);
if (ref && ref.name) {
iden = ref.name;
}
if (iden === identifier) {
return true;
}
let subDefinition = this.context.registry.getDefinition(iden);
if (!subDefinition && this.context.parent) {
subDefinition = this.context.parent.registry.getDefinition(key);
subDefinition = this.context.parent.registry.getDefinition(iden);
}
if (this.depthFirstSearch(identifier, subDefinition)) {
return true;
Expand Down
28 changes: 25 additions & 3 deletions packages/midway-core/test/context/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { childAsyncFunction,
singletonFactory } from '../fixtures/fun_sample';
import { DieselCar, DieselEngine, engineFactory, PetrolEngine } from '../fixtures/mix_sample';
import { HelloSingleton, HelloErrorInitSingleton, HelloErrorSingleton } from '../fixtures/singleton_sample';
import { CircularOne, CircularTwo, CircularThree, TestOne, TestTwo, TestThree } from '../fixtures/circular_dependency';
import { CircularOne, CircularTwo, CircularThree, TestOne, TestTwo, TestThree, TestOne1, TestTwo1, TestThree1 } from '../fixtures/circular_dependency';
import { ManagedValue } from '../../src/context/managed';
import { VALUE_TYPE } from '../../src';

Expand Down Expand Up @@ -316,9 +316,8 @@ describe('/test/context/container.test.ts', () => {
});

describe('circular dependency', () => {
const container = new Container();

it('circular should be ok', async () => {
const container = new Container();
container.registerObject('ctx', {});

container.bind(CircularOne);
Expand All @@ -345,6 +344,29 @@ describe('/test/context/container.test.ts', () => {
expect(circularTwoSync.ttest2('try ttest2')).eq('try ttest2twoone');
expect(await circularTwoSync.ctest2('try ttest2')).eq('try ttest2twoone');
});

it('alias circular should be ok', async () => {
const container = new Container();
container.registerObject('ctx', {});

container.bind(TestOne1);
container.bind(TestTwo1);
container.bind(TestThree1);
container.bind(CircularOne);
container.bind(CircularTwo);
container.bind(CircularThree);

const circularTwo: CircularTwo = await container.getAsync(CircularTwo);
expect(circularTwo.test2).eq('this is two');
expect((circularTwo.circularOne as CircularOne).test1).eq('this is one');
expect(((circularTwo.circularOne as CircularOne).circularTwo as CircularTwo).test2).eq('this is two');

const one = await container.getAsync<TestOne1>(TestOne1);
expect(one).not.null;
expect(one).not.undefined;
expect(one.name).eq('one');
expect((one.two as TestTwo1).name).eq('two');
});
});

describe('circular dependency sync', () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/midway-core/test/fixtures/circular_dependency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import {Provide, Inject, Scope} from '@midwayjs/decorator';
import { ScopeEnum } from '../../src';
@Provide()
export class TestOne1 {
name = 'one';

@Inject('testTwo1')
two: any;
}
@Provide()
export class TestTwo1 {
name = 'two';

@Inject('testOne1')
testOne: any;
}
@Provide()
export class TestThree1 {
name = 'three';

@Inject('testTwo1')
two: any;
}
@Provide()
@Scope(ScopeEnum.Request)
export class CircularTwo {
Expand Down

0 comments on commit 8852c6c

Please sign in to comment.