Skip to content

Commit 35cbd1e

Browse files
committed
- fixes a bug where collections of primitives would not be deserialized properly
1 parent 01e61d8 commit 35cbd1e

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.gson.JsonElement;
3434
import com.google.gson.JsonObject;
3535
import com.google.gson.JsonParseException;
36+
import com.google.gson.JsonPrimitive;
3637
import com.microsoft.graph.http.BaseCollectionResponse;
3738
import com.microsoft.graph.logger.ILogger;
3839

@@ -96,6 +97,14 @@ public static <T1> BaseCollectionResponse<T1> deserialize(@Nonnull final JsonEle
9697
final T1 targetObject = (T1)serializer.deserializeObject(sourceObject, entityClass);
9798
((IJsonBackedObject)targetObject).setRawObject(serializer, sourceObject);
9899
list.add(targetObject);
100+
} else if (sourceElement.isJsonPrimitive()) {
101+
final JsonPrimitive primitiveValue = sourceElement.getAsJsonPrimitive();
102+
if(primitiveValue.isString())
103+
list.add((T1)primitiveValue.getAsString());
104+
else if(primitiveValue.isBoolean())
105+
list.add((T1) Boolean.valueOf(primitiveValue.getAsBoolean()));
106+
else if(primitiveValue.isNumber())
107+
list.add((T1) Long.valueOf(primitiveValue.getAsLong()));
99108
}
100109
}
101110
final BaseCollectionResponse<T1> response = (BaseCollectionResponse<T1>)responseClass.getConstructor().newInstance();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.microsoft.graph.serializer;
2+
3+
import com.microsoft.graph.http.BaseCollectionResponse;
4+
5+
public class CollectionResponseOfPrimitives extends BaseCollectionResponse<String> {
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.microsoft.graph.serializer;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.mockito.Mockito.mock;
6+
7+
import com.microsoft.graph.logger.ILogger;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
public class CollectionResponseOfPrimitivesTests {
12+
@Test
13+
void DeserializesCollectionOfStrings() {
14+
final var serializer = new DefaultSerializer(mock(ILogger.class));
15+
final var serializedValue = "{\"@odata.context\": \"https://graph.microsoft.com/v1.0/$metadata#Collection(Edm.String)\",\"value\": [\"b72e90c8-3d3a-457e-8ca0-0fdde204d320\"]}";
16+
final var result = serializer.deserializeObject(serializedValue, CollectionResponseOfPrimitives.class);
17+
assertNotNull(result);
18+
assertNotNull(result.value);
19+
assertNotNull(result.additionalDataManager());
20+
assertEquals("https://graph.microsoft.com/v1.0/$metadata#Collection(Edm.String)", result.additionalDataManager().get("@odata.context").getAsString());
21+
assertEquals(1, result.value.size());
22+
}
23+
}

0 commit comments

Comments
 (0)