Skip to content

Commit

Permalink
replace default string representation of Iterable fields with pipe-de…
Browse files Browse the repository at this point in the history
…limited format

affects application/csv, application/kvp+json, and text/csv return types
fixes #375
  • Loading branch information
alexdunnjpl committed Sep 26, 2023
1 parent a372fbd commit 55cc717
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
import org.slf4j.Logger;
Expand Down Expand Up @@ -111,7 +115,7 @@ public int setResponse(SearchHits hits, Summary summary, List<String> fields) {
WyriwygProductKeyValuePair kvp = new WyriwygProductKeyValuePair();
try {
kvp.setKey(SearchUtil.openPropertyToJsonProperty(pair.getKey()));
kvp.setValue(String.valueOf(pair.getValue()));
kvp.setValue(getStringValueOf(pair.getValue()));
product.addKeyValuePairsItem(kvp);
} catch (UnsupportedSearchProperty e) {
log.warn("openSearch property " + pair.getKey() + " is not supported, ignored");
Expand All @@ -125,4 +129,21 @@ public int setResponse(SearchHits hits, Summary summary, List<String> fields) {
this.products = products;
return (int) (hits.getTotalHits().value);
}

private String getStringValueOf(Object o) {
String valueOf;
if (o instanceof Iterable) {
List<String> stringRepresentations = new ArrayList<>();
for (Object el : (Iterable<Object>) o ) {
stringRepresentations.add(String.valueOf(el));
}

String delimiter = "|";
valueOf = String.join(delimiter, stringRepresentations);
} else {
valueOf = String.valueOf(o);
}

return valueOf;
}
}

0 comments on commit 55cc717

Please sign in to comment.