Repository: marshmallow-code/marshmallow

Issue:
missing and default values should be specified in deserialized form
Marshmallow expects `missing` and `default` values to be specified in a pre-serialized form, which is inconvenient and unintuitive.

Here is a schema which I would expect to work:

``` python
import datetime
import uuid

class TestSchema(Schema):
    id = fields.UUID(
        missing=uuid.uuid1)
    ts = fields.DateTime(
        format='rfc',
        missing=datetime.datetime.now)
```

However, it does not:

``` python
schema = TestSchema()
pprint.pprint(tuple(schema.load({})))
({}, {'id': [u'Not a valid UUID.'], 'ts': [u'Not a valid datetime.']})
```

Instead, I have to convert my `default` and `missing` values to primitive types, which undermines the purpose of a library like marshmallow that already knows how to do this.  For data types like datetimes, which have many possible serialized formats, I have to keep the formatting of my `default` and `missing` values in sync with my field properties, which is fragile and convoluted:

``` python
from email.Utils import formatdate

class TestSchema(Schema):
    id = fields.UUID(
        missing=lambda: str(uuid.uuid1()))
    ts = fields.DateTime(
        format='rfc',
        missing=formatdate)
```

In the case of `default` values, marshmallow does not attempt to validate them, so no error is raised, but the resulting document is not deserializable. 


Discussion:
lafrech: TODO: update docs.
sloria: Behavior looks good. This should be good to merge after docs are updated.

Patch:
diff --git a/docs/quickstart.rst b/docs/quickstart.rst
index db101bd1..1ee80e53 100644
--- a/docs/quickstart.rst
+++ b/docs/quickstart.rst
@@ -481,6 +481,25 @@ In the context of a web API, the ``dump_only`` and ``load_only`` parameters are
         created_at = fields.DateTime(dump_only=True)
 
 
+Specify default Serialization/Deserialization values
+----------------------------------------------------
+
+Default values can be provided to a :class:`Field <fields.Field>` for both serialization and deserialization.
+
+`missing` is used for deserialization if the field is not found in the input data. Likewise, `default` is used for serialization if the input value is missing.
+
+.. code-block:: python
+
+    class UserSchema(Schema):
+        id = fields.UUID(missing=uuid.uuid1)
+        birthdate = fields.DateTime(default=dt.datetime(2017, 9, 29))
+
+    UserSchema().load({})
+    # {'id': UUID('337d946c-32cd-11e8-b475-0022192ed31b')}
+    UserSchema().dump({})
+    # {'birthdate': '2017-09-29T00:00:00+00:00'}
+
+
 Next Steps
 ----------
 
