Skip to content

Commit

Permalink
fix byte array deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jillingk committed Sep 15, 2023
1 parent 943dcfb commit 840299b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/adyen/model/marketpayaccount/JSON.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.adyen.model.marketpayaccount;

import com.adyen.serializer.ByteArrayDeserializer;
import com.adyen.serializer.ByteArraySerializer;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
Expand Down Expand Up @@ -33,6 +34,7 @@ public JSON() {
// Custom ByteSerializer
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(byte[].class, new ByteArraySerializer());
simpleModule.addDeserializer(byte[].class, new ByteArrayDeserializer());
mapper.registerModule(simpleModule);
}

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/adyen/serializer/ByteArrayDeserializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.adyen.serializer;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ByteArrayDeserializer extends StdDeserializer<byte[]> {
public ByteArrayDeserializer() {
super(byte[].class);
}

@Override
public byte[] deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
return jsonParser.getValueAsString().getBytes(StandardCharsets.UTF_8);
}
}
3 changes: 2 additions & 1 deletion src/test/java/com/adyen/MarketPayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public void testAccountApi() throws IOException, ApiException, HTTPClientExcepti
"}");
String jsonRequest = request.toJson();
GetTaxFormResponse response = GetTaxFormResponse.fromJson("{\n" +
" \"content\": \"AQIDBAUGBwgJCgsMDQ4PEBESExQ=\",\n" +
" \"contentType\": \"String\",\n" +
" \"invalidFields\": [\n" +
" {\n" +
Expand All @@ -135,8 +136,8 @@ public void testAccountApi() throws IOException, ApiException, HTTPClientExcepti
" \"resultCode\": \"string\"\n" +
"}");
// For some reason setting this content byte [] is broken
// response.setContent("U29tZXJhbmRvbXN0cmluZ0Jhc2U2NEVuY29kZWQ".getBytes(StandardCharsets.UTF_8));
String json = response.toJson();
System.out.println(new String(response.getContent()));
Client client = createMockClientFromResponse(json);
ClassicPlatformAccountApi api = new ClassicPlatformAccountApi(client);

Expand Down
2 changes: 2 additions & 0 deletions templates/libraries/jersey3/JSON.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package {{modelPackage}};

import com.adyen.serializer.ByteArraySerializer;
import com.adyen.serializer.ByteArrayDeserializer;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class JSON implements ContextResolver<ObjectMapper> {
// Custom ByteSerializer
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(byte[].class, new ByteArraySerializer());
simpleModule.addDeserializer(byte[].class, new ByteArrayDeserializer());
mapper.registerModule(simpleModule);
}

Expand Down

0 comments on commit 840299b

Please sign in to comment.