Skip to content

Commit 83262aa

Browse files
committed
fix: CustomClient.fixResponseType logic
1 parent 21e33b3 commit 83262aa

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5-
# [9.3.1] - 2025-0?-??
5+
# [9.3.1] - 2025-05-08
6+
- Fixed `CustomClient` return type validation (normalise to `Void` if unknown)
67
- Deprecated Verify v1, SIM Swap and Number Verification APIs
78

89
# [9.3.0] - 2025-05-07

src/main/java/com/vonage/client/CustomClient.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,28 @@ public <T, R> R makeRequest(HttpMethod requestMethod, String url, T requestBody,
9292
.build().execute(requestBody);
9393
}
9494

95+
/**
96+
* Normalises the response type to {@linkplain Void} if unknown.
97+
*
98+
* @param responseType Response type parameter (not provided directly).
99+
*
100+
* @return The updated response type parameter (to be provided downstream as varargs).
101+
* @param <R> The type parameter.
102+
*/
95103
private <R> R[] fixResponseType(R... responseType) {
96-
return responseType == null || Object.class.equals(responseType.getClass().getComponentType()) ?
97-
(R[]) new Void[0] : responseType;
104+
Class<R> componentType = (Class<R>) responseType.getClass().getComponentType();
105+
if (
106+
String.class.equals(componentType) ||
107+
byte[].class.equals(componentType) ||
108+
Jsonable.class.isAssignableFrom(componentType) ||
109+
Map.class.isAssignableFrom(componentType) ||
110+
Collection.class.isAssignableFrom(componentType)
111+
) {
112+
return responseType;
113+
}
114+
else {
115+
return (R[]) new Void[0];
116+
}
98117
}
99118

100119
/**

src/test/java/com/vonage/client/CustomClientTest.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
package com.vonage.client;
1717

1818
import com.fasterxml.jackson.annotation.JsonProperty;
19-
import org.junit.jupiter.api.*;
19+
import com.vonage.client.common.HttpMethod;
2020
import static org.junit.jupiter.api.Assertions.*;
21+
import org.junit.jupiter.api.*;
2122
import java.util.List;
2223
import java.util.Map;
2324

@@ -79,7 +80,7 @@ public void testDelete() throws Exception {
7980
stubResponse(204);
8081
client.delete(URL);
8182
assertNull(client.delete(URL, (Object) null));
82-
assertNull(client.delete(URL, (Object[]) null));
83+
assertThrows(NullPointerException.class, () -> client.delete(URL, (Object[]) null));
8384
stubResponse(204, PAYLOAD_STR);
8485
TestResponse responseBody = client.delete(URL);
8586
assertEquals(TEST_JSONABLE, responseBody);
@@ -110,7 +111,7 @@ public void testPost() throws Exception {
110111
stubResponse(201);
111112
client.post(URL, TEST_JSONABLE);
112113
assertNull(client.post(URL, TEST_JSONABLE, (Object) null));
113-
assertNull(client.post(URL, TEST_JSONABLE, (Object[]) null));
114+
assertThrows(NullPointerException.class, () -> client.post(URL, TEST_JSONABLE, (Object[]) null));
114115
stubResponse(PAYLOAD_STR);
115116
responseBody = client.post(URL, (Jsonable) null);
116117
assertEquals(TEST_JSONABLE, responseBody);
@@ -129,7 +130,7 @@ public void testPut() throws Exception {
129130
stubResponse(202);
130131
client.put(URL, TEST_JSONABLE);
131132
assertNull(client.put(URL, TEST_JSONABLE, (Object) null));
132-
assertNull(client.put(URL, TEST_JSONABLE, (Object[]) null));
133+
assertThrows(NullPointerException.class, () -> client.put(URL, TEST_JSONABLE, (Object[]) null));
133134
stubResponse(409);
134135
assertThrows(VonageApiResponseException.class, () -> client.put(URL, TEST_JSONABLE));
135136
}
@@ -145,8 +146,39 @@ public void testPatch() throws Exception {
145146
stubResponse(204);
146147
client.patch(URL, TEST_JSONABLE);
147148
assertNull(client.patch(URL, TEST_JSONABLE, (Object) null));
148-
assertNull(client.patch(URL, TEST_JSONABLE, (Object[]) null));
149+
assertThrows(NullPointerException.class, () -> client.patch(URL, TEST_JSONABLE, (Object[]) null));
149150
stubResponse(406);
150151
assertThrows(VonageApiResponseException.class, () -> client.patch(URL, TEST_JSONABLE));
151152
}
153+
154+
@Test
155+
public void testVarResponse() throws Exception {
156+
stubResponse(PAYLOAD_STR);
157+
var var = client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
158+
assertNull(var);
159+
}
160+
161+
@Test
162+
public void testCollectionResponse() throws Exception {
163+
stubResponse("[ " + PAYLOAD_STR + ", {}]");
164+
List<Map<String, ?>> list = client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
165+
assertNotNull(list);
166+
assertEquals(2, list.size());
167+
assertEquals(PAYLOAD_MAP, list.getFirst());
168+
assertEquals(Map.of(), list.get(1));
169+
}
170+
171+
@Test
172+
public void testUnknownObjectResponse() throws Exception {
173+
stubResponse(PAYLOAD_STR);
174+
class MyClass {}
175+
MyClass object = client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
176+
assertNull(object);
177+
}
178+
179+
@Test
180+
public void testUnassignedResponse() throws Exception {
181+
stubResponse(PAYLOAD_STR);
182+
client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
183+
}
152184
}

0 commit comments

Comments
 (0)