Skip to content

Commit b907aaf

Browse files
authored
Merge pull request #1148 from microsoft/andrueastman/serializerBug
Fixes IllegalStateException when jsonarray is in additionalData
2 parents 46cd68f + 03d9d28 commit b907aaf

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
## [1.1.3] - 2024-04-02
15+
16+
### Changed
17+
18+
- Fixes a bug in the seriliazer that would `IllegalStateException` for json arrays in the additional data.
19+
1420
## [1.1.2] - 2024-03-26
1521

1622
### Changed

components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ public JsonParseNode(@Nonnull final JsonElement node) {
163163
}
164164
}
165165

166-
private <T> List<T> iterateOnArray(Function<JsonParseNode, T> fn) {
167-
JsonArray array = currentNode.getAsJsonArray();
166+
private <T> List<T> iterateOnArray(JsonElement jsonElement, Function<JsonParseNode, T> fn) {
167+
JsonArray array = jsonElement.getAsJsonArray();
168168
final Iterator<JsonElement> sourceIterator = array.iterator();
169169
final List<T> result = new ArrayList<>();
170170
while (sourceIterator.hasNext()) {
@@ -182,7 +182,8 @@ private <T> List<T> iterateOnArray(Function<JsonParseNode, T> fn) {
182182
if (currentNode.isJsonNull()) {
183183
return null;
184184
} else if (currentNode.isJsonArray()) {
185-
return iterateOnArray(itemNode -> getPrimitiveValue(targetClass, itemNode));
185+
return iterateOnArray(
186+
currentNode, itemNode -> getPrimitiveValue(targetClass, itemNode));
186187
} else throw new RuntimeException("invalid state expected to have an array node");
187188
}
188189

@@ -192,7 +193,7 @@ private <T> List<T> iterateOnArray(Function<JsonParseNode, T> fn) {
192193
if (currentNode.isJsonNull()) {
193194
return null;
194195
} else if (currentNode.isJsonArray()) {
195-
return iterateOnArray(itemNode -> itemNode.getObjectValue(factory));
196+
return iterateOnArray(currentNode, itemNode -> itemNode.getObjectValue(factory));
196197
} else return null;
197198
}
198199

@@ -202,7 +203,7 @@ private <T> List<T> iterateOnArray(Function<JsonParseNode, T> fn) {
202203
if (currentNode.isJsonNull()) {
203204
return null;
204205
} else if (currentNode.isJsonArray()) {
205-
return iterateOnArray(itemNode -> itemNode.getEnumValue(enumParser));
206+
return iterateOnArray(currentNode, itemNode -> itemNode.getEnumValue(enumParser));
206207
} else throw new RuntimeException("invalid state expected to have an array node");
207208
}
208209

@@ -242,7 +243,7 @@ else if (element.isJsonPrimitive()) {
242243
return new UntypedObject(propertiesMap);
243244

244245
} else if (element.isJsonArray()) {
245-
return new UntypedArray(iterateOnArray(JsonParseNode::getUntypedValue));
246+
return new UntypedArray(iterateOnArray(element, JsonParseNode::getUntypedValue));
246247
}
247248

248249
throw new RuntimeException(

components/serialization/json/src/test/java/com/microsoft/kiota/serialization/JsonParseNodeTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static org.junit.jupiter.api.Assertions.*;
44

55
import com.google.gson.JsonParser;
6+
import com.microsoft.kiota.serialization.mocks.MyEnum;
7+
import com.microsoft.kiota.serialization.mocks.TestEntity;
68
import com.microsoft.kiota.serialization.mocks.UntypedTestEntity;
79

810
import org.junit.jupiter.api.Test;
@@ -16,6 +18,11 @@
1618
class JsonParseNodeTests {
1719
private static final JsonParseNodeFactory _parseNodeFactory = new JsonParseNodeFactory();
1820
private static final String contentType = "application/json";
21+
22+
private static final String testJsonString =
23+
"{\"displayName\":\"My"
24+
+ " Group\",\"phones\":[\"+1234567890\"],\"myEnum\":\"VALUE1\",\"enumCollection\":[\"VALUE1\"],\"id\":\"11111111-1111-1111-1111-111111111111"
25+
+ "\",\"members@delta\":[{\"@odata.type\":\"#microsoft.graph.user\",\"id\":\"22222222-2222-2222-2222-222222222222\"}]}";
1926
private static final String testUntypedJson =
2027
"{\r\n"
2128
+ " \"@odata.context\":"
@@ -95,6 +102,20 @@ void testInvalidOffsetDateTimeStringThrowsException(final String dateTimeString)
95102
}
96103
}
97104

105+
@Test
106+
void getEntityWithArrayInAdditionalData() throws UnsupportedEncodingException {
107+
final var rawResponse = new ByteArrayInputStream(testJsonString.getBytes("UTF-8"));
108+
final var parseNode = _parseNodeFactory.getParseNode(contentType, rawResponse);
109+
// Act
110+
var entity = parseNode.getObjectValue(TestEntity::createFromDiscriminatorValue);
111+
assertEquals("11111111-1111-1111-1111-111111111111", entity.getId());
112+
assertEquals(1, entity.getPhones().size());
113+
assertEquals(MyEnum.MY_VALUE1, entity.getMyEnum());
114+
assertEquals(1, entity.getEnumCollection().size());
115+
final var arrayValue = (UntypedArray) entity.getAdditionalData().get("members@delta");
116+
assertEquals(1, arrayValue.getValue().spliterator().estimateSize());
117+
}
118+
98119
@Test
99120
void GetEntityWithUntypedNodesFromJson() throws UnsupportedEncodingException {
100121
final var rawResponse = new ByteArrayInputStream(testUntypedJson.getBytes("UTF-8"));

components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.time.LocalDate;
1010
import java.time.LocalTime;
1111
import java.time.OffsetDateTime;
12+
import java.util.ArrayList;
1213
import java.util.HashMap;
14+
import java.util.List;
1315
import java.util.Map;
1416
import java.util.Objects;
1517
import java.util.function.Consumer;
@@ -25,6 +27,16 @@ public void setId(String _id) {
2527
this._id = _id;
2628
}
2729

30+
private List<String> _phones;
31+
32+
public List<String> getPhones() {
33+
return _phones;
34+
}
35+
36+
public void setPhones(List<String> _phones) {
37+
this._phones = new ArrayList<String>(_phones);
38+
}
39+
2840
private String _officeLocation;
2941

3042
public String getOfficeLocation() {
@@ -85,6 +97,16 @@ public void setMyEnum(MyEnum value) {
8597
this._myEnum = value;
8698
}
8799

100+
private List<MyEnum> _enumCollection;
101+
102+
public List<MyEnum> getEnumCollection() {
103+
return _enumCollection;
104+
}
105+
106+
public void setEnumCollection(List<MyEnum> value) {
107+
this._enumCollection = new ArrayList<MyEnum>(value);
108+
}
109+
88110
private OffsetDateTime _createdDateTime;
89111

90112
public OffsetDateTime getCreatedDateTime() {
@@ -134,11 +156,21 @@ public Map<String, Consumer<ParseNode>> getFieldDeserializers() {
134156
(n) -> {
135157
setMyEnum(n.getEnumValue(MyEnum::forValue));
136158
});
159+
put(
160+
"enumCollection",
161+
(n) -> {
162+
setEnumCollection(n.getCollectionOfEnumValues(MyEnum::forValue));
163+
});
137164
put(
138165
"createdDateTime",
139166
(n) -> {
140167
setCreatedDateTime(n.getOffsetDateTimeValue());
141168
});
169+
put(
170+
"phones",
171+
(n) -> {
172+
setPhones(n.getCollectionOfPrimitiveValues(String.class));
173+
});
142174
}
143175
};
144176
}
@@ -153,7 +185,9 @@ public void serialize(SerializationWriter writer) {
153185
writer.writeLocalTimeValue("startWorkTime", getStartWorkTime());
154186
writer.writeLocalTimeValue("endWorkTime", getEndWorkTime());
155187
writer.writeEnumValue("myEnum", getMyEnum());
188+
writer.writeCollectionOfEnumValues("enumCollection", getEnumCollection());
156189
writer.writeOffsetDateTimeValue("createdDateTime", getCreatedDateTime());
190+
writer.writeCollectionOfPrimitiveValues("phones", getPhones());
157191
writer.writeAdditionalData(getAdditionalData());
158192
}
159193

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ org.gradle.caching=true
2626
mavenGroupId = com.microsoft.kiota
2727
mavenMajorVersion = 1
2828
mavenMinorVersion = 1
29-
mavenPatchVersion = 2
29+
mavenPatchVersion = 3
3030
mavenArtifactSuffix =
3131

3232
#These values are used to run functional tests

0 commit comments

Comments
 (0)