Skip to content

Commit

Permalink
fix: fix search alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuzynow committed Apr 24, 2024
1 parent 0405c60 commit c8e3424
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.caritas.cob.userservice.api.adapters.web.controller;

import static com.google.common.collect.Lists.newArrayList;

import com.github.jknack.handlebars.internal.lang3.StringUtils;
import com.google.common.base.Splitter;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SearchQueryDecoder {

private static final String SEPARATOR = "+";

/* Allows the query string to contain plus sign, but not change it to space.
See https://bugs.openjdk.org/browse/JDK-8179507 */
public static String decode(String query) {
if (StringUtils.isBlank(query)) {
return StringUtils.EMPTY;
}
var parts = Splitter.on(SEPARATOR).split(query);
return newArrayList(parts).stream()
.map(SearchQueryDecoder::decodePart)
.collect(Collectors.joining(SEPARATOR)).trim();
}

private static String decodePart(String s) {
return URLDecoder.decode(s, StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ public ResponseEntity<List<ConsultantResponseDTO>> getConsultants(@RequestParam
@Override
public ResponseEntity<ConsultantSearchResultDTO> searchConsultants(
String query, Integer page, Integer perPage, String field, String order) {
var decodedInfix = URLDecoder.decode(query, StandardCharsets.UTF_8).trim();
var decodedInfix = SearchQueryDecoder.decode(query);
var isAscending = order.equalsIgnoreCase("asc");
var mappedField = consultantDtoMapper.mappedFieldOf(field);
var resultMap =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.caritas.cob.userservice.api.adapters.web.controller;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class SearchQueryDecoderTest {

@ParameterizedTest
@CsvSource({
"[email protected], [email protected]",
"*, *",
"[email protected], [email protected]",
"A Test, A Test",
",''",
"\"\",\"\"",
"stringWithoutPlusSign,stringWithoutPlusSign",
"firstname.lastname%2Bconsultant%40virtual-identity.com, [email protected]"
})
void decode_Should_Decode_String_Not_ChangingPlusIntoSpace(String input, String expectedOutput) {
assertThat(SearchQueryDecoder.decode(input).trim()).isEqualTo(expectedOutput);
}
}

0 comments on commit c8e3424

Please sign in to comment.