Skip to content

Serialize values for debug output safely in AbstractMongoEventListener. #3970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3968-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3968-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3968-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3968-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.springframework.context.ApplicationListener;
import org.springframework.core.GenericTypeResolver;
import org.springframework.data.mongodb.core.query.SerializationUtils;

/**
* Base class to implement domain class specific {@link ApplicationListener}s.
Expand Down Expand Up @@ -104,7 +105,7 @@ public void onApplicationEvent(MongoMappingEvent<?> event) {
public void onBeforeConvert(BeforeConvertEvent<E> event) {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onBeforeConvert(%s)", event.getSource()));
LOG.debug(String.format("onBeforeConvert(%s)", SerializationUtils.serializeToJsonSafely(event.getSource())));
}
}

Expand All @@ -117,7 +118,7 @@ public void onBeforeConvert(BeforeConvertEvent<E> event) {
public void onBeforeSave(BeforeSaveEvent<E> event) {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onBeforeSave(%s, %s)", event.getSource(), event.getDocument()));
LOG.debug(String.format("onBeforeSave(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument())));
}
}

Expand All @@ -130,7 +131,7 @@ public void onBeforeSave(BeforeSaveEvent<E> event) {
public void onAfterSave(AfterSaveEvent<E> event) {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onAfterSave(%s, %s)", event.getSource(), event.getDocument()));
LOG.debug(String.format("onAfterSave(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument())));
}
}

Expand All @@ -143,7 +144,7 @@ public void onAfterSave(AfterSaveEvent<E> event) {
public void onAfterLoad(AfterLoadEvent<E> event) {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onAfterLoad(%s)", event.getDocument()));
LOG.debug(String.format("onAfterLoad(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
}
}

Expand All @@ -156,7 +157,7 @@ public void onAfterLoad(AfterLoadEvent<E> event) {
public void onAfterConvert(AfterConvertEvent<E> event) {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onAfterConvert(%s, %s)", event.getDocument(), event.getSource()));
LOG.debug(String.format("onAfterConvert(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getDocument()), SerializationUtils.serializeToJsonSafely(event.getSource())));
}
}

Expand All @@ -169,7 +170,7 @@ public void onAfterConvert(AfterConvertEvent<E> event) {
public void onAfterDelete(AfterDeleteEvent<E> event) {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onAfterDelete(%s)", event.getDocument()));
LOG.debug(String.format("onAfterDelete(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
}
}

Expand All @@ -182,7 +183,7 @@ public void onAfterDelete(AfterDeleteEvent<E> event) {
public void onBeforeDelete(BeforeDeleteEvent<E> event) {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onBeforeDelete(%s)", event.getDocument()));
LOG.debug(String.format("onBeforeDelete(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@

import static org.assertj.core.api.Assertions.*;

import java.time.Instant;

import org.bson.Document;
import org.junit.jupiter.api.Test;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.mapping.Account;
import org.springframework.data.mongodb.repository.Contact;
import org.springframework.data.mongodb.repository.Person;

import com.mongodb.BasicDBObject;

/**
* Unit tests for {@link AbstractMongoEventListener}.
*
Expand Down Expand Up @@ -154,6 +157,14 @@ public void donInvokePersonCallbackForUntypedEvent() {
assertThat(listener.invokedOnBeforeDelete).isFalse();
}

@Test // GH-3968
public void debugLogShouldNotFailMongoDBCodecError() {

MongoMappingEvent<BasicDBObject> event = new BeforeConvertEvent<>(new BasicDBObject("date", Instant.now()), "collection-1");
UntypedEventListener listener = new UntypedEventListener();
listener.onApplicationEvent(event);
}

class SamplePersonEventListener extends AbstractMongoEventListener<Person> {

boolean invokedOnBeforeConvert;
Expand Down
1 change: 1 addition & 0 deletions spring-data-mongodb/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
-->

<logger name="org.springframework.data.mongodb.core" level="error"/>
<logger name="org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener" level="debug"/>
<logger name="org.springframework.data.mongodb.test.util" level="info"/>

<root level="error">
Expand Down