Repository: Alexei-Kornienko/schematics_to_swagger

Issue:
Hide private model fields in swagger doc

Discussion:

Patch:
diff --git a/schematics_to_swagger/__init__.py b/schematics_to_swagger/__init__.py
index d108f3f..d203de0 100644
--- a/schematics_to_swagger/__init__.py
+++ b/schematics_to_swagger/__init__.py
@@ -54,17 +54,24 @@ def _map_schematics_type(t):
 
 
 def model_to_definition(model):
-    fields = model.fields.items()
+    properties = {}
+    required = []
+
+    for field_name, field in model.fields.items():
+        if field_name.startswith(f'_{model.__name__}'):
+            continue  # Exclude private fields
+        properties[field_name] = _map_schematics_type(field)
+        if getattr(field, 'required'):
+            required.append(field_name)
+
     result_info = {
         'type': 'object',
         'title': model.__name__,
         'description': model.__doc__,
-        'properties': {k: _map_schematics_type(v) for k, v in fields}
+        'properties': properties
     }
-    required = [k for k, v in fields if getattr(v, 'required')]
     if required:
         result_info['required'] = required
-
     return result_info
 
 


Test patch:
diff --git a/tests/models.py b/tests/models.py
index 5392711..7cd4582 100644
--- a/tests/models.py
+++ b/tests/models.py
@@ -16,3 +16,10 @@ class WeatherStats(Model):
     last_report = types.ModelType(WeatherReport)
     prev_reports = types.ListType(types.ModelType(WeatherReport))
     date_list = types.ListType(types.DateTimeType())
+
+
+class WeatherPrivateData(Model):
+    """Some sample model with private field"""
+    city = types.StringType(max_length=50, metadata={'readOnly': True})
+    temperature = types.DecimalType(required=True)
+    __private_information = types.StringType(max_length=50)
diff --git a/tests/test_model.py b/tests/test_model.py
index ddeabe3..1ed6fba 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -53,6 +53,23 @@ WEATHER_STATS_DEF = {
         }
     },
 }
+WEATHER_PRIVATE_DATA = {
+    'title': 'WeatherPrivateData',
+    'type': 'object',
+    'description': 'Some sample model with private field',
+    'properties': {
+        'city': {
+            'type': 'string',
+            'maxLength': 50,
+            'readOnly': True
+        },
+        'temperature': {
+            'type': 'number',
+            'format': 'double'
+        }
+    },
+    'required': ['temperature']
+}
 
 
 def test_model_to_definition():
@@ -64,7 +81,8 @@ def test_model_to_definition():
 def test_read_models_from_module():
     expected = {
         'WeatherReport': WEATHER_REPORT_DEFINITION,
-        'WeatherStats': WEATHER_STATS_DEF
+        'WeatherStats': WEATHER_STATS_DEF,
+        'WeatherPrivateData': WEATHER_PRIVATE_DATA
     }
     data = schematics_to_swagger.read_models_from_module(models)
     assert expected == data
@@ -74,3 +92,9 @@ def test_compound_type():
     expected = WEATHER_STATS_DEF
     data = schematics_to_swagger.model_to_definition(models.WeatherStats)
     assert expected == data
+
+
+def test_private_fields():
+    expected = WEATHER_PRIVATE_DATA
+    definition = schematics_to_swagger.model_to_definition(models.WeatherPrivateData)
+    assert expected == definition