Skip to content

Fix union types deserialization with transfer webhooks #1489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/__tests__/transferWebhook.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import BankingWebhookHandler from "../notification/bankingWebhookHandler";
import { EstimationTrackingData } from "../typings/transferWebhooks/estimationTrackingData";

describe("Transfer Webhook Serialization", (): void => {
it("should correctly deserialize union types in transfer webhooks", () => {
// Simplest possible webhook with just the fields we need to test
const webhookData = {
data: {
id: "test-transfer-id",
tracking: {
estimatedArrivalTime: "2025-04-23T22:30:00+02:00",
type: "estimation",
},
},
type: "balancePlatform.transfer.updated",
};

const jsonString = JSON.stringify(webhookData);
const bankingWebhookHandler = new BankingWebhookHandler(jsonString);
const transferNotification =
bankingWebhookHandler.getTransferNotificationRequest();

if (transferNotification.data.tracking?.type === "estimation") {
// Verify that the tracking object is properly deserialized to the correct type
expect(transferNotification.data.tracking).toBeInstanceOf(
EstimationTrackingData
);

// Verify that estimatedArrivalTime is properly converted to a Date object
expect(
transferNotification.data.tracking.estimatedArrivalTime
).toBeInstanceOf(Date);
}
});
});
19 changes: 19 additions & 0 deletions src/typings/transferWebhooks/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ export class ObjectSerializer {
return expectedType;
}

// Handle union types
if (expectedType.includes(" | ")) {
const unionTypes = expectedType.split(" | ").map((t) => t.trim());

// For tracking field specifically, use the 'type' field to determine the actual type
if (data && data.type && unionTypes.some((t) => t.includes("TrackingData"))) {
if (data.type === "estimation") return "EstimationTrackingData";
if (data.type === "confirmation") return "ConfirmationTrackingData";
if (data.type === "internalReview") return "InternalReviewTrackingData";
}

// For other union types, return the first non-null type that exists in typeMap
for (const type of unionTypes) {
if (type !== "null" && typeMap[type]) {
return type;
}
}
}

if (!typeMap[expectedType]) {
return expectedType; // w/e we don't know the type
}
Expand Down