Skip to content

Commit

Permalink
Merge pull request #968 from rdesgroppes/honor-user-overriden-json-ge…
Browse files Browse the repository at this point in the history
…nerator-write-object

Honor overriden `writeObject` of `JsonGenerator` passed to `setJsonGeneratorDecorator`
  • Loading branch information
philsttr committed Jun 17, 2023
2 parents 16c92fc + c82d742 commit 9823bdf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private JsonGenerator createGenerator(OutputStream outputStream) throws IOExcept
}

private JsonGenerator decorateGenerator(JsonGenerator generator) {
JsonGenerator decorated = jsonGeneratorDecorator.decorate(generator)
JsonGenerator decorated = jsonGeneratorDecorator.decorate(new SimpleObjectJsonGeneratorDelegate(generator))
/*
* Don't let the json generator close the underlying outputStream and let the
* encoder managed it.
Expand All @@ -323,7 +323,7 @@ private JsonGenerator decorateGenerator(JsonGenerator generator) {
*/
}

return new SimpleObjectJsonGeneratorDelegate(decorated);
return decorated;
}

public JsonFactory getJsonFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package net.logstash.logback.composite.loggingevent;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -37,6 +38,7 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.spi.ContextAware;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.JsonGeneratorDelegate;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -133,4 +135,25 @@ public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOExcep
verify(decorator, times(2)).decorate(any(JsonGenerator.class));
}
}

/*
* When overriden, the writeObject method of the JsonGenerator passed to setJsonGeneratorDecorator is honored
*/
@Test
public void testOverriddenJsonGeneratorWriteObjectIsHonored() throws IOException {
when(event.getArgumentArray()).thenReturn(new Object[] {StructuredArguments.keyValue("answer", 42)});
formatter.setJsonGeneratorDecorator(generator -> new JsonGeneratorDelegate(generator) {
@Override
public void writeObject(final Object pojo) throws IOException {
super.writeObject(String.format("is <%s>", pojo));
}
});
((LoggingEventJsonProviders) formatter.getProviders()).addArguments(new ArgumentsJsonProvider());
formatter.start();

try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
formatter.writeEvent(event, bos);
assertThat(bos).hasToString("{\"answer\":\"is <42>\"}");
}
}
}

0 comments on commit 9823bdf

Please sign in to comment.