Skip to content

Commit 54860e6

Browse files
committed
Improve JsonHelper method
1 parent d939146 commit 54860e6

File tree

6 files changed

+9
-8
lines changed

6 files changed

+9
-8
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AadInstanceDiscoveryProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static Set<String> getAliases(String host) {
121121
static AadInstanceDiscoveryResponse parseInstanceDiscoveryMetadata(String instanceDiscoveryJson) {
122122

123123
try {
124-
return JsonHelper.convertJsonStringToJsonSerializableObject(instanceDiscoveryJson, AadInstanceDiscoveryResponse.class);
124+
return JsonHelper.convertJsonStringToJsonSerializableObject(instanceDiscoveryJson, AadInstanceDiscoveryResponse::fromJson);
125125
} catch (Exception ex) {
126126
throw new MsalClientException("Error parsing instance discovery response. Data must be " +
127127
"in valid JSON format. For more information, see https://aka.ms/msal4j-instance-discovery",
@@ -233,7 +233,7 @@ static AadInstanceDiscoveryResponse sendInstanceDiscoveryRequest(URL authorityUr
233233

234234
IHttpResponse httpResponse = executeRequest(instanceDiscoveryRequestUrl, msalRequest.headers().getReadonlyHeaderMap(), msalRequest, serviceBundle);
235235

236-
AadInstanceDiscoveryResponse response = JsonHelper.convertJsonStringToJsonSerializableObject(httpResponse.body(), AadInstanceDiscoveryResponse.class);
236+
AadInstanceDiscoveryResponse response = JsonHelper.convertJsonStringToJsonSerializableObject(httpResponse.body(), AadInstanceDiscoveryResponse::fromJson);
237237

238238
if (httpResponse.statusCode() != HttpHelper.HTTP_STATUS_200) {
239239
if (httpResponse.statusCode() == HttpHelper.HTTP_STATUS_400 && response.error().equals("invalid_instance")) {

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AbstractManagedIdentitySource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public ManagedIdentityResponse handleResponse(
101101

102102
protected ManagedIdentityResponse getSuccessfulResponse(IHttpResponse response) {
103103

104-
ManagedIdentityResponse managedIdentityResponse = JsonHelper.convertJsonStringToJsonSerializableObject(response.body(), ManagedIdentityResponse.class);
104+
ManagedIdentityResponse managedIdentityResponse = JsonHelper.convertJsonStringToJsonSerializableObject(response.body(), ManagedIdentityResponse::fromJson);
105105

106106
if (managedIdentityResponse == null || managedIdentityResponse.getAccessToken() == null
107107
|| managedIdentityResponse.getAccessToken().isEmpty() || managedIdentityResponse.getExpiresOn() == null

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/JsonHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.azure.json.JsonProviders;
77
import com.azure.json.JsonReader;
88
import com.azure.json.JsonSerializable;
9+
import com.azure.json.ReadValueCallback;
910
import com.fasterxml.jackson.annotation.JsonInclude;
1011
import com.fasterxml.jackson.core.JsonParser;
1112
import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -40,9 +41,9 @@ static <T> T convertJsonToObject(final String json, final Class<T> tClass) {
4041
}
4142

4243
//This method is used to convert a JSON string to an object which implements the JsonSerializable interface from com.azure.json
43-
static <T extends JsonSerializable<T>> T convertJsonStringToJsonSerializableObject(String jsonResponse, Class<T> tClass) {
44+
static <T extends JsonSerializable<T>> T convertJsonStringToJsonSerializableObject(String jsonResponse, ReadValueCallback<JsonReader, T> readFunction) {
4445
try (JsonReader jsonReader = JsonProviders.createReader(jsonResponse)) {
45-
return (T) tClass.getDeclaredMethod("fromJson", JsonReader.class).invoke(null, jsonReader);
46+
return readFunction.read(jsonReader);
4647
} catch (IOException e) {
4748
throw new MsalClientException(e.getMessage(), AuthenticationErrorCode.INVALID_JSON);
4849
} catch (Exception e) {

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OidcDiscoveryProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static OidcDiscoveryResponse performOidcDiscovery(OidcAuthority authority, Abstr
1212

1313
IHttpResponse httpResponse = ((HttpHelper)clientApplication.serviceBundle.getHttpHelper()).executeHttpRequest(httpRequest);
1414

15-
OidcDiscoveryResponse response = JsonHelper.convertJsonStringToJsonSerializableObject(httpResponse.body(), OidcDiscoveryResponse.class);
15+
OidcDiscoveryResponse response = JsonHelper.convertJsonStringToJsonSerializableObject(httpResponse.body(), OidcDiscoveryResponse::fromJson);
1616

1717
if (httpResponse.statusCode() != HttpHelper.HTTP_STATUS_200) {
1818
throw MsalServiceExceptionFactory.fromHttpResponse(httpResponse);

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/UserDiscoveryRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ static UserDiscoveryResponse execute(
3333
throw MsalServiceExceptionFactory.fromHttpResponse(response);
3434
}
3535

36-
return JsonHelper.convertJsonStringToJsonSerializableObject(response.body(), UserDiscoveryResponse.class);
36+
return JsonHelper.convertJsonStringToJsonSerializableObject(response.body(), UserDiscoveryResponse::fromJson);
3737
}
3838
}

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/AadInstanceDiscoveryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void aadInstanceDiscoveryTest_NotSetByDeveloper() throws Exception {
5454
new RequestContext(app, PublicApi.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE, parameters));
5555

5656
URL authority = new URL(app.authority());
57-
AadInstanceDiscoveryResponse expectedResponse= JsonHelper.convertJsonStringToJsonSerializableObject(instanceDiscoveryValidResponse, AadInstanceDiscoveryResponse.class);
57+
AadInstanceDiscoveryResponse expectedResponse= JsonHelper.convertJsonStringToJsonSerializableObject(instanceDiscoveryValidResponse, AadInstanceDiscoveryResponse::fromJson);
5858

5959
try (MockedStatic<AadInstanceDiscoveryProvider> mockedInstanceDiscoveryProvider = mockStatic(AadInstanceDiscoveryProvider.class, CALLS_REAL_METHODS)) {
6060

0 commit comments

Comments
 (0)