Skip to content
This repository was archived by the owner on Apr 6, 2025. It is now read-only.

GaetanCottrez/issue-type-ddd-json-stringify

Repository files navigation

Reproduce the issue

Clone this repository:

git clone https://github.com/GaetanCottrez/issue-type-ddd-json-stringify.git

Install dependencies:

pnpm i

Run tests:

pnpm run test:watch

The test failed :

TypeError: Converting circular structure to JSON
        --> starting at object with constructor 'User'
        |     property '_domainEvents' -> object with constructor 'TsEvents'
        --- property 'aggregate' closes the circle
        at JSON.stringify (<anonymous>)

Because the aggregate ChatConversation contains other aggregate in props :

export interface ConversationProps {
	id?: UID;
	users: User[];
	messages: ChatMessage[];
	isReadByUserId: string[];
	createdAt?: Date;
	updatedAt?: Date;
}

And an aggregate contains a circular ref in _domainEvents > aggregate :

User {
  props: {
    createdAt: 2022-01-01T00:00:00.000Z,
    updatedAt: 2022-01-01T00:00:00.000Z,
    id: [ID],
    name: 'John Doe',
    email: '[email protected]'
  },
  validator: Validator {},
  util: Utils {},
  parentName: 'Entity',
  config: { disableGetters: false, disableSetters: true },
  _id: ID {
    _value: 'valid_user_id',
    _isNew: false,
    _createdAt: 2024-07-16T17:17:03.392Z,
    MAX_SIZE: 16
  },
  autoMapper: AutoMapper { validator: Validator {} },
  _domainEvents: TsEvents {
    aggregate: [Circular *1],
    _metrics: [Object],
    _events: [],
    totalDispatched: 0
  },
  _dispatchEventsAmount: 0
}

I located the problem in the package rich-domain in the file utils/validator.ts: https://github.com/4lessandrodev/rich-domain/blob/9764adf70549440dfc4e0058a4a3bb4011e3a92e/lib/utils/validator.ts#L40

If i used the package npm flatted who manage perfecly the circular ref if you stringify a JSON.

For solved the problem, modify the file node_modules/.pnpm/[email protected]/node_modules/rich-domain/utils/validator.js and add the line const {stringify} = require('flatted'); at the top file.

And replace the line 39 if (JSON.stringify(props) === JSON.stringify({}))) to if (stringify(props) === tringify({})) and the test passed !

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published