Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Fix ConnectionRequest serialization to handle cyclic references (#13)
* feat: add toJSON and toString methods to ConnectionRequest class Co-Authored-By: musthaq@composio.dev <musthu.gm@gmail.com> * refactor: simplify toJSON and pretty-print toString as requested in PR review Co-Authored-By: musthaq@composio.dev <musthu.gm@gmail.com> * test: add unit tests for ConnectionRequest serialization methods Co-Authored-By: musthaq@composio.dev <musthu.gm@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: musthaq@composio.dev <musthu.gm@gmail.com>
devin-ai-integration[bot] committed
May 24, 2025 at 12:01 UTC
6efc5c5a2b5c679541704dab99e002eb6a2a6c39
2 files changed
+80
packages/core/src/models/ConnectionRequest.ts
+19
@@ -169,6 +169,25 @@ export class ConnectionRequest {
169
170
throw new ConnectionRequestTimeoutError(`Connection request timed out for ${this.id}`);
171
}
172
+
173
+ /**
174
+ * Returns a JSON-serializable representation of the connection request
175
+ * Excludes the private client property to avoid cyclic reference issues
176
+ */
177
+ toJSON(): Record<string, unknown> {
178
+ return {
179
+ id: this.id,
180
+ status: this.status,
181
+ redirectUrl: this.redirectUrl,
182
+ };
183
+ }
184
+
185
+ /**
186
+ * Returns a string representation of the connection request
187
+ */
188
+ toString(): string {
189
+ return JSON.stringify(this.toJSON(), null, 2);
190
+ }
191
}
192
193
export const connectionRequest = (client: ComposioClient) => {
packages/core/test/connectedAccounts/connectionRequest.test.ts
+61
@@ -217,4 +217,65 @@ describe('ConnectionRequest', () => {
217
await expect(connectionPromise).rejects.toBe(apiError);
218
});
219
});
220
+
221
+ describe('serialization methods', () => {
222
+ it('should return a JSON-serializable object with toJSON()', () => {
223
+ // Create a connection request instance
224
+ connectionRequest = new ConnectionRequest(
225
+ mockClient as unknown as ComposioClient,
226
+ connectedAccountId,
227
+ ConnectedAccountStatuses.INITIATED,
228
+ redirectUrl
229
+ );
230
+
231
+ const jsonObj = connectionRequest.toJSON();
232
+
233
+ expect(jsonObj).toHaveProperty('id', connectedAccountId);
234
+ expect(jsonObj).toHaveProperty('status', ConnectedAccountStatuses.INITIATED);
235
+ expect(jsonObj).toHaveProperty('redirectUrl', redirectUrl);
236
+
237
+ expect(jsonObj).not.toHaveProperty('client');
238
+ });
239
+
240
+ it('should return a formatted JSON string with toString()', () => {
241
+ // Create a connection request instance
242
+ connectionRequest = new ConnectionRequest(
243
+ mockClient as unknown as ComposioClient,
244
+ connectedAccountId,
245
+ ConnectedAccountStatuses.INITIATED,
246
+ redirectUrl
247
+ );
248
+
249
+ const jsonString = connectionRequest.toString();
250
+
251
+ expect(() => JSON.parse(jsonString)).not.toThrow();
252
+
253
+ const parsedObj = JSON.parse(jsonString);
254
+ expect(parsedObj).toHaveProperty('id', connectedAccountId);
255
+ expect(parsedObj).toHaveProperty('status', ConnectedAccountStatuses.INITIATED);
256
+ expect(parsedObj).toHaveProperty('redirectUrl', redirectUrl);
257
+
258
+ expect(jsonString).toContain('\n');
259
+ expect(jsonString).toContain(' ');
260
+ });
261
+
262
+ it('should be serializable with JSON.stringify without cyclic reference errors', () => {
263
+ // Create a connection request instance
264
+ connectionRequest = new ConnectionRequest(
265
+ mockClient as unknown as ComposioClient,
266
+ connectedAccountId,
267
+ ConnectedAccountStatuses.INITIATED,
268
+ redirectUrl
269
+ );
270
+
271
+ expect(() => JSON.stringify(connectionRequest)).not.toThrow();
272
+
273
+ const stringified = JSON.stringify(connectionRequest);
274
+ const parsed = JSON.parse(stringified);
275
+
276
+ expect(parsed).toHaveProperty('id', connectedAccountId);
277
+ expect(parsed).toHaveProperty('status', ConnectedAccountStatuses.INITIATED);
278
+ expect(parsed).toHaveProperty('redirectUrl', redirectUrl);
279
+ });
280
+ });
281
});