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

Multiple param patch #373

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class URIConfigurator {
private static final String URL_POSTFIX = ".json";
Expand Down Expand Up @@ -97,7 +98,7 @@ public URI createURI(String query, RequestParam... param) {
*/
private URI createURI(String query,
Collection<RequestParam> origParams) {
var distinctParams = distinct(origParams);
var distinctParams = dedup(origParams);
var nameValueParams = toNameValue(distinctParams);
try {
var builder = new URIBuilder(baseURL.toURI());
Expand All @@ -120,6 +121,25 @@ static Collection<RequestParam> distinct(Collection<RequestParam> origParams) {
.values();
}

/**
* Gives a list of parameters with any duplicates removed.
* <br />
* Parameters are considered to be duplicates if they have the same parameter name;
* however, parameters with names ending in "[]" will NOT be removed, as that indicates
* an array element which may be repeated. (In particular, free-form Redmine queries
* all have a parameter called "f[]", and so those must be preserved.)
* <br />
* This does NOT preserve the order of parameters.
*/
static Collection<RequestParam> dedup(Collection<RequestParam> origParams) {
var repeatableParams = origParams.stream()
.filter(param -> param != null && param.getName().endsWith("[]"));
var distinctNonRepeatableParams = distinct(origParams).stream()
.filter(param -> param != null && !param.getName().endsWith("[]"));
return Stream.concat(repeatableParams, distinctNonRepeatableParams)
.collect(Collectors.toList());
}

static Collection<NameValuePair> toNameValue(Collection<RequestParam> origParams) {
return origParams
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class URIConfiguratorTest {
private static final RequestParam param1 = new RequestParam("name1", "value1");
private static final RequestParam param2 = new RequestParam("name3", "value3");
private static final RequestParam param2WithDifferentValue = new RequestParam("name3", "anotherValue3");
private static final RequestParam arrayParam1 = new RequestParam("name4[]", "value4");
private static final RequestParam arrayParam1WithDifferentValue = new RequestParam("name4[]", "anotherValue4");
private static final RequestParam nullParam = null;

@Test
Expand Down Expand Up @@ -44,6 +46,24 @@ public void distinctWithDifferentNamesSameValuesShouldKeepAllItems() {
"two", "value"));
}

@Test
public void dedupRemovesDuplicates() {
List<RequestParam> params = Arrays.asList(param2, param2WithDifferentValue);
assertThat(URIConfigurator.dedup(params)).containsOnly(param2);
}

@Test
public void dedupDoesNotRemoveArrays() {
List<RequestParam> params = Arrays.asList(arrayParam1, arrayParam1WithDifferentValue);
assertThat(URIConfigurator.dedup(params)).containsAll(params);
}

@Test
public void dedupWithDifferentNamesSameValuesShouldKeepAllItems() {
List<RequestParam> params = Arrays.asList(param1, param2, arrayParam1);
assertThat(URIConfigurator.dedup(params)).containsAll(params);
}

@Test
public void toNameValueConvertsCollection() {
assertThat(URIConfigurator.toNameValue(Arrays.asList(param1, param2))).containsOnly(
Expand Down