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

Encoding path parameters #88

Merged
merged 1 commit into from
May 4, 2020
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
11 changes: 9 additions & 2 deletions src/main/java/com/recurly/v3/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.recurly.v3.http.HeaderInterceptor;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
Expand Down Expand Up @@ -237,8 +240,12 @@ protected String interpolatePath(String path, final HashMap<String, String> urlP

while (m.find()) {
final String key = m.group(1).replace("{", "").replace("}", "");
final String value = urlParams.get(key);
path = path.replace(m.group(1), value);
try {
final String value = URLEncoder.encode(urlParams.get(key), StandardCharsets.UTF_8.toString());
path = path.replace(m.group(1), value);
} catch (UnsupportedEncodingException ex) {
throw new RecurlyException(ex.getCause());
}
}

return path.replaceAll("\\{", "").replaceAll("\\}", "");
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/recurly/v3/BaseClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ public void testInterpolatePathWithParams() {
final MockClient client = new MockClient("apiKey");
final String path = "/accounts/{account_id}/notes/{account_note_id}";
final HashMap<String, String> urlParams = new HashMap<String, String>();
urlParams.put("account_id", "accountId");
urlParams.put("account_note_id", "noteId");
urlParams.put("account_id", "accountId/");
urlParams.put("account_note_id", "noteId,");
final String interpolatedPath = client.interpolatePath(path, urlParams);

assertEquals("/accounts/accountId/notes/noteId", interpolatedPath);
assertEquals("/accounts/accountId%2F/notes/noteId%2C", interpolatedPath);
}

protected static void setEnv(Map<String, String> newenv) throws Exception {
Expand Down