@@ -59,7 +59,7 @@ public DefaultSerializer(final ILogger logger) {
5959 * Deserialize an object from the input string.
6060 *
6161 * @param inputString The string that stores the representation of the item.
62- * @param clazz The . class of the item to be deserialized.
62+ * @param clazz The class of the item to be deserialized.
6363 * @param <T> The type of the item to be deserialized.
6464 * @return The deserialized item from the input string.
6565 */
@@ -74,7 +74,7 @@ public <T> T deserializeObject(final String inputString, final Class<T> clazz) {
7474 final JsonObject rawObject = gson .fromJson (inputString , JsonObject .class );
7575
7676 // If there is a derived class, try to get it and deserialize to it
77- Class derivedClass = this .getDerivedClass (rawObject );
77+ Class derivedClass = this .getDerivedClass (rawObject , clazz );
7878 if (derivedClass != null ) {
7979 jsonObject = (T ) gson .fromJson (inputString , derivedClass );
8080 }
@@ -130,9 +130,10 @@ private boolean fieldIsOdataTransient(Map.Entry<String, JsonElement> entry) {
130130 * This covers scenarios in which the service may return one of several derived types
131131 * of a base object, which it defines using the odata.type parameter
132132 * @param jsonObject The raw JSON object of the response
133+ * @param parentClass The parent class the derived class should inherit from
133134 * @return The derived class if found, or null if not applicable
134135 */
135- private Class getDerivedClass (JsonObject jsonObject ) {
136+ private Class getDerivedClass (JsonObject jsonObject , Class parentClass ) {
136137 //Identify the odata.type information if provided
137138 if (jsonObject .get ("@odata.type" ) != null ) {
138139 String odataType = jsonObject .get ("@odata.type" ).getAsString ();
@@ -141,7 +142,12 @@ private Class getDerivedClass(JsonObject jsonObject) {
141142 derivedType = "com.microsoft.graph.models.extensions." + derivedType ; //Add full package path
142143
143144 try {
144- return Class .forName (derivedType );
145+ Class derivedClass = Class .forName (derivedType );
146+ //Check that the derived class inherits from the given parent class
147+ if (parentClass .isAssignableFrom (derivedClass )) {
148+ return derivedClass ;
149+ }
150+ return null ;
145151 } catch (ClassNotFoundException e ) {
146152 logger .logDebug ("Unable to find a corresponding class for derived type " + derivedType + ". Falling back to parent class." );
147153 //If we cannot determine the derived type to cast to, return null
0 commit comments