Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflection free query parameters expansion #991

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

## [0.12.0] - 2024-01-10

### Changed

- [breaking] Removed the reflective extraction of Query Parameters in favor of plain methods invocations

## [0.11.2] - 2023-12-14

### Changed
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.microsoft.kiota;

import java.util.Map;

/** This interface allows to extract Query Parameters to be expanded in the URI Template. */
public interface QueryParameters {

/**
* Extracts the query parameters into a map for the URI template parsing.
* @return a Map of String to Object
*/
@jakarta.annotation.Nonnull Map<String, Object> toQueryParameters();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -73,7 +72,7 @@ public <T extends BaseRequestConfiguration> void configure(
public <T extends BaseRequestConfiguration> void configure(
@Nullable final java.util.function.Consumer<T> requestConfiguration,
@Nonnull final java.util.function.Supplier<T> configurationFactory,
@Nullable final java.util.function.Function<T, Object> queryParametersGetter) {
@Nullable final java.util.function.Function<T, QueryParameters> queryParametersGetter) {
Objects.requireNonNull(configurationFactory);
if (requestConfiguration == null) {
return;
Expand Down Expand Up @@ -152,29 +151,19 @@ public void setUri(@Nonnull final URI uri) {
* Adds query parameters to the request based on the object passed in and its fields.
* @param parameters The object to add the query parameters from.
*/
public void addQueryParameters(@Nullable final Object parameters) {
if (parameters == null) return;
final Field[] fields = parameters.getClass().getFields();
for (final Field field : fields) {
try {
Object value = field.get(parameters);
String name = field.getName();
if (field.isAnnotationPresent(QueryParameter.class)) {
final String annotationName = field.getAnnotation(QueryParameter.class).name();
if (annotationName != null && !annotationName.isEmpty()) {
name = annotationName;
}
}
public void addQueryParameters(@Nullable final QueryParameters parameters) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argh, forgot a line here:

if (parameters == null) return;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arg, I missed it too... Please submit a patch (with patch bump for the version) as I just finished releasing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noticed, and it's ready 😄

Map<String, Object> params = parameters.toQueryParameters();
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (entry.getKey() != null && entry.getValue() != null) {
Object value = entry.getValue();
if (value != null) {
value = replaceEnumValue(value);
if (value.getClass().isArray()) {
queryParameters.put(name, Arrays.asList((Object[]) value));
} else if (!value.toString().isEmpty()) {
queryParameters.put(name, value);
queryParameters.put(entry.getKey(), Arrays.asList((Object[]) value));
} else {
queryParameters.put(entry.getKey(), value);
}
}
} catch (IllegalAccessException ex) {
// TODO log
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

class RequestInformationTest {
@Test
Expand Down Expand Up @@ -92,7 +94,7 @@ void ExpandQueryParametersAfterPathParams() {
}

@Test
void DoesNotSetQueryParametersParametersIfEmptyString() {
void SetQueryParametersParametersWhenEmptyString() {

// Arrange as the request builders would
final RequestInformation requestInfo = new RequestInformation();
Expand All @@ -108,7 +110,7 @@ void DoesNotSetQueryParametersParametersIfEmptyString() {
// Assert
assertEquals("", queryParameters.search);
var uriResult = assertDoesNotThrow(() -> requestInfo.getUri());
assertFalse(uriResult.toString().contains("search"));
assertTrue(uriResult.toString().contains("search"));
}

@Test
Expand Down Expand Up @@ -310,18 +312,23 @@ void ReplacesEnumValuesPathParameters() throws IllegalStateException, URISyntaxE
}

/** The messages in a mailbox or folder. Read-only. Nullable. */
class GetQueryParameters {
class GetQueryParameters implements QueryParameters {
/** Select properties to be returned */
@QueryParameter(name = "%24select")
@jakarta.annotation.Nullable public String[] select;

/** Search items by search phrases */
@QueryParameter(name = "%24search")
@jakarta.annotation.Nullable public String search;

@QueryParameter(name = "dataset")
@jakarta.annotation.Nullable public TestEnum dataset;

@QueryParameter(name = "datasets")
@jakarta.annotation.Nullable public TestEnum[] datasets;

@jakarta.annotation.Nonnull public Map<String, Object> toQueryParameters() {
final Map<String, Object> allQueryParams = new HashMap();
allQueryParams.put("%24select", select);
allQueryParams.put("%24search", search);
allQueryParams.put("dataset", dataset);
allQueryParams.put("datasets", datasets);
return allQueryParams;
}
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ org.gradle.caching=true

mavenGroupId = com.microsoft.kiota
mavenMajorVersion = 0
mavenMinorVersion = 11
mavenPatchVersion = 2
mavenMinorVersion = 12
mavenPatchVersion = 0
mavenArtifactSuffix =

#These values are used to run functional tests
Expand Down
Loading