Repository: s-knibbs/dataclasses-jsonschema (python)

Issue:
Unrecognized enum values not handled correctly if the field is optional
The following will raise a ValueError when it shouldn't:

```python
    class PetType(Enum):
        CAT = "cat"
        DOG = "dog"

    @dataclass
    class Pet(JsonSchemaMixin):
        name: str
        type: Optional[PetType] = None

    p = Pet.from_dict({'name': 'snakey', 'type': 'python'}, validate_enums=False)
```

This happens because we don't check if the field is optional here:

```python
                try:
                    values[f.field.name] = cls._decode_field(f.field.name, f.field.type, data.get(f.mapped_name))
                except ValueError:
                    if is_enum(f.field.type):
                        values[f.field.name] = data.get(f.mapped_name)
                    else:
                        raise

```

Pull request description:
Handle unrecognized values for optional enum fields
Fixes #134 

Patch:
diff --git a/dataclasses_jsonschema/__init__.py b/dataclasses_jsonschema/__init__.py
index b78525a..4350d22 100644
--- a/dataclasses_jsonschema/__init__.py
+++ b/dataclasses_jsonschema/__init__.py
@@ -498,7 +498,8 @@ class JsonSchemaMixin:
                 try:
                     values[f.field.name] = cls._decode_field(f.field.name, f.field.type, data.get(f.mapped_name))
                 except ValueError:
-                    if is_enum(f.field.type):
+                    ftype = unwrap_optional(f.field.type) if is_optional(f.field.type) else f.field.type
+                    if is_enum(ftype):
                         values[f.field.name] = data.get(f.mapped_name)
                     else:
                         raise
@@ -586,7 +587,7 @@ class JsonSchemaMixin:
             if schema_type == SchemaType.OPENAPI_3:
                 field_meta.read_only = field.metadata.get("read_only")
                 if field_meta.read_only and default_value is MISSING:
-                    warnings.warn(f"Read-only fields should have a default value")
+                    warnings.warn("Read-only fields should have a default value")
                 field_meta.write_only = field.metadata.get("write_only")
         return field_meta, required
 


Test patch:
diff --git a/tests/test_core.py b/tests/test_core.py
index 54569b1..56605bf 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -899,16 +899,22 @@ def test_unrecognized_enum_value():
         CAT = "cat"
         DOG = "dog"
 
+    class FoodType(Enum):
+        KIBBLE = "kibble"
+        FRESH = "fresh"
+
     @dataclass
     class Pet(JsonSchemaMixin):
         name: str
         type: PetType
+        favourite_food: Optional[FoodType] = None
 
-    p = Pet.from_dict({'name': 'snakey', 'type': 'python'}, validate_enums=False)
+    p = Pet.from_dict({'name': 'snakey', 'type': 'python', 'favourite_food': 'mice'}, validate_enums=False)
     assert p.type == "python"
+    assert p.favourite_food == "mice"
 
     with pytest.warns(UserWarning):
-        assert p.to_dict() == {'name': 'snakey', 'type': 'python'}
+        assert p.to_dict() == {'name': 'snakey', 'type': 'python', 'favourite_food': 'mice'}
 
 
 def test_inheritance_and_additional_properties_disallowed():