Support map-style query parameters (type: object + additionalProperties)#2144
Support map-style query parameters (type: object + additionalProperties)#2144bkoelman wants to merge 2 commits into
Conversation
…+ additionalProperties)
Add a Map branch to getSanitizedValues so that plain-object dictionary
query parameters are pre-processed before they reach StdUriTemplate.
Without this change, a Map<String, String> value passed through
addQueryParameters or addQueryParameter falls through to the else-return
branch unchanged. When the map contains a null value the subsequent
call to StdUriTemplate.convertNativeTypes(null) throws a
NullPointerException (null.getClass() is called in the fallback throw).
The fix mirrors the kiota-dotnet SanitizeDictionary approach exactly:
null-valued entries are dropped (RFC 6570 para 2.3 treats undefined
variables as absent), and remaining values are normalised to String via
the existing getSanitizedValues pipeline. The sanitized HashMap<String,
String> is returned so StdUriTemplate.isMap/addMapValue can expand it as
individual key=value pairs for RFC 6570 {?param*} templates.
The @SuppressWarnings("unchecked") annotation suppresses the unavoidable
raw-type cast of Map<Object,Object> -- Java generics are erased at
runtime so the cast to the generic Map type is unverifiable by the
compiler, but safe because the entries are accessed only as Object.
Co-authored-by: Cursor <cursoragent@cursor.com>
| // the variable expansion results in no output". Use "" to send ?key= (empty value). | ||
| @SuppressWarnings("unchecked") | ||
| final Map<Object, Object> rawMap = (Map<Object, Object>) value; | ||
| final HashMap<String, String> sanitized = new HashMap<>(rawMap.size()); |
There was a problem hiding this comment.
same as dotnet, can we use map string object instead to avoid the call to tostring below?
|
CC @andreaTP |
There was a problem hiding this comment.
Pull request overview
Adds support for “map-style” (object + additionalProperties) query parameters by sanitizing Map values before URI template expansion, preventing NullPointerException when map entries contain null values and enabling RFC 6570 {?param*} exploded expansion.
Changes:
- Add
Maphandling toRequestInformation.getSanitizedValuesthat drops null-valued entries and normalizes remaining values. - Add unit tests covering exploded map query parameter expansion, null-handling, and interaction with scalar query parameters.
- Extend the test query-parameter model (
GetQueryParameters) with aquerymap field and include it intoQueryParameters().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| components/abstractions/src/main/java/com/microsoft/kiota/RequestInformation.java | Adds Map sanitization so {?query*} can expand dictionary entries safely (dropping null values). |
| components/abstractions/src/test/java/com/microsoft/kiota/RequestInformationTest.java | Adds tests to validate exploded map query parameter expansion and null-handling behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for (final Map.Entry<Object, Object> entry : rawMap.entrySet()) { | ||
| if (entry.getValue() != null) { | ||
| sanitized.put( | ||
| entry.getKey().toString(), | ||
| getSanitizedValues(entry.getValue()).toString()); | ||
| } | ||
| } |
|
seems like a legit change to me 👍 |
…rameters Address PR feedback by using HashMap<String, Object> instead of HashMap<String, String> when sanitizing map query parameters in RequestInformation. This avoids unnecessary .toString() calls on sanitized values and preserves native object types for StdUriTemplate expansion. Also adds null key checks and tests for varied map value types. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Pushed fixes |
| * Expanded via RFC 6570 {?query*} into individual key=value pairs. | ||
| */ | ||
| @jakarta.annotation.Nullable public Map<String, String> query; | ||
| @jakarta.annotation.Nullable public Map<String, Object> query; |
There was a problem hiding this comment.
can we also add tests to validate that map string, string | int | float, etc... (primitive types essentially) also normalize as expected please?
(as I clarified in my similar comment in dotnet, I would expect the "correct type" to be generated, but the internal methods to still use object)
Contributes to microsoft/kiota#3800.
Add a Map branch to getSanitizedValues so that plain-object dictionary query parameters are pre-processed before they reach StdUriTemplate.
Without this change, a Map<String, String> value passed through addQueryParameters or addQueryParameter falls through to the else-return branch unchanged. When the map contains a null value the subsequent call to StdUriTemplate.convertNativeTypes(null) throws a NullPointerException (null.getClass() is called in the fallback throw).
The fix mirrors the kiota-dotnet SanitizeDictionary approach exactly: null-valued entries are dropped (RFC 6570 para 2.3 treats undefined variables as absent), and remaining values are normalised to String via the existing getSanitizedValues pipeline. The sanitized HashMap<String, String> is returned so StdUriTemplate.isMap/addMapValue can expand it as individual key=value pairs for RFC 6570 {?param*} templates.
The @SuppressWarnings("unchecked") annotation suppresses the unavoidable raw-type cast of Map<Object,Object> -- Java generics are erased at runtime so the cast to the generic Map type is unverifiable by the compiler, but safe because the entries are accessed only as Object.