Skip to content

fix: CustomClient return type validation #587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

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

# [9.3.0] - 2025-05-07
- Added support for native failover in Messages API

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Add the following to your `build.gradle` or `build.gradle.kts` file:

```groovy
dependencies {
implementation("com.vonage:server-sdk:9.3.0")
implementation("com.vonage:server-sdk:9.3.1")
}
```

Expand All @@ -85,7 +85,7 @@ Add the following to the `<dependencies>` section of your `pom.xml` file:
<dependency>
<groupId>com.vonage</groupId>
<artifactId>server-sdk</artifactId>
<version>9.3.0</version>
<version>9.3.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.vonage</groupId>
<artifactId>server-sdk</artifactId>
<version>9.3.0</version>
<version>9.3.1</version>

<name>Vonage Java Server SDK</name>
<description>Java client for Vonage APIs</description>
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/vonage/client/CustomClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,28 @@ public <T, R> R makeRequest(HttpMethod requestMethod, String url, T requestBody,
.build().execute(requestBody);
}

/**
* Normalises the response type to {@linkplain Void} if unknown.
*
* @param responseType Response type parameter (not provided directly).
*
* @return The updated response type parameter (to be provided downstream as varargs).
* @param <R> The type parameter.
*/
private <R> R[] fixResponseType(R... responseType) {
return responseType == null || Object.class.equals(responseType.getClass().getComponentType()) ?
(R[]) new Void[0] : responseType;
Class<R> componentType = (Class<R>) responseType.getClass().getComponentType();
if (
String.class.equals(componentType) ||
byte[].class.equals(componentType) ||
Jsonable.class.isAssignableFrom(componentType) ||
Map.class.isAssignableFrom(componentType) ||
Collection.class.isAssignableFrom(componentType)
) {
return responseType;
}
else {
return (R[]) new Void[0];
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/vonage/client/HttpWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class HttpWrapper {
private static final String
CLIENT_NAME = "vonage-java-sdk",
CLIENT_VERSION = "9.3.0",
CLIENT_VERSION = "9.3.1",
JAVA_VERSION = System.getProperty("java.version"),
USER_AGENT = String.format("%s/%s java/%s", CLIENT_NAME, CLIENT_VERSION, JAVA_VERSION);

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/vonage/client/VonageClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ public SmsClient getSmsClient() {
* Returns the Verify v1 API client.
*
* @return The {@linkplain VerifyClient}.
*
* @deprecated Please migrate to using {@linkplain #getVerify2Client()} instead.
*/
@Deprecated
public VerifyClient getVerifyClient() {
return verify;
}
Expand Down Expand Up @@ -259,7 +262,10 @@ public ConversationsClient getConversationsClient() {
*
* @return The {@linkplain SimSwapClient}.
* @since 8.8.0
*
* @deprecated This API will be removed in the next major release.
*/
@Deprecated
public SimSwapClient getSimSwapClient() {
return simSwap;
}
Expand All @@ -269,7 +275,10 @@ public SimSwapClient getSimSwapClient() {
*
* @return The {@linkplain NumberVerificationClient}.
* @since 8.9.0
*
* @deprecated This API will be removed in the next major release.
*/
@Deprecated
public NumberVerificationClient getNumberVerificationClient() {
return numberVerification;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
/**
* A client for communicating with the Vonage Number Verification API. The standard way to obtain an instance
* of this class is to use {@link VonageClient#getNumberVerificationClient()}.
*
* @deprecated This API will be removed in the next major release.
*/
@Deprecated
public class NumberVerificationClient extends NetworkApiClient {
final RestEndpoint<VerifyNumberRequest, VerifyNumberResponse> verifyNumber;
private final UUID appId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
*
* @since 8.9.0
*/
@Deprecated
package com.vonage.client.camara.numberverification;
1 change: 1 addition & 0 deletions src/main/java/com/vonage/client/camara/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
*
* @since 8.8.0
*/
@Deprecated
package com.vonage.client.camara;
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
/**
* A client for communicating with the Vonage SIM Swap API. The standard way to obtain an instance
* of this class is to use {@link VonageClient#getSimSwapClient()}.
*
* @deprecated This API will be removed in the next major release.
*/
@Deprecated
public class SimSwapClient extends NetworkApiClient {
final RestEndpoint<SimSwapRequest, CheckSimSwapResponse> check;
final RestEndpoint<SimSwapRequest, SimSwapDateResponse> retrieveDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
*
* @since 8.8.0
*/
@Deprecated
package com.vonage.client.camara.simswap;
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Implementation of the <a href=https://developer.vonage.com/en/conversation/overview>Conversation API</a>.
* Implementation of the <a href=https://developer.vonage.com/en/api/conversation>Conversation API</a>.
* See the <a href=https://developer.vonage.com/en/conversation/overview>Vonage developer portal</a>
* for an overview and documentation.
*
* @since 8.4.0
*/
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/vonage/client/verify/VerifyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
* <p>
* More information on method parameters can be found on the
* <a href="https://developer.vonage.com/verify/overview">Vonage developer portal</a>.
*
* @deprecated Please migrate to {@link com.vonage.client.verify2}.
*/
@Deprecated
public class VerifyClient {
final RestEndpoint<CheckRequest, CheckResponse> check;
final RestEndpoint<VerifyRequest, VerifyResponse> verify;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/vonage/client/verify/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
* See the <a href=https://developer.vonage.com/en/verify/verify-v1/overview>Vonage developer portal</a>
* for an overview and documentation.
*/
@Deprecated
package com.vonage.client.verify;
57 changes: 52 additions & 5 deletions src/test/java/com/vonage/client/CustomClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
package com.vonage.client;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.junit.jupiter.api.*;
import com.vonage.client.common.HttpMethod;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -79,7 +80,7 @@ public void testDelete() throws Exception {
stubResponse(204);
client.delete(URL);
assertNull(client.delete(URL, (Object) null));
assertNull(client.delete(URL, (Object[]) null));
assertThrows(NullPointerException.class, () -> client.delete(URL, (Object[]) null));
stubResponse(204, PAYLOAD_STR);
TestResponse responseBody = client.delete(URL);
assertEquals(TEST_JSONABLE, responseBody);
Expand Down Expand Up @@ -110,7 +111,7 @@ public void testPost() throws Exception {
stubResponse(201);
client.post(URL, TEST_JSONABLE);
assertNull(client.post(URL, TEST_JSONABLE, (Object) null));
assertNull(client.post(URL, TEST_JSONABLE, (Object[]) null));
assertThrows(NullPointerException.class, () -> client.post(URL, TEST_JSONABLE, (Object[]) null));
stubResponse(PAYLOAD_STR);
responseBody = client.post(URL, (Jsonable) null);
assertEquals(TEST_JSONABLE, responseBody);
Expand All @@ -129,7 +130,7 @@ public void testPut() throws Exception {
stubResponse(202);
client.put(URL, TEST_JSONABLE);
assertNull(client.put(URL, TEST_JSONABLE, (Object) null));
assertNull(client.put(URL, TEST_JSONABLE, (Object[]) null));
assertThrows(NullPointerException.class, () -> client.put(URL, TEST_JSONABLE, (Object[]) null));
stubResponse(409);
assertThrows(VonageApiResponseException.class, () -> client.put(URL, TEST_JSONABLE));
}
Expand All @@ -145,8 +146,54 @@ public void testPatch() throws Exception {
stubResponse(204);
client.patch(URL, TEST_JSONABLE);
assertNull(client.patch(URL, TEST_JSONABLE, (Object) null));
assertNull(client.patch(URL, TEST_JSONABLE, (Object[]) null));
assertThrows(NullPointerException.class, () -> client.patch(URL, TEST_JSONABLE, (Object[]) null));
stubResponse(406);
assertThrows(VonageApiResponseException.class, () -> client.patch(URL, TEST_JSONABLE));
}

@Test
public void testVarResponse() throws Exception {
stubResponse(PAYLOAD_STR);
var var = client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
assertNull(var);
}

@Test
public void testUnassignedResponse() throws Exception {
stubResponse(PAYLOAD_STR);
client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
}

@Test
public void testUnknownObjectResponse() throws Exception {
stubResponse(PAYLOAD_STR);
class MyClass {}
MyClass object = client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
assertNull(object);
}

@Test
public void testByteArrayResponse() throws Exception {
stubResponse(PAYLOAD_STR);
byte[] binary = client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
assertNotNull(binary);
assertEquals(PAYLOAD_STR, new String(binary));
}

@Test
public void testCollectionResponse() throws Exception {
stubResponse("[ " + PAYLOAD_STR + ", {}]");
List<Map<String, ?>> list = client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
assertNotNull(list);
assertEquals(2, list.size());
assertEquals(PAYLOAD_MAP, list.getFirst());
assertEquals(Map.of(), list.get(1));
}

@Test
public void testStringResponse() throws Exception {
stubResponse(PAYLOAD_STR);
String str = client.makeRequest(HttpMethod.GET, URL, TEST_JSONABLE);
assertEquals(PAYLOAD_STR, str);
}
}