Skip to content

Commit 7ecec8e

Browse files
committed
Applied proposed changes.
1 parent c201034 commit 7ecec8e

File tree

4 files changed

+83
-75
lines changed

4 files changed

+83
-75
lines changed

src/main/java/com/microsoft/graph/serializer/CollectionResponseSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ public static <T1> BaseCollectionResponse<T1> deserialize(@Nonnull final JsonEle
8383
logger.logDebug("could not find class" + baseEntityClassCanonicalName);
8484
}
8585
try {
86+
final DerivedClassIdentifier derivedClassIdentifier = new DerivedClassIdentifier(logger);
8687
for(JsonElement sourceElement : sourceArray) {
8788
if(sourceElement.isJsonObject()) {
8889
final JsonObject sourceObject = sourceElement.getAsJsonObject();
89-
final DerivedClassIdentifier derivedClassIdentifier = new DerivedClassIdentifier(logger);
90-
Class<?> entityClass = derivedClassIdentifier.getDerivedClass(sourceObject, baseEntityClass);
90+
Class<?> entityClass = derivedClassIdentifier.identify(sourceObject, baseEntityClass);
9191
if(entityClass == null) {
9292
if(baseEntityClass == null) {
9393
logger.logError("Could not find target class for object " + sourceObject.toString(), null);

src/main/java/com/microsoft/graph/serializer/DefaultSerializer.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,23 @@
4545
* The default serializer implementation for the SDK
4646
*/
4747
public class DefaultSerializer implements ISerializer {
48-
private static final String graphResponseHeadersKey = "graphResponseHeaders";
48+
49+
private static final String GRAPH_RESPONSE_HEADERS_KEY = "graphResponseHeaders";
50+
51+
/**
52+
* The logger
53+
*/
54+
private final ILogger logger;
4955

5056
/**
5157
* The instance of the internal serializer
5258
*/
5359
private final Gson gson;
5460

55-
/**
56-
* The logger
57-
*/
58-
private final ILogger logger;
61+
/**
62+
* Derived class identifier
63+
*/
64+
private final DerivedClassIdentifier derivedClassIdentifier;
5965

6066
/**
6167
* Creates a DefaultSerializer
@@ -65,6 +71,7 @@ public class DefaultSerializer implements ISerializer {
6571
public DefaultSerializer(@Nonnull final ILogger logger) {
6672
this.logger = Objects.requireNonNull(logger, "parameter logger cannot be null");
6773
this.gson = GsonFactory.getGsonInstance(logger);
74+
this.derivedClassIdentifier = new DerivedClassIdentifier(logger);
6875
}
6976

7077
@Override
@@ -105,8 +112,7 @@ public <T> T deserializeObject(@Nonnull final JsonElement rawElement, @Nonnull f
105112
// If there is a derived class, try to get it and deserialize to it
106113
T jo = jsonObject;
107114
if (rawElement.isJsonObject()) {
108-
final DerivedClassIdentifier derivedClassIdentifier = new DerivedClassIdentifier(logger);
109-
final Class<?> derivedClass = derivedClassIdentifier.getDerivedClass(rawObject, clazz);
115+
final Class<?> derivedClass = derivedClassIdentifier.identify(rawObject, clazz);
110116
if (derivedClass != null)
111117
jo = (T) gson.fromJson(rawElement, derivedClass);
112118
}
@@ -121,7 +127,7 @@ public <T> T deserializeObject(@Nonnull final JsonElement rawElement, @Nonnull f
121127

122128
if (responseHeaders != null) {
123129
JsonElement convertedHeaders = gson.toJsonTree(responseHeaders);
124-
jsonBackedObject.additionalDataManager().put(graphResponseHeadersKey, convertedHeaders);
130+
jsonBackedObject.additionalDataManager().put(GRAPH_RESPONSE_HEADERS_KEY, convertedHeaders);
125131
}
126132
return jo;
127133
} else {
@@ -302,7 +308,7 @@ private void addAdditionalDataFromJsonObjectToJson (final Object item, final Jso
302308
*/
303309
private void addAdditionalDataFromManagerToJson(AdditionalDataManager additionalDataManager, JsonObject jsonNode) {
304310
for (Map.Entry<String, JsonElement> entry : additionalDataManager.entrySet()) {
305-
if(!entry.getKey().equals(graphResponseHeadersKey)) {
311+
if(!entry.getKey().equals(GRAPH_RESPONSE_HEADERS_KEY)) {
306312
jsonNode.add(entry.getKey(), entry.getValue());
307313
}
308314
}
Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,60 @@
1-
package com.microsoft.graph.serializer;
2-
3-
import com.google.common.base.CaseFormat;
4-
import com.google.gson.JsonObject;
5-
import com.microsoft.graph.logger.ILogger;
6-
7-
import java.util.Objects;
8-
import javax.annotation.Nonnull;
9-
import javax.annotation.Nullable;
10-
11-
public class DerivedClassIdentifier {
12-
13-
private final static String ODATA_TYPE_KEY = "@odata.type";
14-
private final ILogger logger;
15-
16-
public DerivedClassIdentifier(ILogger logger) {
17-
this.logger = logger;
18-
}
19-
20-
/**
21-
* Get the derived class for the given JSON object
22-
* This covers scenarios in which the service may return one of several derived types
23-
* of a base object, which it defines using the odata.type parameter
24-
*
25-
* @param jsonObject the raw JSON object of the response
26-
* @param parentClass the parent class the derived class should inherit from
27-
* @return the derived class if found, or null if not applicable
28-
*/
29-
@Nullable
30-
public Class<?> getDerivedClass(@Nonnull final JsonObject jsonObject, @Nullable final Class<?> parentClass) {
31-
Objects.requireNonNull(jsonObject, "parameter jsonObject cannot be null");
32-
//Identify the odata.type information if provided
33-
if (jsonObject.get(ODATA_TYPE_KEY) != null) {
34-
/** #microsoft.graph.user or #microsoft.graph.callrecords.callrecord */
35-
final String odataType = jsonObject.get(ODATA_TYPE_KEY).getAsString();
36-
final int lastDotIndex = odataType.lastIndexOf(".");
37-
final String derivedType = (odataType.substring(0, lastDotIndex) +
38-
".models." +
39-
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL,
40-
odataType.substring(lastDotIndex + 1)))
41-
.replace("#", "com.");
42-
try {
43-
Class<?> derivedClass = Class.forName(derivedType);
44-
//Check that the derived class inherits from the given parent class
45-
if (parentClass == null || parentClass.isAssignableFrom(derivedClass)) {
46-
return derivedClass;
47-
}
48-
return null;
49-
} catch (ClassNotFoundException e) {
50-
logger.logDebug("Unable to find a corresponding class for derived type " + derivedType + ". Falling back to parent class.");
51-
//If we cannot determine the derived type to cast to, return null
52-
//This may happen if the API and the SDK are out of sync
53-
return null;
54-
}
55-
}
56-
//If there is no defined OData type, return null
57-
return null;
58-
}
59-
}
1+
package com.microsoft.graph.serializer;
2+
3+
import com.google.common.base.CaseFormat;
4+
import com.google.gson.JsonObject;
5+
import com.microsoft.graph.logger.ILogger;
6+
7+
import java.util.Objects;
8+
import javax.annotation.Nonnull;
9+
import javax.annotation.Nullable;
10+
11+
public class DerivedClassIdentifier {
12+
13+
private final static String ODATA_TYPE_KEY = "@odata.type";
14+
15+
private final ILogger logger;
16+
17+
public DerivedClassIdentifier(@Nonnull ILogger logger) {
18+
this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");;
19+
}
20+
21+
/**
22+
* Get the derived class for the given JSON object
23+
* This covers scenarios in which the service may return one of several derived types
24+
* of a base object, which it defines using the odata.type parameter
25+
*
26+
* @param jsonObject the raw JSON object of the response
27+
* @param parentClass the parent class the derived class should inherit from
28+
* @return the derived class if found, or null if not applicable
29+
*/
30+
@Nullable
31+
public Class<?> identify(@Nonnull final JsonObject jsonObject, @Nullable final Class<?> parentClass) {
32+
Objects.requireNonNull(jsonObject, "parameter jsonObject cannot be null");
33+
//Identify the odata.type information if provided
34+
if (jsonObject.get(ODATA_TYPE_KEY) != null) {
35+
/** #microsoft.graph.user or #microsoft.graph.callrecords.callrecord */
36+
final String odataType = jsonObject.get(ODATA_TYPE_KEY).getAsString();
37+
final int lastDotIndex = odataType.lastIndexOf(".");
38+
final String derivedType = (odataType.substring(0, lastDotIndex) +
39+
".models." +
40+
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL,
41+
odataType.substring(lastDotIndex + 1)))
42+
.replace("#", "com.");
43+
try {
44+
Class<?> derivedClass = Class.forName(derivedType);
45+
//Check that the derived class inherits from the given parent class
46+
if (parentClass == null || parentClass.isAssignableFrom(derivedClass)) {
47+
return derivedClass;
48+
}
49+
return null;
50+
} catch (ClassNotFoundException e) {
51+
logger.logDebug("Unable to find a corresponding class for derived type " + derivedType + ". Falling back to parent class.");
52+
//If we cannot determine the derived type to cast to, return null
53+
//This may happen if the API and the SDK are out of sync
54+
return null;
55+
}
56+
}
57+
//If there is no defined OData type, return null
58+
return null;
59+
}
60+
}

src/main/java/com/microsoft/graph/serializer/FallbackTypeAdapterFactory.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,14 @@ private class ODataTypeParametrizedIJsonBackedObjectAdapter extends TypeAdapter<
123123
private final Gson gson;
124124
private final TypeAdapter<?> delegatedAdapter;
125125
private final TypeToken<?> type;
126+
private final DerivedClassIdentifier derivedClassIdentifier;
126127

127128
public ODataTypeParametrizedIJsonBackedObjectAdapter(@Nonnull Gson gson, @Nonnull TypeAdapter<?> delegatedAdapter, @Nonnull final TypeToken<?> type) {
128129
super();
129-
this.gson = gson;
130-
this.delegatedAdapter = delegatedAdapter;
131-
this.type = type;
130+
this.gson = Objects.requireNonNull(gson, "parameter gson cannot be null");;
131+
this.delegatedAdapter = Objects.requireNonNull(delegatedAdapter, "object delegated adapted cannot be null");
132+
this.type = Objects.requireNonNull(type, "object type cannot be null");
133+
this.derivedClassIdentifier = new DerivedClassIdentifier(logger);
132134
}
133135

134136
@Override
@@ -143,8 +145,7 @@ public IJsonBackedObject read(JsonReader in) {
143145
JsonElement jsonElement = Streams.parse(in);
144146

145147
if (jsonElement.isJsonObject()) {
146-
final DerivedClassIdentifier derivedClassIdentifier = new DerivedClassIdentifier(logger);
147-
final Class<?> derivedClass = derivedClassIdentifier.getDerivedClass(jsonElement.getAsJsonObject(), type.getRawType());
148+
final Class<?> derivedClass = derivedClassIdentifier.identify(jsonElement.getAsJsonObject(), type.getRawType());
148149

149150
if (derivedClass != null) {
150151
final TypeAdapter<?> subTypeAdapter = gson.getDelegateAdapter(FallbackTypeAdapterFactory.this, TypeToken.get(derivedClass));

0 commit comments

Comments
 (0)