Avoid property name collisions when serializing AuditEvent to JSON
Previously, in case the data for the audit event contained an entry with the key "type", the member `type` from the AuditEvent would be overwritten when rendering to JSON due to the use of @JsonAnyGetter on the data property. This commit removes @JsonAnyGetter so that the data map is rendered as a separate property in the JSON. Closes gh-7990
This commit is contained in:
committed by
Andy Wilkinson
parent
3fcdd310d3
commit
fcf36ed091
@@ -22,7 +22,6 @@ import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
@@ -137,7 +136,6 @@ public class AuditEvent implements Serializable {
|
||||
* Returns the event data.
|
||||
* @return the event data
|
||||
*/
|
||||
@JsonAnyGetter
|
||||
public Map<String, Object> getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
@@ -18,10 +18,13 @@ package org.springframework.boot.actuate.audit;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -74,4 +77,14 @@ public class AuditEventTests {
|
||||
new AuditEvent("phil", null, Collections.singletonMap("a", (Object) "b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonFormat() throws Exception {
|
||||
AuditEvent event = new AuditEvent("johannes", "UNKNOWN", Collections.singletonMap("type", (Object) "BadCredentials"));
|
||||
|
||||
String json = Jackson2ObjectMapperBuilder.json().build().writeValueAsString(event);
|
||||
JSONObject jsonObject = new JSONObject(json);
|
||||
|
||||
assertThat(jsonObject.getString("type")).isEqualTo("UNKNOWN");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user