|
14 | 14 | import java.util.List; |
15 | 15 | import java.util.Map; |
16 | 16 | import java.util.Set; |
17 | | -import java.util.stream.Collectors; |
18 | 17 |
|
19 | 18 | /** |
20 | 19 | * Used throughout Prebid to create targeting keys as keys which can be used in an ad server like DFP. |
@@ -267,33 +266,36 @@ private static String sizeFrom(Integer width, Integer height) { |
267 | 266 | private Map<String, String> truncateKeys(Map<String, String> keyValues, |
268 | 267 | Map<String, List<ExtBidderError>> bidWarnings) { |
269 | 268 |
|
270 | | - if (truncateAttrChars > 0) { |
271 | | - final List<String> truncatedKeys = new ArrayList<>(); |
272 | | - final Map<String, String> keys = keyValues.entrySet().stream().collect(Collectors.toMap( |
273 | | - keyValue -> truncateKey(keyValue.getKey(), truncatedKeys), |
274 | | - Map.Entry::getValue, |
275 | | - (key1, key2) -> key1)); |
276 | | - |
277 | | - if (!truncatedKeys.isEmpty()) { |
278 | | - final String errorMessage = "The following keys have been truncated: %s" |
279 | | - .formatted(String.join(", ", truncatedKeys)); |
280 | | - bidWarnings.computeIfAbsent("targeting", ignored -> new ArrayList<>()) |
281 | | - .add(ExtBidderError.of(BidderError.Type.bad_input.getCode(), errorMessage)); |
| 269 | + if (truncateAttrChars <= 0) { |
| 270 | + return keyValues; |
| 271 | + } |
| 272 | + |
| 273 | + final Map<String, String> keys = new HashMap<>(); |
| 274 | + final List<String> truncatedKeys = new ArrayList<>(); |
| 275 | + for (Map.Entry<String, String> entry : keyValues.entrySet()) { |
| 276 | + final String key = entry.getKey(); |
| 277 | + final String truncatedKey = truncateKey(key); |
| 278 | + keys.putIfAbsent(truncatedKey, entry.getValue()); |
| 279 | + |
| 280 | + if (truncatedKey.length() != key.length()) { |
| 281 | + truncatedKeys.add(key); |
282 | 282 | } |
| 283 | + } |
283 | 284 |
|
284 | | - return keys; |
| 285 | + if (!truncatedKeys.isEmpty()) { |
| 286 | + final String errorMessage = "The following keys have been truncated: %s" |
| 287 | + .formatted(String.join(", ", truncatedKeys)); |
| 288 | + bidWarnings.computeIfAbsent("targeting", ignored -> new ArrayList<>()) |
| 289 | + .add(ExtBidderError.of(BidderError.Type.bad_input.getCode(), errorMessage)); |
285 | 290 | } |
286 | 291 |
|
287 | | - return keyValues; |
| 292 | + return keys; |
288 | 293 | } |
289 | 294 |
|
290 | | - private String truncateKey(String key, List<String> truncatedKeys) { |
291 | | - if (key.length() > truncateAttrChars) { |
292 | | - truncatedKeys.add(key); |
293 | | - return key.substring(0, truncateAttrChars); |
294 | | - } |
295 | | - |
296 | | - return key; |
| 295 | + private String truncateKey(String key) { |
| 296 | + return key.length() > truncateAttrChars |
| 297 | + ? key.substring(0, truncateAttrChars) |
| 298 | + : key; |
297 | 299 | } |
298 | 300 |
|
299 | 301 | /** |
|
0 commit comments