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 my aggregate ChatConversation contains arrays of aggregate in props :
export interface ConversationProps {
id?: UID;
users: User[]; // User is an Aggregate
messages: ChatMessage[]; // ChatMessage is an Aggregate
isReadByUserId: string[];
createdAt?: Date;
updatedAt?: Date;
}
And an aggregate contains a circular ref in _domainEvents > aggregate. For example :
If i used the package npm flatted for stringify, the problem not appears
For solved the problem, modify the file node_modules/.pnpm/rich-domain@1.23.2/node_modules/rich-domain/utils/validator.js and add the line const {stringify} = require('flatted'); at the top file.
And replace the line 41 if (JSON.stringify(props) === JSON.stringify({}))) to if (stringify(props) === tringify({})) and the test passed !
Describe the bug
I obtained this error :
Because my aggregate
ChatConversation
contains arrays of aggregate in props :And an aggregate contains a circular ref in
_domainEvents > aggregate
. For example :I located the problem in the package
rich-domain
in the fileutils/validator.ts
: https://github.com/4lessandrodev/rich-domain/blob/9764adf70549440dfc4e0058a4a3bb4011e3a92e/lib/utils/validator.ts#L40If i used the package npm flatted for stringify, the problem not appears
For solved the problem, modify the file
node_modules/.pnpm/rich-domain@1.23.2/node_modules/rich-domain/utils/validator.js
and add the lineconst {stringify} = require('flatted');
at the top file.And replace the line 41
if (JSON.stringify(props) === JSON.stringify({}))
) toif (stringify(props) === tringify({}))
and the test passed !I create a pull request for the package
rich-domain
to solve it : https://github.com/4lessandrodev/rich-domain/pull/170When you merged the pull request and increment version of
rich-domain
, up the version ofrich-domain
intype-ddd
To Reproduce I prepared a repository to reproduce the problem : https://github.com/GaetanCottrez/issue-type-ddd-json-stringify
Screenshots https://github.com/user-attachments/assets/c5bbe52a-b0d0-4829-b553-f8e493aa168c