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

Merged
merged 12 commits into from
Apr 29, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void deletePerson(@PathVariable("personId") Integer personId){

@GetMapping(path = "/persons")
public List<Person> getPersons() {
return persons;
return persons;
}

@PostMapping(path = "/sleep")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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 @@ -91,7 +92,9 @@ public void testInvokeWithObjects() throws Exception {

client.invokeMethod(daprRun.getAppName(), "persons/1", null, HttpExtension.DELETE).block();

persons = Arrays.asList(client.invokeMethod(daprRun.getAppName(), "persons", null, HttpExtension.GET, Person[].class).block());
Map<String, List<String>> queryParams = Map.of("uri", List.of("abc/pqr"));
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET, queryParams, Map.of());
persons = Arrays.asList(client.invokeMethod(daprRun.getAppName(), "persons", null, httpExtension, Person[].class).block());
assertEquals(9, persons.size());

Person person = new Person();
Expand Down
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