-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(union-types): adds support for OneOf and AnyOf (#41)
This commit adds support for union types that are OneOf and AnyOf types. There are 4 new classes (OneOf, AnyOf, LeafType, UnionTypeContext) that are participating as generic algorithms for validating requests and responses containing union types. You can read more about union types [here](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/). By using those classes, the Python core library now has the handling for union types. This also adds support for the serialization of nested maps and arrays.
- Loading branch information
1 parent
f338272
commit 89b3f40
Showing
44 changed files
with
3,732 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ | |
'utilities', | ||
'factories', | ||
'types', | ||
'logger' | ||
'logger', | ||
'exceptions' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
__all__ = [ | ||
'oneof_validation_exception', | ||
'anyof_validation_exception' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
class AnyOfValidationException(Exception): | ||
def __init__(self, message): | ||
self.message = message | ||
super().__init__(self.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
class OneOfValidationException(Exception): | ||
def __init__(self, message): | ||
self.message = message | ||
super().__init__(self.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
'error_case', | ||
'file_wrapper', | ||
'array_serialization_format', | ||
'xml_attributes' | ||
'xml_attributes', | ||
'union_types' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
__all__ = [ | ||
"any_of", | ||
"one_of", | ||
"union_type_context", | ||
"leaf_type" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from apimatic_core_interfaces.types.union_type import UnionType | ||
from apimatic_core.types.union_types.union_type_context import UnionTypeContext | ||
from apimatic_core.utilities.union_type_helper import UnionTypeHelper | ||
|
||
|
||
class AnyOf(UnionType): | ||
|
||
def __init__(self, union_types, union_type_context: UnionTypeContext = UnionTypeContext()): | ||
super(AnyOf, self).__init__(union_types, union_type_context) | ||
self.collection_cases = None | ||
|
||
def validate(self, value): | ||
context = self._union_type_context | ||
UnionTypeHelper.update_nested_flag_for_union_types(self._union_types) | ||
is_optional_or_nullable = UnionTypeHelper.is_optional_or_nullable_case(context, | ||
[nested_type.get_context() | ||
for nested_type in self._union_types]) | ||
|
||
if value is None and is_optional_or_nullable: | ||
self.is_valid = True | ||
return self | ||
|
||
if value is None: | ||
self.is_valid = False | ||
self.error_messages = UnionTypeHelper.process_errors(value, self._union_types, self.error_messages, | ||
self.get_context().is_nested, False) | ||
return self | ||
|
||
self._validate_value_against_case(value, context) | ||
|
||
if not self.is_valid: | ||
self.error_messages = UnionTypeHelper.process_errors(value, self._union_types, self.error_messages, | ||
self.get_context().is_nested, False) | ||
|
||
return self | ||
|
||
def deserialize(self, value): | ||
if value is None: | ||
return None | ||
|
||
return UnionTypeHelper.deserialize_value(value, self._union_type_context, self.collection_cases, | ||
self._union_types) | ||
|
||
def _validate_value_against_case(self, value, context): | ||
if context.is_array() and context.is_dict() and context.is_array_of_dict(): | ||
self.is_valid, self.collection_cases = UnionTypeHelper.validate_array_of_dict_case(self._union_types, value, | ||
False) | ||
elif context.is_array() and context.is_dict(): | ||
self.is_valid, self.collection_cases = UnionTypeHelper.validate_dict_of_array_case(self._union_types, value, | ||
False) | ||
elif context.is_array(): | ||
self.is_valid, self.collection_cases = UnionTypeHelper.validate_array_case(self._union_types, value, False) | ||
elif context.is_dict(): | ||
self.is_valid, self.collection_cases = UnionTypeHelper.validate_dict_case(self._union_types, value, False) | ||
else: | ||
self.is_valid = UnionTypeHelper.get_matched_count(value, self._union_types, False) >= 1 | ||
|
||
def __deepcopy__(self, memo={}): | ||
copy_object = AnyOf(self._union_types, self._union_type_context) | ||
copy_object.is_valid = self.is_valid | ||
copy_object.collection_cases = self.collection_cases | ||
copy_object.error_messages = self.error_messages | ||
return copy_object |
Oops, something went wrong.