Skip to content

Commit 46772ec

Browse files
committed
service: percent-encode slashes inside entity key values
The problem and fix: urllib.parse.quote() defaults to safe='/', so a slash inside a key value (e.g. SoftwareComponents('/BAZ/FOO')) was left unencoded in the request path. The entity-set name and key string were quoted as one string, making it impossible to encode key-value slashes while preserving the structural slashes that separate navigation segments. Add encode_entity_path(), a parenthesis-depth-aware encoder: text outside the key predicate is quoted with safe='/' (structural separators kept), text inside (...) with safe='' (every reserved char, including /, encoded). Output is byte-identical to the old quote() for keys without slashes, so no existing path changes. Applied to EntityGetRequest, EntityDeleteRequest, EntityModifyRequest and GetEntitySetRequest; batch inherits it via get_path(). encode_path=False still bypasses encoding unchanged. Additional notes: This is outbound-only by design and has no from_literal-style counterpart: encode_entity_path runs solely in get_path() to build request URLs. pyodata never decodes a key back out of a path (the server does the percent-decoding; the __next link is reused verbatim), so there is no reverse direction to keep symmetric. The encode_path=False workaround from PR #243 is intentionally left in place for backward compatibility: callers who pre-encode keys and disable encoding must keep working unchanged, and the flag also serves as a raw-path override for non-conformant servers. Removing it is deferred to a future major release. Fixes #282
1 parent 5d00f1c commit 46772ec

4 files changed

Lines changed: 115 additions & 5 deletions

File tree

‎.github/workflows/python-linters.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
python-version: ["3.10.10"]
19+
python-version: ["3.10"]
2020
lxml-version: ["4.9.1"]
2121

