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

Params accept array string #257

Merged
merged 5 commits into from
Mar 8, 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
8 changes: 7 additions & 1 deletion src/main/java/com/amadeus/Params.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -48,7 +49,12 @@ public static Params with(@NonNull String key, Object value) {
* @return the Param object, allowing for convenient chaining
*/
public Params and(@NonNull String key, Object value) {
put(key, String.valueOf(value));
if (value instanceof List) {
@SuppressWarnings("unchecked") List<String> values = (List<String>) value;
put(key, String.join(",", values));
} else {
put(key, String.valueOf(value));
}
return this;
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/amadeus/ParamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Arrays;
import org.junit.jupiter.api.Test;

public class ParamsTest {
Expand All @@ -22,6 +23,11 @@ public class ParamsTest {
assertEquals(params.toQueryString(), "bar=234&foo=123");
}

@Test public void testToQueryStringWithList() {
Params params = Params.with("foo", Arrays.asList("id1", "id2"));
assertEquals(params.toQueryString(), "foo=id1%2Cid2");
}

@Test public void testToQueryStringWithEncodingError() {
Params params = Params.with("foo", "123");
params.encoding = "foobar";
Expand Down
Loading