Skip to content

Commit

Permalink
EPA-161: [ehealthid] Better logging on debug level for JSON decoding (#…
Browse files Browse the repository at this point in the history
…109)

Co-authored-by: francogrbac-oviva <[email protected]>
  • Loading branch information
thomasrichner-oviva and francogrbac-oviva authored Oct 28, 2024
1 parent a4c4dd2 commit 5620df3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
11 changes: 6 additions & 5 deletions ehealthid/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>

<!-- BEGIN wiremock // fix for broken dependency convergence -->
<dependency>
Expand Down Expand Up @@ -133,6 +128,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
16 changes: 15 additions & 1 deletion ehealthid/src/main/java/com/oviva/ehealthid/util/JsonCodec.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package com.oviva.ehealthid.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonCodec {

private static final Logger logger = LoggerFactory.getLogger(JsonCodec.class);
private static ObjectMapper om;
private static ObjectMapper debugOm;

static {
var om = new ObjectMapper();

om.registerModule(new JoseModule());
om.setSerializationInclusion(Include.NON_NULL);
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

if (logger.isDebugEnabled()) {
logger.debug("debug level enabled, including source location in error messages");
debugOm = om.copy();
debugOm.enable(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION);
}

JsonCodec.om = om;
}
Expand All @@ -32,6 +43,9 @@ public static String writeValueAsString(Object value) {

public static <T> T readValue(byte[] in, Class<T> clazz) {
try {
if (debugOm != null) {
return debugOm.readValue(in, clazz);
}
return om.readValue(in, clazz);
} catch (IOException e) {
throw new DeserializeException("failed to deserialize JSON", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -43,6 +41,35 @@ static List<FailingEncodeTC> testCasesFailingEncode() {
new FailingEncodeTC(new JsonCodecTest(), SerializeException.class));
}

@Test
void testDebug_includeSource() {

var raw = """
{"hello": "world" }\
""";
var bytes = raw.getBytes(StandardCharsets.UTF_8);

// when
var e =
assertThrows(
DeserializeException.class, () -> JsonCodec.readValue(bytes, HelloWorld.class));

// then
var msg = allMessages(e);
assertTrue(msg.contains(raw));
}

private String allMessages(Throwable e) {
var buf = new StringBuilder();
while (e != null) {
buf.append(e.getMessage()).append('\n');
e = e.getCause();
}
return buf.toString();
}

private record HelloWorld(List<String> hello) {}

@Test
void readBadJson() {
var raw = """
Expand Down

0 comments on commit 5620df3

Please sign in to comment.