Skip to content

Commit

Permalink
Fix Edm.Binary literal representation
Browse files Browse the repository at this point in the history
While Edm.Binary values are Base64 encoded in JSON payload [1][1], the
specification demands a prefixed and quoted Base16 encoding for values in URIs
and HTTP headers [2][2].

As there has been no specific conversion for Edm.Binary so far and we want to
stay backward compatible, the Python side representation shall remain a string
with the Base64 encoded payload.

However there are cases, where the literal is generated from the payload and
sent to the server, that expects it in the correct format.  E.g. this happens
when building the resource path while using a Edm.Binary property as an entity
key (see #187 and phanak-sap/pyodata-issue-files#2).

Another case might be when using the $include filter for a Edm.Binary property.

To meet those requirements, we want to have these representations:

 JSON   | Python       | Literal
--------|--------------|------------------------
 Base64 | str (Base64) | prefixed quoted Base16

This adds a specific type for Edm.Binary with the necessary conversions.

Further it extends the test cases to cover those conversions.

As the Edm.Binary type has been used to test the generic prefixed conversions,
we need to switch this to the Edm.Byte type, that remains generic.

[1](https://www.odata.org/documentation/odata-version-2-0/json-format)
[2](https://www.odata.org/documentation/odata-version-2-0/overview/)
  • Loading branch information
inxonic authored and filak-sap committed Jan 11, 2022
1 parent 7d65f74 commit 3208e24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
15 changes: 14 additions & 1 deletion pyodata/v2/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
# pylint: disable=missing-docstring,too-many-instance-attributes,too-many-arguments,protected-access,no-member,line-too-long,logging-format-interpolation,too-few-public-methods,too-many-lines, too-many-public-methods

import base64
import collections
import datetime
from enum import Enum, auto
Expand Down Expand Up @@ -194,7 +195,7 @@ def _build_types():
Types.Types = {}

Types.register_type(Typ('Null', 'null'))
Types.register_type(Typ('Edm.Binary', 'binary\'\''))
Types.register_type(Typ('Edm.Binary', 'binary\'\'', EdmBinaryTypTraits('(?:binary|X)')))
Types.register_type(Typ('Edm.Boolean', 'false', EdmBooleanTypTraits()))
Types.register_type(Typ('Edm.Byte', '0'))
Types.register_type(Typ('Edm.DateTime', 'datetime\'2000-01-01T00:00\'', EdmDateTimeTypTraits()))
Expand Down Expand Up @@ -361,6 +362,18 @@ def from_literal(self, value):
return matches.group(1)


class EdmBinaryTypTraits(EdmPrefixedTypTraits):
"""Edm.Binary traits"""

def to_literal(self, value):
binary = base64.b64decode(value, validate=True)
return f"binary'{base64.b16encode(binary).decode()}'"

def from_literal(self, value):
binary = base64.b16decode(super().from_literal(value), casefold=True)
return base64.b64encode(binary).decode()


class EdmDateTimeTypTraits(EdmPrefixedTypTraits):
"""Emd.DateTime traits
Expand Down
17 changes: 13 additions & 4 deletions tests/test_model_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,19 @@ def test_traits():
"""Test individual traits"""

# generic
typ = Types.from_name('Edm.Binary')
typ = Types.from_name('Edm.Byte')
assert repr(typ.traits) == 'TypTraits'
assert typ.traits.to_literal('bincontent') == 'bincontent'
assert typ.traits.from_literal('some bin content') == 'some bin content'
assert typ.traits.to_literal('85') == '85'
assert typ.traits.from_literal('170') == '170'

# binary
typ = Types.from_name('Edm.Binary')
assert repr(typ.traits) == 'EdmBinaryTypTraits'
assert typ.traits.to_literal('wAHK/rqt8A0=') == 'binary\'C001CAFEBAADF00D\''
assert typ.traits.from_literal('binary\'C001cafeBAADF00D\'') == 'wAHK/rqt8A0='
assert typ.traits.from_literal('X\'C001cafeBAADF00D\'') == 'wAHK/rqt8A0='
assert typ.traits.to_json('cHlvZGF0YQ==') == 'cHlvZGF0YQ=='
assert typ.traits.from_json('cHlvZGF0YQ==') == 'cHlvZGF0YQ=='

# string
typ = Types.from_name('Edm.String')
Expand Down Expand Up @@ -1462,4 +1471,4 @@ def test_invalid_xml(xml_builder_factory):

with pytest.raises(PyODataParserError) as e_info:
MetadataBuilder(xml).build()
assert str(e_info.value) == 'Metadata document syntax error'
assert str(e_info.value) == 'Metadata document syntax error'

0 comments on commit 3208e24

Please sign in to comment.