Skip to content
Closed
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
40 changes: 37 additions & 3 deletions src/main/java/com/hubspot/jinjava/objects/collections/PyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.google.common.collect.ForwardingMap;
import com.hubspot.jinjava.objects.PyWrapper;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -62,9 +66,39 @@ public void putAll(Map<? extends String, ? extends Object> m) {
@Override
public int hashCode() {
int h = 0;
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() != map && entry.getValue() != this) {
h += entry.hashCode();
List<Object> valueList = new ArrayList<>(map.entrySet());
ListIterator<Object> valueIterator = valueList.listIterator();
Set<Integer> visited = new HashSet<>();
while (valueIterator.hasNext()) {
Object next = valueIterator.next();
int code = System.identityHashCode(next);
if (visited.contains(code)) {
continue;
} else {
visited.add(code);
}
if (next instanceof Entry) {
Entry nextEntry = (Entry) next;
if (nextEntry.getKey() != null) {
h += nextEntry.getKey().hashCode();
}
valueIterator.add(nextEntry.getValue());
valueIterator.previous();
} else if (next instanceof Iterable) {
for (Object o : (Iterable) next) {
valueIterator.add(o);
valueIterator.previous();
}
} else if (next instanceof Map) {
((Map) next).entrySet()
.forEach(
e -> {
valueIterator.add(e);
valueIterator.previous();
}
);
} else if (next != null) {
h += next.hashCode();
}
}
return h;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.google.common.collect.ImmutableList;
import com.hubspot.jinjava.BaseJinjavaTest;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.LegacyOverrides;
import com.hubspot.jinjava.interpret.IndexOutOfRangeException;
import com.hubspot.jinjava.interpret.RenderResult;
import com.hubspot.jinjava.interpret.TemplateError;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import org.junit.Test;
Expand Down Expand Up @@ -367,4 +369,61 @@ public void itUpdatesKeysWithVariableName() {
)
.isEqualTo("value2");
}

@Test
public void itComputesHashCodeWhenContainedWithinItself() {
PyMap map = new PyMap(new HashMap<>());
map.put("map1key1", "value1");

PyMap map2 = new PyMap(new HashMap<>());
map2.put("map2key1", map);

map.put("map1key2", map2);

assertThat(map.hashCode()).isEqualTo(-413943561);
}

@Test
public void itComputesHashCodeWhenContainedWithinItselfWithFurtherEntries() {
PyMap map = new PyMap(new HashMap<>());
map.put("map1key1", "value1");

PyMap map2 = new PyMap(new HashMap<>());
map2.put("map2key1", map);

map.put("map1key2", map2);

int originalHashCode = map.hashCode();
map2.put("newKey", "newValue");
int newHashCode = map.hashCode();
assertThat(originalHashCode).isNotEqualTo(newHashCode);
}

@Test
public void itComputesHashCodeWhenContainedWithinItselfInsideList() {
PyMap map = new PyMap(new HashMap<>());
map.put("map1key1", "value1");

PyMap map2 = new PyMap(new HashMap<>());
map2.put("map2key1", map);

map.put("map1key2", new PyList(ImmutableList.of((map2))));

assertThat(map.hashCode()).isEqualTo(-413943561);
}

@Test
public void itComputesHashCodeWithNullKeysAndValues() {
PyMap map = new PyMap(new HashMap<>());
map.put(null, "value1");

PyMap map2 = new PyMap(new HashMap<>());
map2.put("map2key1", map);

PyList list = new PyList(new ArrayList<>());
list.add(null);
map.put("map1key2", new PyList(list));

assertThat(map.hashCode()).isEqualTo(-687497624);
}
}