Skip to content

Commit

Permalink
bugfix(core): Allow circular structures for dynamic module #678
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Oct 3, 2018
1 parent 65bb7e7 commit 958da96
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'chai';
import { Test } from '@nestjs/testing';
import { CircularModule } from '../src/circular-structure-dynamic-module/circular.module';
import { InputService } from '../src/circular-structure-dynamic-module/input.service';

describe('Circular structure for dynamic modules', () => {
it('should resolve circular structure with dynamic modules', async () => {
const builder = Test.createTestingModule({
imports: [CircularModule.forRoot()],
});
const testingModule = await builder.compile();
const inputService = testingModule.get<InputService>(InputService);

expect(inputService).to.be.instanceof(InputService);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DynamicModule } from '@nestjs/common';
import { InputService } from './input.service';

export class CircularModule {
static forRoot(): DynamicModule {
const a = {
module: CircularModule,
providers: [
InputService,
],
b: null,
};
a.b = a;
return a;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class InputService {
}
5 changes: 4 additions & 1 deletion packages/core/injector/module-token-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DynamicModule } from '@nestjs/common';
import { SHARED_MODULE_METADATA } from '@nestjs/common/constants';
import { Type } from '@nestjs/common/interfaces/type.interface';
import * as hash from 'object-hash';
import safeStringify from 'fast-safe-stringify';

export class ModuleTokenFactory {
public create(
Expand All @@ -22,7 +23,9 @@ export class ModuleTokenFactory {
public getDynamicMetadataToken(
dynamicModuleMetadata: Partial<DynamicModule> | undefined,
): string {
return dynamicModuleMetadata ? JSON.stringify(dynamicModuleMetadata) : '';
// Uses safeStringify instead of JSON.stringify
// to support circular dynamic modules
return dynamicModuleMetadata ? safeStringify(dynamicModuleMetadata) : '';
}

public getModuleName(metatype: Type<any>): string {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/test/injector/module-token-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai';
import * as hash from 'object-hash';
import { SingleScope } from '../../../common';
import { ModuleTokenFactory } from '../../injector/module-token-factory';
import safeStringify from 'fast-safe-stringify';

describe('ModuleTokenFactory', () => {
let factory: ModuleTokenFactory;
Expand Down Expand Up @@ -46,7 +47,7 @@ describe('ModuleTokenFactory', () => {
expect(token).to.be.deep.eq(
hash({
module: Module.name,
dynamic: JSON.stringify({
dynamic: safeStringify({
components: [{}],
}),
scope: [Module.name],
Expand Down

0 comments on commit 958da96

Please sign in to comment.