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

Issue:
Support field descriptions using field metadata
Additional metadata can be given to [dataclass fields](https://docs.python.org/3/library/dataclasses.html#dataclasses.field).

This could be used to provide a description of each field like so:

```python
@dataclass
class InventoryItem(JsonSchemaMixin):
    '''Class for keeping track of an item in inventory.'''
    name: str
    unit_price: float = field(metadata=dict(description="Unit price in £"))
    quantity_on_hand: int = 0
```

Pull request description:
Support property descriptions using field metadata
Closes #47 

Patch:
diff --git a/dataclasses_jsonschema/__init__.py b/dataclasses_jsonschema/__init__.py
index b7628f0..9dc52d5 100644
--- a/dataclasses_jsonschema/__init__.py
+++ b/dataclasses_jsonschema/__init__.py
@@ -318,6 +318,7 @@ class JsonSchemaMixin:
         field_schema: JsonDict = {'type': 'object'}
         required = True
         default = None
+        description = None
         if isinstance(field, Field):
             field_type = field.type
             default_value = None
@@ -331,6 +332,8 @@ class JsonSchemaMixin:
             if default_value is not None:
                 default = cls._encode_field(field.type, default_value, omit_none=False)
                 required = False
+            if field.metadata is not None and "description" in field.metadata:
+                description = field.metadata["description"]
         else:
             field_type = field
         field_type_name = cls._get_field_type_name(field_type)
@@ -394,6 +397,8 @@ class JsonSchemaMixin:
 
         if default is not None:
             field_schema['default'] = default
+        if description is not None:
+            field_schema['description'] = description
 
         return field_schema, required
 


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
index e91d313..40e9a33 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -33,8 +33,8 @@ class Weekday(Enum):
 
 @dataclass(eq=True)
 class Point(SubSchemas):
-    x: float
-    y: float
+    x: float = field(metadata=dict(description="Point x coordinate"))
+    y: float = field(metadata=dict(description="Point y coordinate"))
 
     @classmethod
     def field_mapping(cls) -> Dict[str, str]:
diff --git a/tests/test_all.py b/tests/test_all.py
index 3fe1bf3..e6f2752 100644
--- a/tests/test_all.py
+++ b/tests/test_all.py
@@ -76,8 +76,8 @@ POINT_SCHEMA = {
     'description': Point.__doc__,
     'type': 'object',
     'properties': {
-        'z': {'type': 'number'},
-        'y': {'type': 'number'}
+        'z': {'type': 'number', 'description': 'Point x coordinate'},
+        'y': {'type': 'number', 'description': 'Point y coordinate'}
     },
     'required': ['z', 'y']
 }