Skip to content

Fix URL building logic #1320

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
Expand Down Expand Up @@ -88,6 +89,11 @@ public void sleep(@RequestBody int seconds) throws InterruptedException {
Thread.sleep(seconds * 1000);
}

@GetMapping(path = "/query")
public Map<String, String> getQuery(@RequestParam("uri") String uri) {
return Map.of("uri", uri);
}

@GetMapping(path = "/health")
public void health() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.dapr.it.methodinvoke.http;

import com.fasterxml.jackson.databind.JsonNode;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.DaprHttp;
import io.dapr.client.domain.HttpExtension;
import io.dapr.exceptions.DaprException;
import io.dapr.it.BaseIT;
Expand Down Expand Up @@ -140,4 +141,24 @@ public void testInvokeException() throws Exception {
assertTrue(new String(exception.getPayload()).contains("Internal Server Error"));
}
}

@Test
public void testInvokeQueryParamEncoding() throws Exception {
try (DaprClient client = daprRun.newDaprClientBuilder().build()) {
client.waitForSidecar(10000).block();

String uri = "abc/pqr";
Map<String, List<String>> queryParams = Map.of("uri", List.of(uri));
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET, queryParams, Map.of());
JsonNode result = client.invokeMethod(
daprRun.getAppName(),
"/query",
null,
httpExtension,
JsonNode.class
).block();

assertEquals(uri, result.get("uri").asText());
}
}
}
21 changes: 18 additions & 3 deletions sdk/src/main/java/io/dapr/client/DaprHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand Down Expand Up @@ -324,10 +323,17 @@
private static URI createUri(URI uri, String[] pathSegments, Map<String, List<String>> urlParameters) {
String path = createPath(uri, pathSegments);
String query = createQuery(urlParameters);
StringBuilder result = new StringBuilder();

result.append(uri.getScheme()).append("://").append(uri.getAuthority()).append(path);

if (query != null) {
result.append("?").append(query);
}

try {
return new URI(uri.getScheme(), uri.getAuthority(), path, query, null);
} catch (URISyntaxException exception) {
return URI.create(result.toString());
} catch (IllegalArgumentException exception) {

Check warning on line 336 in sdk/src/main/java/io/dapr/client/DaprHttp.java

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/dapr/client/DaprHttp.java#L336

Added line #L336 was not covered by tests
throw new DaprException(exception);
}
}
Expand All @@ -346,6 +352,10 @@
}

for (String segment : pathSegments) {
if (segment == null || segment.isEmpty()) {
continue; // Skip empty segments

Check warning on line 356 in sdk/src/main/java/io/dapr/client/DaprHttp.java

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/dapr/client/DaprHttp.java#L356

Added line #L356 was not covered by tests
}

pathBuilder.append(encodePathSegment(segment)).append("/"); // Encode each segment
}

Expand All @@ -363,6 +373,11 @@

for (Map.Entry<String, List<String>> entry : urlParameters.entrySet()) {
String key = entry.getKey();

if (key == null || key.isEmpty()) {
continue; // Skip empty keys

Check warning on line 378 in sdk/src/main/java/io/dapr/client/DaprHttp.java

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/dapr/client/DaprHttp.java#L378

Added line #L378 was not covered by tests
}

List<String> values = entry.getValue();

for (String value : values) {
Expand Down
Loading