From cbdbc946292275cb4a7ddaf302833c5cd45b46b2 Mon Sep 17 00:00:00 2001 From: Ted Liang Date: Sun, 23 May 2021 20:30:19 +1000 Subject: [PATCH] Skip children when InvalidObject is found in list --- .../beanvalidation/BeanValidationModule.java | 15 +++++++++++++++ .../beanvalidation/KotlinValidationTest.kt | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/unbrokendome/jackson/beanvalidation/BeanValidationModule.java b/src/main/java/org/unbrokendome/jackson/beanvalidation/BeanValidationModule.java index dbf50e9..34c906f 100644 --- a/src/main/java/org/unbrokendome/jackson/beanvalidation/BeanValidationModule.java +++ b/src/main/java/org/unbrokendome/jackson/beanvalidation/BeanValidationModule.java @@ -1,7 +1,9 @@ package org.unbrokendome.jackson.beanvalidation; +import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.Version; import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; @@ -83,6 +85,19 @@ public Object handleInstantiationProblem(DeserializationContext ctxt, Class i return super.handleInstantiationProblem(ctxt, instClass, argument, t); } } + + @Override + public boolean handleUnknownProperty( + DeserializationContext ctxt, JsonParser p, JsonDeserializer deserializer, + Object beanOrClass, String propertyName + ) throws IOException { + + if (beanOrClass instanceof InvalidObject) { + p.skipChildren(); + return true; + } + return super.handleUnknownProperty(ctxt, p, deserializer, beanOrClass, propertyName); + } }); } } diff --git a/src/test/kotlin/org/unbrokendome/jackson/beanvalidation/KotlinValidationTest.kt b/src/test/kotlin/org/unbrokendome/jackson/beanvalidation/KotlinValidationTest.kt index 9f64d3e..9c80fe6 100644 --- a/src/test/kotlin/org/unbrokendome/jackson/beanvalidation/KotlinValidationTest.kt +++ b/src/test/kotlin/org/unbrokendome/jackson/beanvalidation/KotlinValidationTest.kt @@ -144,7 +144,7 @@ class KotlinValidationTest : AbstractValidationTest() { private class NestedListArgumentsProvider : ArgumentsProvider { override fun provideArguments(context: ExtensionContext): Stream = Stream.of( - Arguments.of("nested[0].value", """{ "nested": [{}] }"""), + Arguments.of("nested[0].value", """{ "nested": [{}, {"value":"v1"}] }"""), Arguments.of("nested[1].value", """{ "nested": [{"value":"test"},{}] }"""), Arguments.of("nested[2].value", """{ "nested": [{"value":"1"},{"value":"2"},{}] }""") )