From fcf36ed091a7a81f4f59e1b6582f212f5f2c51e7 Mon Sep 17 00:00:00 2001 From: Johannes Edmeier Date: Sun, 15 Jan 2017 17:40:15 +0100 Subject: [PATCH 1/2] 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 --- .../boot/actuate/audit/AuditEvent.java | 2 -- .../boot/actuate/audit/AuditEventTests.java | 13 +++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java index 5a25dd009a..3a956f7810 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java @@ -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 getData() { return this.data; } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java index 4313f5f304..096cf4b2c1 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java @@ -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"); + } + } From 3e88c366ec007c884f2ff0ecc56cc126ceb2b2bd Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 19 Jan 2017 12:38:35 +0000 Subject: [PATCH 2/2] Polish "Avoid property name collisions when serializing AuditEvent to JSON" See gh-7990 --- .../boot/actuate/audit/AuditEvent.java | 2 +- .../boot/actuate/audit/AuditEventTests.java | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java index 3a956f7810..ae96349f10 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java index 096cf4b2c1..1665a9353a 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,12 +79,15 @@ public class AuditEventTests { @Test public void jsonFormat() throws Exception { - AuditEvent event = new AuditEvent("johannes", "UNKNOWN", Collections.singletonMap("type", (Object) "BadCredentials")); - - String json = Jackson2ObjectMapperBuilder.json().build().writeValueAsString(event); + AuditEvent event = new AuditEvent("johannes", "UNKNOWN", + Collections.singletonMap("type", (Object) "BadCredentials")); + String json = Jackson2ObjectMapperBuilder.json().build() + .writeValueAsString(event); + System.out.println(json); JSONObject jsonObject = new JSONObject(json); - assertThat(jsonObject.getString("type")).isEqualTo("UNKNOWN"); + assertThat(jsonObject.getJSONObject("data").getString("type")) + .isEqualTo("BadCredentials"); } }