Skip to content

Commit 727aa26

Browse files
authored
Deserialization test cases for WRAP_EXCEPTIONS and builders (#2938)
This test illustrates a change in behavior between 2.11 and 2.12 where 2.12 no longer rethrows JsonProcessingException.
1 parent 1a994c8 commit 727aa26

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderSimpleTest.java

+111
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1111
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
1212
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
13+
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
1314
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
1415

1516
public class BuilderSimpleTest extends BaseMapTest
@@ -295,6 +296,54 @@ private Value2354 build() {
295296
}
296297
}
297298

299+
@JsonDeserialize(builder = ValidatingValue.Builder.class)
300+
static class ValidatingValue
301+
{
302+
private final String first;
303+
private final String second;
304+
305+
ValidatingValue(String first, String second) {
306+
this.first = first;
307+
this.second = second;
308+
}
309+
310+
static class ValidationException extends RuntimeException
311+
{
312+
ValidationException(String message) {
313+
super(message);
314+
}
315+
}
316+
317+
static class Builder
318+
{
319+
320+
private String first;
321+
private String second;
322+
323+
@JsonSetter("a")
324+
Builder first(String value) {
325+
this.first = value;
326+
return this;
327+
}
328+
329+
@JsonSetter("b")
330+
Builder second(String value) {
331+
this.second = value;
332+
return this;
333+
}
334+
335+
ValidatingValue build() {
336+
if (first == null) {
337+
throw new ValidationException("Missing first");
338+
}
339+
if (second == null) {
340+
throw new ValidationException("Missing second");
341+
}
342+
return new ValidatingValue(first, second);
343+
}
344+
}
345+
}
346+
298347
/*
299348
/**********************************************************
300349
/* Test methods
@@ -430,4 +479,66 @@ public void testPrivateInnerBuilder() throws Exception
430479
Value2354 result = MAPPER.readValue(json, Value2354.class);
431480
assertEquals(13, result.value());
432481
}
482+
483+
public void testSuccessfulValidatingBuilder() throws Exception
484+
{
485+
ValidatingValue result = MAPPER.readValue(aposToQuotes("{'a':'1','b':'2'}"), ValidatingValue.class);
486+
assertEquals("1", result.first);
487+
assertEquals("2", result.second);
488+
}
489+
490+
491+
public void testFailingValidatingBuilderWithExceptionWrapping() throws Exception
492+
{
493+
ObjectMapper withWrapping = MAPPER.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS)
494+
? MAPPER : MAPPER.copy().enable(DeserializationFeature.WRAP_EXCEPTIONS);
495+
try {
496+
withWrapping
497+
.readValue(aposToQuotes("{'a':'1'}"), ValidatingValue.class);
498+
fail("Expected an exception");
499+
} catch (JsonMappingException e) {
500+
assertTrue(e.getMessage().contains("Missing second"));
501+
assertTrue(e.getCause() instanceof ValidatingValue.ValidationException);
502+
}
503+
}
504+
505+
public void testFailingValidatingBuilderWithExceptionWrappingFromTree() throws Exception
506+
{
507+
ObjectMapper withWrapping = MAPPER.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS)
508+
? MAPPER : MAPPER.copy().enable(DeserializationFeature.WRAP_EXCEPTIONS);
509+
try {
510+
JsonNode tree = withWrapping.readTree(aposToQuotes("{'a':'1'}"));
511+
withWrapping.treeToValue(tree, ValidatingValue.class);
512+
fail("Expected an exception");
513+
} catch (ValueInstantiationException e) {
514+
assertTrue(e.getMessage().contains("Missing second"));
515+
assertTrue(e.getCause() instanceof ValidatingValue.ValidationException);
516+
}
517+
}
518+
519+
public void testFailingValidatingBuilderWithoutExceptionWrapping() throws Exception
520+
{
521+
ObjectMapper withoutWrapping = MAPPER.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS)
522+
? MAPPER.copy().disable(DeserializationFeature.WRAP_EXCEPTIONS) : MAPPER;
523+
try {
524+
withoutWrapping
525+
.readValue(aposToQuotes("{'a':'1'}"), ValidatingValue.class);
526+
fail("Expected an exception");
527+
} catch (ValidatingValue.ValidationException e) {
528+
assertEquals("Missing second", e.getMessage());
529+
}
530+
}
531+
532+
public void testFailingValidatingBuilderWithoutExceptionWrappingFromTree() throws Exception
533+
{
534+
ObjectMapper withoutWrapping = MAPPER.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS)
535+
? MAPPER.copy().disable(DeserializationFeature.WRAP_EXCEPTIONS) : MAPPER;
536+
try {
537+
JsonNode tree = withoutWrapping.readTree(aposToQuotes("{'a':'1'}"));
538+
withoutWrapping.treeToValue(tree, ValidatingValue.class);
539+
fail("Expected an exception");
540+
} catch (ValidatingValue.ValidationException e) {
541+
assertEquals("Missing second", e.getMessage());
542+
}
543+
}
433544
}

0 commit comments

Comments
 (0)