Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…dx-python-lib#290

Signed-off-by: Paul Horton <paul.horton@owasp.org>
  • Loading branch information
madpah committed Jan 27, 2023
1 parent ccd610f commit 2cfc44d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 2 deletions.
3 changes: 2 additions & 1 deletion serializable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ def _from_json(cls: Type[_T], data: Dict[str, Any]) -> object:
new_key = None
if decoded_k not in klass_properties:
for p, pi in klass_properties.items():
if pi.custom_names.get(SerializationType.JSON, None) == k:
# TODO
if pi.custom_names.get(SerializationType.JSON, None) == decoded_k:
new_key = p
else:
new_key = decoded_k
Expand Down
54 changes: 54 additions & 0 deletions tests/fixtures/the-phoenix-project-camel-case-v4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"id": "f3758bf0-0ff7-4366-a5e5-c209d4352b2d",
"title": "The Phoenix Project",
"isbnNumber": "978-1942788294",
"edition": {
"number": 5,
"name": "5th Anniversary Limited Edition"
},
"publishDate": "2018-04-16",
"type": "fiction",
"authors": [
"Kevin Behr",
"Gene Kim",
"George Spafford"
],
"publisher": {
"name": "IT Revolution Press LLC"
},
"chapters": [
{
"number": 1,
"title": "Tuesday, September 2"
},
{
"number": 2,
"title": "Tuesday, September 2"
},
{
"number": 3,
"title": "Tuesday, September 2"
},
{
"number": 4,
"title": "Wednesday, September 3"
}
],
"references": [
{
"reference": "my-ref-3",
"refersTo": [
"sub-ref-2"
]
},
{
"reference": "my-ref-2",
"refersTo": [
"sub-ref-1", "sub-ref-2"
]
},
{
"reference": "my-ref-1"
}
]
}
57 changes: 57 additions & 0 deletions tests/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class SchemaVersion3(ViewType):
pass


class SchemaVersion4(ViewType):
pass


@serializable.serializable_class
class Chapter:

Expand Down Expand Up @@ -131,6 +135,39 @@ def __hash__(self) -> int:
return hash((self.number, self.name))


@serializable.serializable_class
class BookReference:

def __init__(self, *, reference: str, references: Optional[Iterable["BookReference"]] = None) -> None:
self.reference = reference
self.references = list(references or [])

@property # type: ignore[misc]
@serializable.xml_attribute()
def reference(self) -> str:
return self._reference

@reference.setter
def reference(self, reference: str) -> None:
self._reference = reference

@property # type: ignore[misc]
@serializable.json_name('refersTo')
@serializable.xml_array(serializable.XmlArraySerializationType.FLAT, 'reference')
def references(self) -> List["BookReference"]:
return self._references

@references.setter
def references(self, references: Iterable["BookReference"]) -> None:
self._references = list(references)

def __hash__(self) -> int:
return hash((self.reference, tuple(self.references)))

def __repr__(self) -> str:
return f'<BookReference ref={self.reference}, targets={len(self.references)}>'


@serializable.serializable_class(name='bigbook',
ignore_during_deserialization=['something_to_be_ignored', 'ignore_me', 'ignored'])
class Book:
Expand Down Expand Up @@ -203,6 +240,16 @@ def chapters(self, chapters: Iterable[Chapter]) -> None:
def type_(self) -> BookType:
return self._type_

@property # type: ignore[misc]
@serializable.view(SchemaVersion4)
@serializable.xml_sequence(7)
def references(self) -> Set[BookReference]:
return self._references

@references.setter
def references(self, references: Iterable[BookReference]) -> None:
self._references = set(references)


ThePhoenixProject_v1 = Book(
title='The Phoenix Project', isbn='978-1942788294', publish_date=date(year=2018, month=4, day=16),
Expand Down Expand Up @@ -230,4 +277,14 @@ def type_(self) -> BookType:
ThePhoenixProject_v2.chapters.append(Chapter(number=3, title='Tuesday, September 2'))
ThePhoenixProject_v2.chapters.append(Chapter(number=4, title='Wednesday, September 3'))

SubRef1 = BookReference(reference='sub-ref-1')
SubRef2 = BookReference(reference='sub-ref-2')
SubRef3 = BookReference(reference='sub-ref-3')

Ref1 = BookReference(reference='my-ref-1')
Ref2 = BookReference(reference='my-ref-2', references=[SubRef1, SubRef3])
Ref3 = BookReference(reference='my-ref-3', references=[SubRef2])

ThePhoenixProject_v2.references = [Ref3, Ref2, Ref1]

ThePhoenixProject = ThePhoenixProject_v2
7 changes: 6 additions & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
SnakeCasePropertyNameFormatter,
)
from tests.base import FIXTURES_DIRECTORY, BaseTestCase
from tests.model import Book, SchemaVersion2, SchemaVersion3, ThePhoenixProject, ThePhoenixProject_v1
from tests.model import Book, SchemaVersion2, SchemaVersion3, ThePhoenixProject, ThePhoenixProject_v1, SchemaVersion4


class TestJson(BaseTestCase):
Expand All @@ -46,6 +46,11 @@ def test_serialize_tfp_cc_v3(self) -> None:
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-camel-case-v3.json')) as expected_json:
self.assertEqualJson(expected_json.read(), ThePhoenixProject.as_json(view_=SchemaVersion3))

def test_serialize_tfp_cc_v4(self) -> None:
CurrentFormatter.formatter = CamelCasePropertyNameFormatter
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-camel-case-v4.json')) as expected_json:
self.assertEqualJson(expected_json.read(), ThePhoenixProject.as_json(view_=SchemaVersion4))

def test_deserialize_tfp_cc(self) -> None:
CurrentFormatter.formatter = CamelCasePropertyNameFormatter
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-camel-case.json')) as input_json:
Expand Down

0 comments on commit 2cfc44d

Please sign in to comment.