Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,29 @@ public JsonNode getJsonNode() {
return jsonNode;
}

void normalizeKeys(Map<String, Object> map) {
Map<String, Object> toAdd = new HashMap<>();
private Map<String, Object> normalizeKeys(final Map<String, Object> map) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it possible that there could be existing use cases that are functioning based on the expectation that the map they are passing gets modified?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@san81 , Possibly, but I don't think we should support that. For example, if you create the Map in the builder that always copies.

final Map<String, Object> replacementMap = new HashMap<>();
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
String key = entry.getKey();
Object value = entry.getValue();
final String newKey = JacksonEventKey.replaceInvalidCharacters(key);
if (value instanceof Map) {
normalizeKeys((Map<String, Object>)value);
}
if (!newKey.equals(key)) {
toAdd.put(newKey, value);
iterator.remove();
value = normalizeKeys((Map<String, Object>)value);
}
replacementMap.put(newKey, value);
}
map.putAll(toAdd);
return replacementMap;
}

@Override
public void put(EventKey key, Object value, boolean replaceInvalidCharacters) {
if (replaceInvalidCharacters && (value instanceof Map)) {
normalizeKeys((Map<String, Object>)value);
put(key, normalizeKeys((Map<String, Object>)value));
} else {
put(key, value);
}
put(key, value);
}

@Override
Expand Down Expand Up @@ -203,13 +201,14 @@ public void put(EventKey key, Object value) {

@Override
public void put(String key, final Object value, final boolean replaceInvalidCharacters) {
Object valueToPut = value;
if (replaceInvalidCharacters) {
key = JacksonEventKey.replaceInvalidCharacters(key);
if (value instanceof Map) {
normalizeKeys((Map<String, Object>)value);
valueToPut = normalizeKeys((Map<String, Object>)value);
}
}
put(key, value);
put(key, valueToPut);
}

public EventFailureMetadata updateFailureMetadata() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,37 @@ public void testPutWithReplaceInvalidKeyChars(final String key) {
assertThat(event.get("key_1", String.class), equalTo(value));
}

@ParameterizedTest
@ValueSource(strings = {"key&1", "key^1", "key%1", "key_1"})
public void testPutWithReplaceInvalidKeyChars_for_map(final String key) {
final String value = UUID.randomUUID().toString();
final Map<String, String> mapToPut = Map.of(key, value);

event.put("myMap", mapToPut, true);
assertThat(event.get("myMap/key_1", String.class), equalTo(value));
}

@ParameterizedTest
@ValueSource(strings = {"key&1", "key^1", "key%1", "key_1"})
public void testPutEventKeyWithReplaceInvalidKeyChars_for_map(final String key) {
final String value = UUID.randomUUID().toString();
final Map<String, String> mapToPut = Map.of(key, value);

final EventKey eventKey = new JacksonEventKey("myMap");
event.put(eventKey, mapToPut, true);
assertThat(event.get("myMap/key_1", String.class), equalTo(value));
}

@ParameterizedTest
@ValueSource(strings = {"key&1", "key^1", "key%1", "key_1"})
public void testPutEventKeyWithReplaceInvalidKeyCharsFalse_for_map(final String key) {
final String value = UUID.randomUUID().toString();
final Map<String, String> mapToPut = Map.of(key, value);

final EventKey eventKey = new JacksonEventKey("myMap");
event.put(eventKey, mapToPut, false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No assertion needed here?

}

@ParameterizedTest
@ValueSource(strings = {"key&1", "key^1", "key%1"})
public void testPutWithoutReplaceInvalidKeyChars(final String key) {
Expand Down
Loading