diff --git a/docs/upgrading.rst b/docs/upgrading.rst
index 9683f2a8..1c439c80 100644
--- a/docs/upgrading.rst
+++ b/docs/upgrading.rst
@@ -393,6 +393,23 @@ The ``json_module`` class Meta option is deprecated in favor of ``render_module`
         class Meta:
             render_module = ujson
 
+
+``missing`` and ``default`` ``Field`` parameters are passed in deserialized form
+********************************************************************************
+
+.. code-block:: python
+
+    # 2.x
+    class UserSchema(Schema):
+        id = fields.UUID(missing=lambda: str(uuid.uuid1()))
+        birthdate = fields.DateTime(default=lambda: dt.datetime(2017, 9, 29).isoformat())
+
+    # 3.x
+    class UserSchema(Schema):
+        id = fields.UUID(missing=uuid.uuid1)
+        birthdate = fields.DateTime(default=dt.datetime(2017, 9, 29))
+
+
 Pass ``default`` as a keyword argument
 **************************************
 
diff --git a/marshmallow/fields.py b/marshmallow/fields.py
index 832ae1a8..946865e1 100755
--- a/marshmallow/fields.py
+++ b/marshmallow/fields.py
@@ -105,7 +105,7 @@ class Field(FieldABC):
         inputs are excluded from serialized output.
     """
     # Some fields, such as Method fields and Function fields, are not expected
-    #  to exists as attributes on the objects to serialize. Set this to False
+    #  to exist as attributes on the objects to serialize. Set this to False
     #  for those fields
     _CHECK_ATTRIBUTE = True
     _creation_index = 0  # Used for sorting
@@ -237,12 +237,11 @@ class Field(FieldABC):
         """
         if self._CHECK_ATTRIBUTE:
             value = self.get_value(obj, attr, accessor=accessor)
+            if value is missing_ and hasattr(self, 'default'):
+                default = self.default
+                value = default() if callable(default) else default
             if value is missing_:
-                if hasattr(self, 'default'):
-                    if callable(self.default):
-                        return self.default()
-                    else:
-                        return self.default
+                return value
         else:
             value = None
         return self._serialize(value, attr, obj)
@@ -256,6 +255,9 @@ class Field(FieldABC):
         # Validate required fields, deserialize, then validate
         # deserialized value
         self._validate_missing(value)
+        if value is missing_:
+            _miss = self.missing
+            return _miss() if callable(_miss) else _miss
         if getattr(self, 'allow_none', False) is True and value is None:
             return None
         output = self._deserialize(value, attr, data)
diff --git a/marshmallow/marshalling.py b/marshmallow/marshalling.py
index 1f3b2a32..162c722e 100644
--- a/marshmallow/marshalling.py
+++ b/marshmallow/marshalling.py
@@ -276,11 +276,6 @@ class Unmarshaller(ErrorStore):
                     (partial_is_collection and attr_name in partial)
                 ):
                     continue
-                _miss = field_obj.missing
-                raw_value = _miss() if callable(_miss) else _miss
-            if raw_value is missing and not field_obj.required:
-                continue
-
             getter = lambda val: field_obj.deserialize(val, field_name, data)
             value = self.call_and_store(
                 getter_func=getter,


Test patch:
diff --git a/tests/test_deserialization.py b/tests/test_deserialization.py
index 881c6098..b3a6bfd4 100644
--- a/tests/test_deserialization.py
+++ b/tests/test_deserialization.py
@@ -1105,26 +1105,30 @@ class TestSchemaDeserialization:
         assert 'nicknames' not in result
 
     def test_deserialize_with_missing_param_value(self):
+        bdate = dt.datetime(2017, 9, 29)
+
         class AliasingUserSerializer(Schema):
             name = fields.String()
-            years = fields.Integer(missing=10)
+            birthdate = fields.DateTime(missing=bdate)
         data = {
             'name': 'Mick',
         }
         result = AliasingUserSerializer().load(data)
         assert result['name'] == 'Mick'
-        assert result['years'] == 10
+        assert result['birthdate'] == bdate
 
     def test_deserialize_with_missing_param_callable(self):
+        bdate = dt.datetime(2017, 9, 29)
+
         class AliasingUserSerializer(Schema):
             name = fields.String()
-            years = fields.Integer(missing=lambda: 13 + 7)
+            birthdate = fields.DateTime(missing=lambda: bdate)
         data = {
             'name': 'Mick',
         }
         result = AliasingUserSerializer().load(data)
         assert result['name'] == 'Mick'
-        assert result['years'] == 20
+        assert result['birthdate'] == bdate
 
     def test_deserialize_with_missing_param_none(self):
         class AliasingUserSerializer(Schema):
diff --git a/tests/test_serialization.py b/tests/test_serialization.py
index 24826eb5..e8e6c360 100644
--- a/tests/test_serialization.py
+++ b/tests/test_serialization.py
@@ -29,10 +29,6 @@ class TestFieldSerialization:
     def user(self):
         return User("Foo", email="foo@bar.com", age=42)
 
-    def test_default(self, user):
-        field = fields.Field(default='nan')
-        assert field.serialize('age', {}) == 'nan'
-
     @pytest.mark.parametrize(('value', 'expected'),
     [
         (42, float(42)),
@@ -54,10 +50,6 @@ class TestFieldSerialization:
         field = fields.Number(as_string=True, allow_none=True)
         assert field.serialize('age', user) is None
 
-    def test_callable_default(self, user):
-        field = fields.Field(default=lambda: 'nan')
-        assert field.serialize('age', {}) == 'nan'
-
     def test_function_field_passed_func(self, user):
         field = fields.Function(lambda obj: obj.name.upper())
         assert "FOO" == field.serialize("key", user)
@@ -815,6 +807,29 @@ class TestFieldSerialization:
         else:
             assert res is None
 
+class TestSchemaSerialization:
+
+    def test_serialize_with_missing_param_value(self):
+        class AliasingUserSerializer(Schema):
+            name = fields.String()
+            birthdate = fields.DateTime(default=dt.datetime(2017, 9, 29))
+        data = {
+            'name': 'Mick',
+        }
+        result = AliasingUserSerializer().dump(data)
+        assert result['name'] == 'Mick'
+        assert result['birthdate'] == '2017-09-29T00:00:00+00:00'
+
+    def test_serialize_with_missing_param_callable(self):
+        class AliasingUserSerializer(Schema):
+            name = fields.String()
+            birthdate = fields.DateTime(default=lambda: dt.datetime(2017, 9, 29))
+        data = {
+            'name': 'Mick',
+        }
+        result = AliasingUserSerializer().dump(data)
+        assert result['name'] == 'Mick'
+        assert result['birthdate'] == '2017-09-29T00:00:00+00:00'
 
 def test_serializing_named_tuple():
     Point = namedtuple('Point', ['x', 'y'])