Skip to content

Commit 1a5320a

Browse files
Merge pull request #216 from Adyen/PW-1346-byte-array-serialisation
Fix base64 byte array serialization
2 parents eaca843 + 48f5af2 commit 1a5320a

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

src/main/java/com/adyen/model/binlookup/DSPublicKeyDetail.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import java.util.Objects;
2424

25-
import com.adyen.serializer.ByteArrayToBase64TypeAdapter;
25+
import com.adyen.serializer.ByteArrayToStringAdapter;
2626
import com.google.gson.annotations.JsonAdapter;
2727
import com.google.gson.annotations.SerializedName;
2828

@@ -39,7 +39,7 @@ public class DSPublicKeyDetail {
3939
private String directoryServerId = null;
4040

4141
@SerializedName("publicKey")
42-
@JsonAdapter(ByteArrayToBase64TypeAdapter.class)
42+
@JsonAdapter(ByteArrayToStringAdapter.class)
4343
private byte[] publicKey = null;
4444

4545
public DSPublicKeyDetail brand(String brand) {

src/main/java/com/adyen/model/checkout/ThreeDSecureData.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.io.IOException;
2525
import java.util.Objects;
26+
27+
import com.adyen.serializer.ByteArrayToStringAdapter;
2628
import com.google.gson.TypeAdapter;
2729
import com.google.gson.annotations.JsonAdapter;
2830
import com.google.gson.annotations.SerializedName;
@@ -37,6 +39,7 @@ public class ThreeDSecureData {
3739
@SerializedName("authenticationResponse")
3840
private AuthenticationResponseEnum authenticationResponse = null;
3941
@SerializedName("cavv")
42+
@JsonAdapter(ByteArrayToStringAdapter.class)
4043
private byte[] cavv = null;
4144
@SerializedName("cavvAlgorithm")
4245
private String cavvAlgorithm = null;
@@ -47,6 +50,7 @@ public class ThreeDSecureData {
4750
@SerializedName("threeDSVersion")
4851
private String threeDSVersion = null;
4952
@SerializedName("xid")
53+
@JsonAdapter(ByteArrayToStringAdapter.class)
5054
private byte[] xid = null;
5155
@SerializedName("dsTransID")
5256
private String dsTransID = null;

src/main/java/com/adyen/serializer/ByteArrayToBase64TypeAdapter.java renamed to src/main/java/com/adyen/serializer/ByteArrayToStringAdapter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@
2828
import com.google.gson.JsonPrimitive;
2929
import com.google.gson.JsonSerializationContext;
3030
import com.google.gson.JsonSerializer;
31-
import org.apache.commons.codec.binary.Base64;
3231

3332
import java.lang.reflect.Type;
3433

35-
public class ByteArrayToBase64TypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
34+
public class ByteArrayToStringAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
3635
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
37-
return Base64.decodeBase64(json.getAsString());
36+
return json.getAsString().getBytes();
3837
}
3938

4039
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
41-
return new JsonPrimitive(Base64.encodeBase64String(src));
40+
return new JsonPrimitive(new String(src));
4241
}
4342
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* ######
3+
* ######
4+
* ############ ####( ###### #####. ###### ############ ############
5+
* ############# #####( ###### #####. ###### ############# #############
6+
* ###### #####( ###### #####. ###### ##### ###### ##### ######
7+
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
8+
* ###### ###### #####( ###### #####. ###### ##### ##### ######
9+
* ############# ############# ############# ############# ##### ######
10+
* ############ ############ ############# ############ ##### ######
11+
* ######
12+
* #############
13+
* ############
14+
*
15+
* Adyen Java API Library
16+
*
17+
* Copyright (c) 2019 Adyen B.V.
18+
* This file is open source and available under the MIT license.
19+
* See the LICENSE file for more info.
20+
*/
21+
package com.adyen.serializer;
22+
23+
import com.google.gson.JsonElement;
24+
import com.google.gson.JsonPrimitive;
25+
import org.apache.commons.codec.binary.Base64;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.junit.MockitoJUnitRunner;
29+
30+
import static org.junit.Assert.assertArrayEquals;
31+
import static org.junit.Assert.assertEquals;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class ByteArrayToStringAdapterTest {
35+
36+
@Test
37+
public void testSerialize() {
38+
ByteArrayToStringAdapter adapter = new ByteArrayToStringAdapter();
39+
byte[] base64Bytes = Base64.encodeBase64("Bytes-To-Be-Encoded".getBytes());
40+
41+
JsonElement serializedElement = adapter.serialize(base64Bytes, null, null);
42+
assertEquals("Qnl0ZXMtVG8tQmUtRW5jb2RlZA==", serializedElement.getAsString());
43+
}
44+
45+
@Test
46+
public void testDeserialize() {
47+
ByteArrayToStringAdapter adapter = new ByteArrayToStringAdapter();
48+
byte[] base64Bytes = Base64.encodeBase64("Bytes-To-Be-Encoded".getBytes());
49+
50+
JsonElement element = new JsonPrimitive("Qnl0ZXMtVG8tQmUtRW5jb2RlZA==");
51+
byte[] deserializedBytes = adapter.deserialize(element, null, null);
52+
53+
assertArrayEquals(deserializedBytes, base64Bytes);
54+
}
55+
}

0 commit comments

Comments
 (0)