From aeac3393474581c489c5a24334d24101c8127eff Mon Sep 17 00:00:00 2001 From: Nick Bradley Date: Wed, 26 Aug 2026 14:48:18 +0100 Subject: [PATCH] fix: avoid circular reference when normalizing the flattened response shape The flattened response was normalized by inserting the account object into its own product_environments array, which made --json output fail with "Converting circular structure to JSON". Insert a copy instead. Co-Authored-By: Claude Fable 5 --- src/lib/provision.ts | 5 ++++- test/provision.test.mjs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lib/provision.ts b/src/lib/provision.ts index 53ac582..baeec5f 100644 --- a/src/lib/provision.ts +++ b/src/lib/provision.ts @@ -198,7 +198,10 @@ function normalizeAccount(raw: unknown): CloudAccount { return account; } if (typeof account.cloud_name === 'string' && account.cloud_name !== '') { - account.product_environments = [account]; + // Copy, not a self-reference: the account must stay JSON-serializable + // (--json output stringifies it). + const { product_environments: _omit, ...env } = account; + account.product_environments = [env]; return account; } throw new ProvisionError( diff --git a/test/provision.test.mjs b/test/provision.test.mjs index acdc715..c4efbf7 100644 --- a/test/provision.test.mjs +++ b/test/provision.test.mjs @@ -164,6 +164,24 @@ test('normalizes the flattened response shape', async () => { ); }); +test('flattened response normalizes without circular references (--json serializes it)', async () => { + await withStub( + (req, res) => { + req.on('data', () => {}); + req.on('end', () => { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(FLAT_RESPONSE)); + }); + }, + async host => { + const account = await provisionCloud({}, { apiHost: host }); + const serialized = JSON.parse(JSON.stringify(account)); + assert.equal(serialized.product_environments[0].cloud_name, 'cloud-flat'); + assert.equal(serialized.product_environments[0].product_environments, undefined); + }, + ); +}); + test('unrecognized success shape throws with the raw response preserved', async () => { await withStub( (req, res) => {