Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Package custom helpers provides utilities to easy usage of Formance API.
// This file is NOT auto-generated and can be freely modified.
package com.formance.formance_sdk.utils.custom_helpers;

import java.util.Base64;
import java.nio.charset.StandardCharsets;

public class PaymentsAccountId {
/**
* Creates an account ID string
* @param connectorId The ID of the connector
* @param reference The identifier of the account from the connector's provider.
* @return The identifier for Formance payments account.
* @throws IllegalArgumentException if connectorId or reference is null/blank, or if connectorId is invalid.
*
*/
public static String Build(String connectorId, String reference) {
if (connectorId == null || connectorId.isBlank()) {
throw new IllegalArgumentException("connectorId must not be null or blank");
}
if (reference == null || reference.isBlank()) {
throw new IllegalArgumentException("reference must not be null or blank");
}

String connectorIdJson = new String(Base64.getDecoder().decode(connectorId), StandardCharsets.UTF_8);
String json = String.format("{\"ConnectorID\":%s,\"Reference\":\"%s\"}", connectorIdJson, reference.replace("\\", "\\\\").replace("\"", "\\\""));
Comment on lines +25 to +26
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# First, locate and examine the PaymentsAccountId.java file
fd "PaymentsAccountId.java" --type f

Repository: formancehq/formance-sdk-java

Length of output: 155


🏁 Script executed:

# Check if the file exists and read its content
cat -n "src/main/java/com/formance/formance_sdk/utils/custom_helpers/PaymentsAccountId.java"

Repository: formancehq/formance-sdk-java

Length of output: 1948


🏁 Script executed:

# Search for JSON-related imports and utilities in the codebase
rg "import.*json|import.*JSON" --type java | head -20

Repository: formancehq/formance-sdk-java

Length of output: 367


🏁 Script executed:

# Check if there are existing JSON escaping patterns in the codebase
rg "escapeJson|JsonEscape|replace.*\\\\\\\\|StandardCharsets" --type java -B 2 -A 2 | head -40

Repository: formancehq/formance-sdk-java

Length of output: 4084


Escape JSON control characters in reference to prevent malformed IDs.

The current escaping only handles backslash and quote. Control characters like newline, tab, and other characters below 0x20 are not escaped, resulting in invalid JSON when such characters appear in reference. This breaks the account ID format expected by the Formance API.

Add a helper method to properly escape all JSON-required characters:

Suggested fix (local escape helper)
-        String json = String.format("{\"ConnectorID\":%s,\"Reference\":\"%s\"}", connectorIdJson, reference.replace("\\", "\\\\").replace("\"", "\\\""));
+        String json = String.format("{\"ConnectorID\":%s,\"Reference\":\"%s\"}", connectorIdJson, escapeJsonString(reference));
@@
     public static String Build(String connectorId, String reference) {
@@
     }
+
+    private static String escapeJsonString(String input) {
+        StringBuilder sb = new StringBuilder(input.length() + 16);
+        for (int i = 0; i < input.length(); i++) {
+            char c = input.charAt(i);
+            switch (c) {
+                case '\\': sb.append("\\\\"); break;
+                case '"': sb.append("\\\""); break;
+                case '\b': sb.append("\\b"); break;
+                case '\f': sb.append("\\f"); break;
+                case '\n': sb.append("\\n"); break;
+                case '\r': sb.append("\\r"); break;
+                case '\t': sb.append("\\t"); break;
+                default:
+                    if (c < 0x20) {
+                        sb.append(String.format("\\u%04x", (int) c));
+                    } else {
+                        sb.append(c);
+                    }
+            }
+        }
+        return sb.toString();
+    }
🤖 Prompt for AI Agents
In
`@src/main/java/com/formance/formance_sdk/utils/custom_helpers/PaymentsAccountId.java`
around lines 25 - 26, The JSON building in PaymentsAccountId currently only
escapes backslashes and quotes for `reference`, which misses control characters
and can produce invalid JSON; add a private helper method (e.g.,
escapeJson(String s) in the PaymentsAccountId class) that escapes backslash and
quote and converts control characters (\b, \f, \n, \r, \t) and any codepoint <
0x20 into their proper JSON escape sequences (use \u00XX for other control
chars), then replace the direct replace(...) call and pass
`escapeJson(reference)` into the String.format call that builds `json` (the code
referencing `connectorIdJson` and `reference`).


try {
return Base64.getEncoder().withoutPadding().encodeToString(json.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
throw new IllegalArgumentException("Failed to build account ID. Please check if the connectorId is valid.", e);
}
}
}