Skip to content

Commit 62ce9c3

Browse files
manuelseegerphanak-sap
authored andcommitted
Fix adding expanded entities to EntityProxy
EntityProxy expected expanded properties to be wrapped in a 'results' object, which isn't to V2 JSON spec. This fix removes this expectation. The original behavior is not removed, so if there happens to be an expanded property wrapped in a 'results' object, EntityProxy continues to work as before. Fixes #258
1 parent ea558f5 commit 62ce9c3

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

pyodata/v2/service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,10 +873,12 @@ def __init__(self, service, entity_set, entity_type, proprties=None, entity_key=
873873
# if there are no entities available, received data consists of
874874
# metadata properties only.
875875
if 'results' in proprties[prop.name]:
876-
877876
# available entities are serialized in results array
878877
for entity in proprties[prop.name]['results']:
879878
self._cache[prop.name].append(EntityProxy(service, None, prop_etype, entity))
879+
else:
880+
for entity in proprties[prop.name]:
881+
self._cache[prop.name].append(EntityProxy(service, None, prop_etype, entity))
880882
else:
881883
raise PyODataException('Unknown multiplicity {0} of association role {1}'
882884
.format(prop.to_role.multiplicity, prop.to_role.name))

tests/test_service_v2.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ def test_get_entity_not_encoded_path(service):
14761476

14771477

14781478
@responses.activate
1479-
def test_get_entity_expanded(service):
1479+
def test_get_entity_expanded_with_results(service):
14801480
"""Check getting entities with expanded navigation properties"""
14811481

14821482
# pylint: disable=redefined-outer-name
@@ -1513,6 +1513,42 @@ def test_get_entity_expanded(service):
15131513
assert emp.Addresses[0].Street == 'Baker Street'
15141514
assert emp.Addresses[0].City == 'London'
15151515

1516+
@responses.activate
1517+
def test_get_entity_expanded(service):
1518+
"""Check getting entities with expanded navigation properties"""
1519+
1520+
# pylint: disable=redefined-outer-name
1521+
path = quote("Employees(23)")
1522+
responses.add(
1523+
responses.GET,
1524+
f"{service.url}/{path}",
1525+
json={'d': {
1526+
'ID': 23,
1527+
'NameFirst': 'Rob',
1528+
'NameLast': 'Ickes',
1529+
'Addresses': [
1530+
{
1531+
'ID': 456,
1532+
'Street': 'Baker Street',
1533+
'City': 'London'
1534+
}
1535+
]
1536+
1537+
}},
1538+
status=200)
1539+
1540+
request = service.entity_sets.Employees.get_entity(23)
1541+
assert isinstance(request, pyodata.v2.service.EntityGetRequest)
1542+
1543+
emp = request.expand('Addresses').execute()
1544+
1545+
assert emp.ID == 23
1546+
assert emp.NameFirst == 'Rob'
1547+
assert emp.NameLast == 'Ickes'
1548+
1549+
assert emp.Addresses[0].ID == 456
1550+
assert emp.Addresses[0].Street == 'Baker Street'
1551+
assert emp.Addresses[0].City == 'London'
15161552

15171553
@responses.activate
15181554
def test_batch_request(service):

0 commit comments

Comments
 (0)