Skip to content

Commit 9ae0872

Browse files
authored
Merge pull request #33 from salismt/fix/snapbi-minify-json-signature
fix(snapbi): preserve whitespace in JSON string values during minification
2 parents 4488217 + bdacc8e commit 9ae0872

13 files changed

Lines changed: 481 additions & 7 deletions

File tree

.gitignore

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ target/
3232
.classpath
3333
.project
3434
.settings
35+
.factorypath
3536
**/.checkstyle
3637
target/
3738
bin/
@@ -57,4 +58,14 @@ dist
5758
/.mvn/
5859
/mvnw
5960
/mvnw.bat
60-
mvnw.cmd
61+
mvnw.cmd
62+
63+
## GitHub Copilot prompts & skills (OpenSpec-generated):
64+
.github/prompts/
65+
.github/skills/
66+
67+
## OpenCode (AI agent config):
68+
.opencode/
69+
70+
## OpenSpec (spec-driven workflow artifacts):
71+
openspec/

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## CHANGELOG MIDTRANS JAVA LIBRARY
22

3+
## v3.2.2 (April 7, 2026)
4+
5+
Bug fixes:
6+
- Fix `minifyJson()` in SnapBi stripping whitespace inside JSON string values, which caused incorrect SNAP-BI webhook notification signature verification. The method now uses a character-level JSON minifier that preserves whitespace within quoted strings.
7+
- Fix `MidtransError` getter methods not being generated by Lombok on JDK 25. Replaced `@Getter` annotation with explicit getter methods for `statusCode`, `responseBody`, and `response`.
8+
39
## v3.2.1 (October 16, 2024)
410

511
Feature:

example/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<dependency>
4545
<groupId>com.midtrans</groupId>
4646
<artifactId>java-library</artifactId>
47-
<version>3.2.1</version>
47+
<version>3.2.2</version>
4848
<scope>compile</scope>
4949
</dependency>
5050
</dependencies>

library/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.midtrans</groupId>
77
<artifactId>java-library</artifactId>
8-
<version>3.2.1</version>
8+
<version>3.2.2</version>
99
<packaging>jar</packaging>
1010

1111
<name>Midtrans Java Official Library</name>

library/src/main/java/com/midtrans/httpclient/error/MidtransError.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
package com.midtrans.httpclient.error;
22

3-
import lombok.Getter;
43
import okhttp3.Response;
54

65
/**
76
* MidtransError class to catch error messages
87
*/
9-
@Getter
108
public class MidtransError extends Exception {
119

1210
private String message;
1311
private Integer statusCode;
1412
private String responseBody;
1513
private Response response;
1614

15+
@Override
16+
public String getMessage() {
17+
return message;
18+
}
19+
20+
public Integer getStatusCode() {
21+
return statusCode;
22+
}
23+
24+
public String getResponseBody() {
25+
return responseBody;
26+
}
27+
28+
public Response getResponse() {
29+
return response;
30+
}
31+
1732
/**
1833
* Constructs a Midtrans exception with the message
1934
*

library/src/main/java/com/midtrans/snapbi/SnapBi.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,37 @@ public static String getSymmetricSignatureHmacSh512(String accessToken, Map<Stri
295295
}
296296

297297
private static String minifyJson(String json) {
298-
// Minify JSON by removing whitespace
299-
return json.replaceAll("\\s+", "");
298+
if (json == null || json.isEmpty()) {
299+
return "";
300+
}
301+
// Minify JSON by removing insignificant whitespace (outside of string values)
302+
// while preserving whitespace inside quoted strings and maintaining key order.
303+
StringBuilder sb = new StringBuilder(json.length());
304+
boolean inString = false;
305+
boolean escaped = false;
306+
for (int i = 0; i < json.length(); i++) {
307+
char c = json.charAt(i);
308+
if (escaped) {
309+
sb.append(c);
310+
escaped = false;
311+
continue;
312+
}
313+
if (c == '\\' && inString) {
314+
sb.append(c);
315+
escaped = true;
316+
continue;
317+
}
318+
if (c == '"') {
319+
inString = !inString;
320+
sb.append(c);
321+
continue;
322+
}
323+
if (!inString && Character.isWhitespace(c)) {
324+
continue;
325+
}
326+
sb.append(c);
327+
}
328+
return sb.toString();
300329
}
301330

302331
private static byte[] hashSha256(String input) throws NoSuchAlgorithmException {

0 commit comments

Comments
 (0)