Skip to content

Commit

Permalink
bugfix(core): Allow circular structures for dynamic module nestjs#678
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Oct 2, 2018
1 parent 65bb7e7 commit 3dd8b03
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
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 3dd8b03

Please sign in to comment.