Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions docs/changelog/126562.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 126562
summary: Add a custom `toString` to `DynamicMap`
area: Infra/Scripting
type: bug
issues:
- 70262
13 changes: 12 additions & 1 deletion server/src/main/java/org/elasticsearch/script/DynamicMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ public Set<Entry<String, Object>> entrySet() {

@Override
public String toString() {
return delegate.toString();
StringBuilder sb = new StringBuilder("DynamicMap [");
int count = 0;
for (Map.Entry<String, Object> entry : entrySet()) {
sb.append(entry.getKey());
sb.append(": ");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should the same format as other Map's (eg HashMap, Map.of, etc) in Java use, which I believe is with curly braces and = between key/values. Also I wonder if we need the DynamicMap prefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed to match AbstractMap

sb.append(entry.getValue());
if (++count < size()) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class ScriptTests extends ESTestCase {
Expand Down Expand Up @@ -192,7 +194,6 @@ public void testDynamicMapToString() {
map.put("long", 1L);
map.put("string", "value");
DynamicMap dm = new DynamicMap(map, Collections.emptyMap());
assertTrue(dm.toString().contains("string=value"));
assertTrue(dm.toString().contains("long=1"));
assertThat(dm.toString(), both(containsString("long: 1")).and(containsString("string: value")));
}
}
Loading