Skip to content

Commit 5d0dcdb

Browse files
author
Caitlin Bales (MSFT)
authored
Merge pull request #36 from davidmoten/derived-type-serializer-fix
Ensure derived type serialization sets rawObject
2 parents a5583bc + 03fe2f4 commit 5d0dcdb

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,26 @@ public <T> T deserializeObject(final String inputString, final Class<T> clazz) {
7070
return deserializeObject(inputString, clazz, null);
7171
}
7272

73+
@SuppressWarnings("unchecked")
7374
@Override
7475
public <T> T deserializeObject(final String inputString, final Class<T> clazz, Map<String, java.util.List<String>> responseHeaders) {
75-
T jsonObject = gson.fromJson(inputString, clazz);
76+
final T jsonObject = gson.fromJson(inputString, clazz);
7677

7778
// Populate the JSON-backed fields for any annotations that are not in the object model
7879
if (jsonObject instanceof IJsonBackedObject) {
7980
logger.logDebug("Deserializing type " + clazz.getSimpleName());
80-
final IJsonBackedObject jsonBackedObject = (IJsonBackedObject) jsonObject;
8181
final JsonObject rawObject = gson.fromJson(inputString, JsonObject.class);
82-
83-
// If there is a derived class, try to get it and deserialize to it
84-
Class derivedClass = this.getDerivedClass(rawObject, clazz);
85-
if (derivedClass != null) {
86-
jsonObject = (T) gson.fromJson(inputString, derivedClass);
87-
}
88-
82+
83+
// If there is a derived class, try to get it and deserialize to it
84+
Class<?> derivedClass = this.getDerivedClass(rawObject, clazz);
85+
final T jo;
86+
if (derivedClass != null) {
87+
jo = (T) gson.fromJson(inputString, derivedClass);
88+
} else {
89+
jo = jsonObject;
90+
}
91+
92+
final IJsonBackedObject jsonBackedObject = (IJsonBackedObject) jo;
8993
jsonBackedObject.setRawObject(this, rawObject);
9094

9195
if (responseHeaders != null) {
@@ -94,11 +98,11 @@ public <T> T deserializeObject(final String inputString, final Class<T> clazz, M
9498
}
9599

96100
jsonBackedObject.additionalDataManager().setAdditionalData(rawObject);
101+
return jo;
97102
} else {
98103
logger.logDebug("Deserializing a non-IJsonBackedObject type " + clazz.getSimpleName());
104+
return jsonObject;
99105
}
100-
101-
return jsonObject;
102106
}
103107

104108
/**

src/test/java/com/microsoft/graph/serializer/DefaultSeralizerTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.microsoft.graph.models.generated.RecurrenceRangeType;
1111
import com.microsoft.graph.models.generated.BaseRecurrenceRange;
1212
import com.google.gson.JsonElement;
13+
import com.google.gson.JsonObject;
1314
import com.microsoft.graph.http.MockConnection;
1415
import com.microsoft.graph.logger.DefaultLogger;
1516
import com.microsoft.graph.models.extensions.Attachment;
@@ -93,5 +94,8 @@ public void testDeserializeDerivedType() throws Exception {
9394

9495
FileAttachment fileAttachment = (FileAttachment) result;
9596
assertNotNull(fileAttachment.contentBytes);
97+
JsonObject o = fileAttachment.getRawObject();
98+
assertNotNull(o);
99+
assertEquals("#microsoft.graph.fileAttachment", o. get("@odata.type").getAsString());
96100
}
97101
}

0 commit comments

Comments
 (0)