Skip to content

Commit

Permalink
fix(entry): return None if the nested link is unresolvable (#59)
Browse files Browse the repository at this point in the history
* fix(entry): return None if the nested link is unresolvable

* Add Changelog and update CI config

Co-authored-by: David Litvak <david.litvakb@gmail.com>
  • Loading branch information
Spring3 and dlitvakb committed Jul 16, 2020
1 parent 83c4d52 commit 261f675
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -2,10 +2,10 @@ sudo: false
language: python
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "pypy"
- "3.7"
- "3.8"
- "pypy3"
install:
- pip install -r requirements.txt
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,8 @@
# CHANGELOG

## Unreleased
### Fixed
* Unresolved single references fields will no longer return the invalid Link and return None instead, matching behaviour of multiple references fields, where unresolvable links are ignored. [#58](https://github.com/contentful/contentful.py/issues/58)

## v1.12.3
### Fixed
Expand Down
4 changes: 3 additions & 1 deletion contentful/entry.py
Expand Up @@ -22,7 +22,9 @@ class Entry(FieldsResource):
"""

def _coerce(self, field_id, value, localized, includes, errors, resources=None):
if is_link(value) and not unresolvable(value, errors):
if is_link(value):
if unresolvable(value, errors):
return None
return self._build_nested_resource(
value,
localized,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,4 +1,4 @@
python-dateutil==2.6.0
python-dateutil==2.8.1
requests>=2.20.0,<3.0

vcrpy==1.10.3
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -95,6 +95,7 @@ def get_email(package):
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],
test_suite='tests',
tests_require=test_requirements
Expand Down
2 changes: 1 addition & 1 deletion tests/client_test.py
Expand Up @@ -198,7 +198,7 @@ def test_client_sync_with_environments(self):
client = Client('a22o2qgm356c', 'bfbc63cf745a037125dbcc64f716a9a0e9d091df1a79e84920b890f87a6e7ab9', environment='staging', content_type_cache=False)
sync = client.sync({'initial': True})

self.assertEquals(sync.items[0].environment.id, 'staging')
self.assertEqual(sync.items[0].environment.id, 'staging')

@vcr.use_cassette('fixtures/client/array_endpoints.yaml')
def test_client_creates_wrapped_arrays(self):
Expand Down
58 changes: 58 additions & 0 deletions tests/entry_test.py
Expand Up @@ -42,6 +42,64 @@ def test_entry(self):
self.assertEqual(entry.name, 'foobar')
self.assertEqual(entry.date, '2016-06-06')

def test_entry_unresolved_link(self):
ContentTypeCache.__CACHE__ = []

errors = [
{
"sys": {
"id": "notResolvable",
"type": "error"
},
"details": {
"type": "Link",
"linkType": "Entry",
"id": "unresolvedLinkId"
}
}
]

entry = Entry(
{
'sys': {
'space': {
'sys': {
'type': 'Link',
'linkType': 'Space',
'id': 'foo'
}
},
'contentType': {
'sys': {
'type': 'Link',
'linkType': 'ContentType',
'id': 'foo'
}
},
'type': 'Entry',
'createdAt': '2016-06-06',
'updatedAt': '2016-06-06',
'deletedAt': '2016-06-06',
'id': 'foobar',
'version': 1
},
'fields': {
'name': 'foobar',
'entryLink': {
'sys': {
'type': 'Link',
'linkType': 'Entry',
'id': 'unresolvedLinkId'
}
}
}
},
None,
errors
)

self.assertEqual(entry.fields().get('entryLink'), None)

def test_cached_content_type_entry(self):
ContentTypeCache.__CACHE__ = []

Expand Down

0 comments on commit 261f675

Please sign in to comment.