Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow failing on null values for creator (DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES) #990

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*<p>
* Note that having an injectable value counts as "not missing".
*<p>
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> 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;
}

/*
Expand Down