Skip to content

Commit b21e82f

Browse files
authored
Merge pull request #2055 from espenvis/main
fix: mitigate illegal substitution exceptions
2 parents 867ee57 + d06dc71 commit b21e82f

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

components/abstractions/src/main/java/com/microsoft/kiota/RequestInformation.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,20 @@ private static Object getSanitizedValues(Object value) {
462462
return null;
463463
}
464464
if (value.getClass().isArray()) {
465-
if (((Object[]) value).length > 0 && ((Object[]) value)[0] instanceof ValuedEnum) {
465+
final Object[] values = (Object[]) value;
466+
467+
if (values.length > 0) {
468+
if (values[0].getClass().isArray()) {
469+
throw new IllegalArgumentException("multidimensional arrays are not supported");
470+
}
471+
466472
final ArrayList<String> result = new ArrayList<>();
467-
for (final Object item : (Object[]) value) {
468-
result.add(((ValuedEnum) item).getValue());
473+
for (final Object item : values) {
474+
result.add(getSanitizedValues(item).toString());
469475
}
470476
return result;
471477
}
472-
return Arrays.asList((Object[]) value);
478+
return Arrays.asList(values);
473479
} else if (value instanceof ValuedEnum) {
474480
return ((ValuedEnum) value).getValue();
475481
} else if (value instanceof UUID) {

components/abstractions/src/test/java/com/microsoft/kiota/RequestInformationTest.java

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.HashMap;
2323
import java.util.Map;
24+
import java.util.UUID;
2425

2526
class RequestInformationTest {
2627
@Test
@@ -50,6 +51,27 @@ void BuildsUrlOnProvidedBaseUrl() {
5051
assertEquals("http://localhost/users", result.toString());
5152
}
5253

54+
@Test
55+
void ThrowsIllegalArgumentWhenMultidimensionalQueryParameterIsSet() {
56+
// Arrange as the request builders would
57+
final RequestInformation requestInfo = new RequestInformation();
58+
requestInfo.httpMethod = HttpMethod.GET;
59+
requestInfo.urlTemplate = "{+baseurl}/users{?datasetIds}";
60+
final GetQueryParameters queryParameters = new GetQueryParameters();
61+
queryParameters.parallelDatasetIds =
62+
new UUID[][] {
63+
{UUID.fromString("f0f351e7-8e5f-4d0e-8f2a-7b5e4b6f4f3e")},
64+
{UUID.fromString("a2f351e7-8e5f-4d0e-8f2a-7b5e4b6f4f3e")}
65+
};
66+
67+
// Assert
68+
var exception =
69+
assertThrows(
70+
IllegalArgumentException.class,
71+
() -> requestInfo.addQueryParameters(queryParameters));
72+
assertTrue(exception.getMessage().contains("multidimensional arrays are not supported"));
73+
}
74+
5375
@Test
5476
void SetsPathParametersOfDateTimeOffsetType() {
5577
// Arrange as the request builders would
@@ -131,6 +153,30 @@ void SetsPathParametersOfPeriodAndDurationType() {
131153
assertTrue(uriResult.toString().contains("seatingDuration='PT30M'"));
132154
}
133155

156+
@Test
157+
void SetsQueryParametersOfPeriodAndDurationTypedArray()
158+
throws IllegalStateException, URISyntaxException {
159+
// Arrange as the request builders would
160+
final RequestInformation requestInfo = new RequestInformation();
161+
requestInfo.httpMethod = HttpMethod.GET;
162+
requestInfo.urlTemplate = "http://localhost/{?periods}";
163+
164+
final GetQueryParameters queryParameters = new GetQueryParameters();
165+
queryParameters.messageAges =
166+
new PeriodAndDuration[] {
167+
PeriodAndDuration.parse("PT30M"),
168+
PeriodAndDuration.parse("PT20M"),
169+
PeriodAndDuration.parse("PT1H20M")
170+
};
171+
172+
// Act
173+
requestInfo.addQueryParameters(queryParameters);
174+
175+
// Assert
176+
final URI uri = requestInfo.getUri();
177+
assertEquals("http://localhost/?periods=PT30M,PT20M,PT1H20M", uri.toString());
178+
}
179+
134180
@Test
135181
void ExpandQueryParametersAfterPathParams() {
136182
// Arrange as the request builders would
@@ -207,6 +253,24 @@ void SetsPathParametersOfBooleanType() {
207253
assertTrue(uriResult.toString().contains("count=true"));
208254
}
209255

256+
@Test
257+
void SetsQueryParametersOfBooleanTypedArray() throws IllegalStateException, URISyntaxException {
258+
// Arrange as the request builders would
259+
final RequestInformation requestInfo = new RequestInformation();
260+
requestInfo.httpMethod = HttpMethod.GET;
261+
requestInfo.urlTemplate = "http://localhost/{?expandChildren}";
262+
263+
final GetQueryParameters queryParameters = new GetQueryParameters();
264+
queryParameters.expandChildren = new Boolean[] {true, false, true, true};
265+
266+
// Act
267+
requestInfo.addQueryParameters(queryParameters);
268+
269+
// Assert
270+
final URI uri = requestInfo.getUri();
271+
assertEquals("http://localhost/?expandChildren=true,false,true,true", uri.toString());
272+
}
273+
210274
@Test
211275
void SetsPathParametersOfUUIDType() {
212276
// Arrange as the request builders would
@@ -239,6 +303,30 @@ void SetsQueryParametersOfUUIDType() {
239303
assertTrue(uriResult.toString().contains("?id=f0f351e7-8e5f-4d0e-8f2a-7b5e4b6f4f3e"));
240304
}
241305

306+
@Test
307+
void SetsQueryParametersOfUUIDTypedArray() throws IllegalStateException, URISyntaxException {
308+
// Arrange as the request builders would
309+
final RequestInformation requestInfo = new RequestInformation();
310+
requestInfo.httpMethod = HttpMethod.GET;
311+
requestInfo.urlTemplate = "http://localhost/{?datasetIds}";
312+
313+
final GetQueryParameters queryParameters = new GetQueryParameters();
314+
queryParameters.datasetIds =
315+
new UUID[] {
316+
UUID.fromString("f0f351e7-8e5f-4d0e-8f2a-7b5e4b6f4f3e"),
317+
UUID.fromString("a2f351e7-8e5f-4d0e-8f2a-7b5e4b6f4f3e")
318+
};
319+
320+
// Act
321+
requestInfo.addQueryParameters(queryParameters);
322+
323+
// Assert
324+
final URI uri = requestInfo.getUri();
325+
assertEquals(
326+
"http://localhost/?datasetIds=f0f351e7-8e5f-4d0e-8f2a-7b5e4b6f4f3e,a2f351e7-8e5f-4d0e-8f2a-7b5e4b6f4f3e",
327+
uri.toString());
328+
}
329+
242330
@Test
243331
void SetsParsableContent() {
244332
// Arrange as the request builders would
@@ -415,12 +503,31 @@ class GetQueryParameters implements QueryParameters {
415503

416504
@jakarta.annotation.Nullable public TestEnum[] datasets;
417505

506+
@jakarta.annotation.Nullable public UUID[] datasetIds;
507+
508+
/**
509+
* Search by dataset ids in parallel (or something like that)
510+
*/
511+
@jakarta.annotation.Nullable public UUID[][] parallelDatasetIds;
512+
513+
/** Per-dataset boolean indicating whether to resolve its child datasets */
514+
@jakarta.annotation.Nullable public Boolean[] expandChildren;
515+
516+
/**
517+
* Minimum message ages as duration, per dataset
518+
*/
519+
@jakarta.annotation.Nullable public PeriodAndDuration[] messageAges;
520+
418521
@jakarta.annotation.Nonnull public Map<String, Object> toQueryParameters() {
419-
final Map<String, Object> allQueryParams = new HashMap();
522+
final Map<String, Object> allQueryParams = new HashMap<>();
420523
allQueryParams.put("%24select", select);
421524
allQueryParams.put("%24search", search);
422525
allQueryParams.put("dataset", dataset);
423526
allQueryParams.put("datasets", datasets);
527+
allQueryParams.put("datasetIds", datasetIds);
528+
allQueryParams.put("parallelDatasetIds", parallelDatasetIds);
529+
allQueryParams.put("expandChildren", expandChildren);
530+
allQueryParams.put("periods", messageAges);
424531
return allQueryParams;
425532
}
426533
}

0 commit comments

Comments
 (0)