Skip to content

Commit

Permalink
Fixed an 'IndexError: list assignment index out of range' error in Ri…
Browse files Browse the repository at this point in the history
…chTextField._coerce_block due to removing items by index from a list from the front causing the indexes to no longer be valid. (#52)
  • Loading branch information
peter-bertuglia authored and dlitvakb committed Oct 11, 2019
1 parent 155ef03 commit 99a574e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contentful/content_type_field_types.py
Expand Up @@ -217,7 +217,7 @@ def _coerce_block(self, value, includes=None, errors=None, resources=None, defau
for node_index, coerced_node in coerced_nodes.items():
value['content'][node_index] = coerced_node

for node_index in invalid_nodes:
for node_index in reversed(invalid_nodes):
del value['content'][node_index]

return value
Expand Down
38 changes: 38 additions & 0 deletions tests/content_type_field_types_test.py
Expand Up @@ -203,3 +203,41 @@ def test_rich_text_field(self):

coerced = rt_field.coerce(document)
self.assertTrue(isinstance(coerced['content'][0]['data']['target'], Link))

# with 2 embedded entries that have errors, both will be removed
# without a list index out of range error
document = {
"nodeType": "document",
"content": [{
"nodeType": "embedded-entry-block",
"nodeClass": "block",
"data": {
"target": {
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "4JJ21pcEI0QSsea20g6K6K"
}
}
}
}, {
"nodeType": "embedded-entry-block",
"nodeClass": "block",
"data": {
"target": {
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "4JJ21pcEI0QSsea20g6K6K"
}
}
}
}]
}

errors = [{"details": {"id": "4JJ21pcEI0QSsea20g6K6K"}}]

self.assertEqual(rt_field.coerce(document, errors=errors), {
"nodeType": "document",
"content": []
})

0 comments on commit 99a574e

Please sign in to comment.