From 525696d2af6066422129fa0d1dcd69e71f75db8c Mon Sep 17 00:00:00 2001 From: nick Date: Thu, 29 Oct 2015 08:16:24 +0000 Subject: [PATCH] example of fail on null creators for discussion --- .../jackson/databind/DeserializationFeature.java | 16 ++++++++++++++++ .../databind/deser/impl/PropertyValueBuffer.java | 10 +++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java b/src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java index 53da367364..bb5bc685c9 100644 --- a/src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java +++ b/src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java @@ -230,6 +230,22 @@ public enum DeserializationFeature implements ConfigFeature * @since 2.6 */ FAIL_ON_MISSING_CREATOR_PROPERTIES(false), + + /** + * Feature that determines what happens if one or more Creator properties (properties + * bound to parameters of Creator method (constructor or static factory method)) + * are are bound to null values - either from the JSON or as a default value. This + * is useful if you want to avoid nulls in your codebase, and particularly useful + * if you are using Java or Scala optionals for non-mandatory fields. + *

+ * Note that having an injectable value counts as "not missing". + *

+ * Feature is disabled by default, so that no exception is thrown for missing creator + * property values, unless they are explicitly marked as `required`. + * + * @since 2.6 + */ + FAIL_ON_NULL_CREATOR_PROPERTIES(false), /** * Feature that determines whether Jackson code should catch diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java index 32e100b7e0..7f63ee221e 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java @@ -145,9 +145,17 @@ protected Object _findMissing(SettableBeanProperty prop) throws JsonMappingExcep throw _context.mappingException("Missing creator property '%s' (index %d); DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES enabled", prop.getName(), prop.getCreatorIndex()); } + // Third: default value JsonDeserializer deser = prop.getValueDeserializer(); - return deser.getNullValue(_context); + Object value = deser.getNullValue(_context); + + if (_context.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES) && value == null) { + throw _context.mappingException("Null value for creator property '%s'; DeserializationFeature.FAIL_ON_NULL_FOR_CREATOR_PARAMETERS enabled", + prop.getName(), prop.getCreatorIndex()); + } + + return value; } /*