2222
steps:

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- service: entity key values containing `/` are now percent-encoded as `%2F` in the request path, while structural slashes in navigation property paths stay unencoded (#282)
12+
913

1014
## [1.12.2]
1115

‎pyodata/v2/service.py‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,45 @@ def urljoin(*path):
3232
return '/'.join((part.strip('/') for part in path))
3333

3434

35+
def encode_entity_path(path):
36+
"""Percent-encode an OData resource path built from entity-set names and
37+
entity keys.
38+
39+
Structural slashes that separate path segments (e.g. the separator in
40+
``Employees(23)/Addresses``) are preserved, while slashes that appear
41+
inside an entity key value (e.g. ``SoftwareComponents('/BAZ/FOO')``) are
42+
encoded as ``%2F``.
43+
44+
This is achieved by encoding the parts outside of the key parentheses
45+
with ``safe='/'`` (keeping structural separators intact) and the parts
46+
inside the key parentheses with ``safe=''`` (encoding every reserved
47+
character, including slashes).
48+
"""
49+
50+
encoded = []
51+
depth = 0
52+
start = 0
53+
54+
for index, char in enumerate(path):
55+
if char == '(':
56+
if depth == 0:
57+
encoded.append(quote(path[start:index], safe='/'))
58+
start = index
59+
depth += 1
60+
elif char == ')':
61+
if depth > 0:
62+
depth -= 1
63+
if depth == 0:
64+
encoded.append(quote(path[start:index + 1], safe=''))
65+
start = index + 1
66+
67+
# Encode the trailing part that is outside of any key parentheses. If the
68+
# parentheses were unbalanced, the remaining key part keeps full encoding.
69+
encoded.append(quote(path[start:], safe='' if depth > 0 else '/'))
70+
71+
return ''.join(encoded)
72+
73+
3574
def encode_multipart(boundary, http_requests):
3675
"""Encode list of requests into multipart body"""
3776

@@ -412,7 +451,7 @@ def expand(self, expand):
412451

413452
def get_path(self):
414453
if self.get_encode_path():
415-
return quote(self._entity_set_proxy.last_segment + self._entity_key.to_key_string())
454+
return encode_entity_path(self._entity_set_proxy.last_segment + self._entity_key.to_key_string())
416455
return self._entity_set_proxy.last_segment + self._entity_key.to_key_string()
417456

418457
def get_default_headers(self):
@@ -569,7 +608,7 @@ def __init__(self, url, connection, handler, entity_set, entity_key, encode_path
569608

570609
def get_path(self):
571610
if self.get_encode_path():
572-
return quote(self._entity_set.name + self._entity_key.to_key_string())
611+
return encode_entity_path(self._entity_set.name + self._entity_key.to_key_string())
573612
return self._entity_set.name + self._entity_key.to_key_string()
574613

575614
def get_encode_path(self):
@@ -613,7 +652,7 @@ def __init__(self, url, connection, handler, entity_set, entity_key, method="PAT
613652

614653
def get_path(self):
615654
if self.get_encode_path():
616-
return quote(self._entity_set.name + self._entity_key.to_key_string())
655+
return encode_entity_path(self._entity_set.name + self._entity_key.to_key_string())
617656
return self._entity_set.name + self._entity_key.to_key_string()
618657

619658
def get_method(self):
@@ -1367,7 +1406,7 @@ def filter(self, *args, group=False, **kwargs):
13671406

13681407
def get_path(self):
13691408
if self.get_encode_path():
1370-
path = quote(self._last_segment)
1409+
path = encode_entity_path(self._last_segment)
13711410
else:
13721411
path = self._last_segment
13731412

‎tests/test_service_v2.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,73 @@ def test_delete_entity_http_error(service):
839839
assert caught_ex.value.response.status_code == 404
840840

841841

842+
# Issue #282 — slashes inside an entity key value must be percent-encoded as %2F,
843+
# while structural slashes that separate path segments must stay unencoded.
844+
845+
def test_get_entity_path_encodes_slash_in_key(service):
846+
"""GET: a slash in the key value is encoded as %2F (issue #282)"""
847+
848+
# pylint: disable=redefined-outer-name
849+
request = service.entity_sets.Cars.get_entity('/BAZ/FOO')
850+
851+
assert request.get_path() == "Cars%28%27%2FBAZ%2FFOO%27%29"
852+
853+
854+
def test_delete_entity_path_encodes_slash_in_key(service):
855+
"""DELETE: a slash in the key value is encoded as %2F (issue #282)"""
856+
857+
# pylint: disable=redefined-outer-name
858+
request = service.entity_sets.Cars.delete_entity('/BAZ/FOO')
859+
860+
assert request.get_path() == "Cars%28%27%2FBAZ%2FFOO%27%29"
861+
862+
863+
def test_update_entity_path_encodes_slash_in_key(service):
864+
"""PATCH/MODIFY: a slash in the key value is encoded as %2F (issue #282)"""
865+
866+
# pylint: disable=redefined-outer-name
867+
request = service.entity_sets.Cars.update_entity('/BAZ/FOO')
868+
869+
assert request.get_path() == "Cars%28%27%2FBAZ%2FFOO%27%29"
870+
871+
872+
def test_get_entities_path_encodes_slash_in_parent_key(service):
873+
"""GET entity set: a slash in a parent key value is encoded as %2F (issue #282)"""
874+
875+
# pylint: disable=redefined-outer-name
876+
request = service.entity_sets.Customers.get_entity('/BAZ/FOO').nav('Orders').get_entities()
877+
878+
assert isinstance(request, pyodata.v2.service.GetEntitySetRequest)
879+
assert request.get_path() == "Customers%28%27%2FBAZ%2FFOO%27%29/Orders"
880+
881+
882+
def test_entity_path_keeps_structural_slash(service):
883+
"""Navigation property separator stays an unencoded slash (issue #282)"""
884+
885+
# pylint: disable=redefined-outer-name
886+
request = service.entity_sets.Cars.get_entity('/BAZ/FOO').nav('IDPic')
887+
888+
assert request.get_path() == "Cars%28%27%2FBAZ%2FFOO%27%29/IDPic"
889+
890+
891+
def test_entity_path_encode_path_false_bypasses_encoding(service):
892+
"""encode_path=False still produces a raw, unencoded path (regression guard)"""
893+
894+
# pylint: disable=redefined-outer-name
895+
request = service.entity_sets.Cars.get_entity('/BAZ/FOO', encode_path=False)
896+
897+
assert request.get_path() == "Cars('/BAZ/FOO')"
898+
899+
900+
def test_entity_path_encodes_other_special_characters_in_key(service):
901+
"""Other reserved characters in the key value are still encoded (issue #282)"""
902+
903+
# pylint: disable=redefined-outer-name
904+
request = service.entity_sets.Cars.get_entity('a/b?c&d')
905+
906+
assert request.get_path() == "Cars%28%27a%2Fb%3Fc%26d%27%29"
907+
908+
842909
def test_update_entity_with_entity_key(service):
843910
"""Make sure the method update_entity handles correctly the parameter key which is EntityKey"""
844911

0 commit comments

Comments
 (0)