Skip to content

feat: handle circular structure serialization#7275

Open
leandro-costa-oliveira wants to merge 1 commit into
expressjs:masterfrom
leandro-costa-oliveira:feat-circular-structure
Open

feat: handle circular structure serialization#7275
leandro-costa-oliveira wants to merge 1 commit into
expressjs:masterfrom
leandro-costa-oliveira:feat-circular-structure

Conversation

@leandro-costa-oliveira
Copy link
Copy Markdown

@leandro-costa-oliveira leandro-costa-oliveira commented May 22, 2026

Problem

res.json in lib/response.js calls stringify which fails if a object with circular structure is passed.

This is pretty common when you have calls using axios lib, the axios errors include the response object which have circular structure. Since a bunch a libs rely on axios, this is a pretty common cause of issues.

Example of Stack Trace

Doing a soap call with routing-controllers on top of express, fails to parse the error.

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Agent'

--- property 'agent' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (/app/node_modules/express/lib/response.js:1034:12)
    at ServerResponse.json (/app/node_modules/express/lib/response.js:245:14)
    at ExpressDriver.handleError (/app/node_modules/routing-controllers/cjs/driver/express/ExpressDriver.js:351:26)
    at /app/node_modules/routing-controllers/cjs/RoutingControllers.js:115:36
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5)

Tests

Added tests in test/res.json and test/res.jsonp simulating the circular structure.

@leandro-costa-oliveira leandro-costa-oliveira force-pushed the feat-circular-structure branch 3 times, most recently from 0716583 to 1ea6f49 Compare May 22, 2026 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant