Skip to content

Commit 75b7129

Browse files
committed
narrow down the scope where unknown Json property is more tolerated
1 parent 3d66002 commit 75b7129

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

service/src/main/java/uk/nhs/adaptors/gp2gp/common/configuration/ObjectMapperConfig.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package uk.nhs.adaptors.gp2gp.common.configuration;
22

33
import com.fasterxml.jackson.core.StreamReadConstraints;
4-
import com.fasterxml.jackson.databind.DeserializationFeature;
54
import com.fasterxml.jackson.databind.ObjectMapper;
65
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
76
import org.springframework.context.annotation.Bean;
@@ -13,9 +12,7 @@ public class ObjectMapperConfig {
1312
@Bean
1413
public ObjectMapper objectMapper() {
1514
ObjectMapper mapper = new ObjectMapper();
16-
mapper.getFactory().setStreamReadConstraints(
17-
StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build());
18-
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
15+
mapper.getFactory().setStreamReadConstraints(StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build());
1916
mapper.registerModule(new JavaTimeModule());
2017

2118
return mapper;

service/src/main/java/uk/nhs/adaptors/gp2gp/common/task/TaskDefinition.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ public abstract class TaskDefinition {
4343
*/
4444
private final String toOdsCode;
4545

46+
private final TaskType taskType;
47+
4648
public abstract TaskType getTaskType();
4749
}

service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/SendAcknowledgementTaskDefinition.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static uk.nhs.adaptors.gp2gp.common.task.TaskType.SEND_ACKNOWLEDGEMENT;
44

5+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
56
import lombok.EqualsAndHashCode;
67
import lombok.Getter;
78
import lombok.experimental.SuperBuilder;
@@ -12,6 +13,7 @@
1213
/**
1314
* Task definition for sending acknowledgment
1415
*/
16+
@JsonIgnoreProperties(ignoreUnknown = true)
1517
@Jacksonized
1618
@SuperBuilder
1719
@Getter

service/src/test/java/uk/nhs/adaptors/gp2gp/common/configuration/ObjectMapperConfigTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.fasterxml.jackson.core.StreamReadConstraints;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
67
import org.junit.jupiter.api.Test;
78
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
89

9-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1010
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
1112
import static org.junit.jupiter.api.Assertions.assertTrue;
1213

1314
class ObjectMapperConfigTest {
@@ -28,7 +29,7 @@ void objectMapperConfigSetToWorkWithUnlimitedDataSizeTest() {
2829
}
2930

3031
@Test
31-
public void javaTimeModuleIsRegisteredTest() {
32+
void javaTimeModuleIsRegisteredTest() {
3233
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ObjectMapperConfig.class);
3334
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
3435

@@ -37,14 +38,14 @@ public void javaTimeModuleIsRegisteredTest() {
3738
}
3839

3940
@Test
40-
public void objectMapperConfigIgnoresUnknownPropertiesTest() {
41+
void objectMapperConfigDoesNotIgnoreUnknownPropertiesTest() {
4142

4243
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ObjectMapperConfig.class);
4344
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
4445

4546
String jsonWithUnknownProperty = "{\"knownProperty\":\"testValue\", \"unknownProperty\":\"extraValue\"}";
4647

47-
assertDoesNotThrow(() -> {
48+
assertThrows(UnrecognizedPropertyException.class, () -> {
4849
TestClass result = objectMapper.readValue(jsonWithUnknownProperty, TestClass.class);
4950
assertEquals("testValue", result.knownProperty);
5051
});

0 commit comments

Comments
 (0)