Skip to content

Commit

Permalink
Fixed issue where defaultValues were set to None instead of being omi…
Browse files Browse the repository at this point in the history
…tted for fields in content types. (#117)
  • Loading branch information
rubydog authored Jul 26, 2024
1 parent aa2b9e4 commit 434bad8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed
* Updated `pyyaml` version to fix a vulnerability.
* Fixed defaultValues omit instead of None for fields in content types.

## v2.14.0
* Adds support for cross-space references
Expand Down
7 changes: 5 additions & 2 deletions contentful_management/content_type_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def __init__(self, field_data):
self.omitted = field_data.get('omitted', False)
self.required = field_data.get('required', False)
self.disabled = field_data.get('disabled', False)
self.default_value = field_data.get('defaultValue', None)
if field_data.get('defaultValue', None) is not None:
self.default_value = field_data.get('defaultValue')
self.validations = [ContentTypeFieldValidation(v)
for v in field_data.get('validations', [])]
self.allowedResources = field_data.get('allowedResources')
Expand All @@ -51,9 +52,11 @@ def to_json(self):
'required': self.required,
'disabled': self.disabled,
'validations': [v.to_json() for v in self.validations],
'defaultValue': self.default_value
}

if hasattr(self, 'default_value'):
result['defaultValue'] = self.default_value

if self.type == 'Array':
result['items'] = self.items

Expand Down
30 changes: 24 additions & 6 deletions tests/content_type_field_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
]
}

FIELD_WITH_DEFAULT_VALUE = {
'name': 'foo',
'id': 'foo',
'type': 'Symbol',
'defaultValue': 'bar'
}

class ContentTypeFieldTest(TestCase):
def test_simple_field_test(self):
field = ContentTypeField(SIMPLE_FIELD)
Expand All @@ -56,7 +63,6 @@ def test_simple_field_test(self):
'required': False,
'disabled': False,
'validations': [],
'defaultValue': None
})

def test_link_field_test(self):
Expand All @@ -73,7 +79,6 @@ def test_link_field_test(self):
'required': False,
'disabled': False,
'validations': [],
'defaultValue': None
})

def test_array_field_test(self):
Expand All @@ -91,8 +96,7 @@ def test_array_field_test(self):
'omitted': False,
'required': False,
'disabled': False,
'validations': [],
'defaultValue': None
'validations': []
})

def test_link_array_field_test(self):
Expand All @@ -112,7 +116,6 @@ def test_link_array_field_test(self):
'required': False,
'disabled': False,
'validations': [],
'defaultValue': None
})

def test_validations_field_test(self):
Expand All @@ -130,10 +133,25 @@ def test_validations_field_test(self):
'validations': [{
'size': {'min': 3}
}],
'defaultValue': None
})

def test_coercion(self):
field = ContentTypeField(SIMPLE_FIELD)

self.assertEqual(field.coerce(123), '123')

def test_default_value_test(self):
field = ContentTypeField(FIELD_WITH_DEFAULT_VALUE)

self.assertEqual(str(field), "<ContentTypeField[foo] id='foo' type='Symbol'>")
self.assertEqual(field.to_json(), {
'id': 'foo',
'name': 'foo',
'type': 'Symbol',
'localized': False,
'omitted': False,
'required': False,
'disabled': False,
'validations': [],
'defaultValue': 'bar'
})
1 change: 0 additions & 1 deletion tests/content_type_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def test_content_type_to_json(self):
'required': False,
'disabled': False,
'validations': [],
'defaultValue': None
}
]
})
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def test_snapshot_to_json(self):
"omitted": False,
"type": "Text",
"validations": [],
"defaultValue": None
},
{
"id": "body",
Expand All @@ -187,7 +186,6 @@ def test_snapshot_to_json(self):
"omitted": False,
"type": "Text",
"validations": [],
"defaultValue": None
}
],
"sys": {
Expand Down

0 comments on commit 434bad8

Please sign in to comment.