microsoft / Partner-Center-Java

Partner Center SDK for Java
https://docs.microsoft.com/java/partnercenter/
31 stars 12 forks source link

ClassCastException handling a 204 response with empty body #126

Open hummer-hpe opened 4 years ago

hummer-hpe commented 4 years ago

Steps to reproduce

What steps can reproduce the defect? Please share the setup, sample project, version of Java etc.

Occasionally, when querying for azure utilization by subscription by customer, the PartnerServiceClient.handleResponse method throws a ClassCastException. This occurs when a 204 response is handled with an empty body. Here's an example query:

ResourceCollection<AzureUtilizationRecord> records = api.getCustomers()
                     .byId(customerId)
                     .getSubscriptions()
                     .byId(subscription.getId())
                     .getUtilization()
                     .getAzure()
                     .query(startTime, endTime, AzureUtilizationGranularity.HOURLY, true, 1000);

Here's where PartnerServiceClient.handleResponse triggers the exception:

response = httpClient().newCall(request).execute();
if(response.isSuccessful())
{
    responseBody = response.body().string();
    if(StringHelper.isNullOrEmpty(responseBody))
    {
        value = (T)response;  // <--- THIS CAUSES ClassCastException
    }
    else
    {
        value = getJsonConverter().readValue(responseBody, responseType);
    }
    response.close();
    return value; 
}

Expected behavior

Share the expected output

I'd expect a null to be returned, or some form of not found exception to be thrown, but never a class cast exception, especially for a 204 response, which is defined as not having a response body.

Actual behavior

What is the behavior observed?

ClassCastException is thrown.

Diagnostic logs

Please share test platform diagnostics logs.
The logs may contain test assembly paths, kindly review and mask those before sharing.

Environment

Please share additional details about your environment. Version

Observed this behavior with both versions 1.13.2 and 1.15.3, and I see that the latest code on master (1.15.4) will do the same.

thebitmuncher commented 4 years ago

Appears to be the same as issue: https://github.com/microsoft/Partner-Center-Java-Samples/issues/10