-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[python] Adds python oneOf/anyOf models + tests #5341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e43db8b
9690af9
231e7b9
9ebaa4a
fb64c40
68e41a3
0bda221
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -828,7 +828,7 @@ def model_to_dict(model_instance, serialize=True): | |
|
|
||
| model_instances = [model_instance] | ||
| if model_instance._composed_schemas() is not None: | ||
| model_instances = model_instance._composed_instances | ||
| model_instances.extend(model_instance._composed_instances) | ||
|
||
| for model_instance in model_instances: | ||
| for attr, value in six.iteritems(model_instance._data_store): | ||
| if serialize: | ||
|
|
@@ -951,12 +951,12 @@ def get_oneof_instance(self, model_args, constant_args): | |
| used to make instances | ||
|
|
||
| Returns | ||
| oneof_instance (instance) | ||
| oneof_instance (instance/None) | ||
| """ | ||
| oneof_instance = None | ||
| if len(self._composed_schemas()['oneOf']) == 0: | ||
| return oneof_instance | ||
| return None | ||
|
|
||
| oneof_instances = [] | ||
| for oneof_class in self._composed_schemas()['oneOf']: | ||
| # transform js keys to python keys in fixed_model_args | ||
| fixed_model_args = change_keys_js_to_python( | ||
|
|
@@ -969,20 +969,30 @@ def get_oneof_instance(self, model_args, constant_args): | |
| if var_name in fixed_model_args: | ||
| kwargs[var_name] = fixed_model_args[var_name] | ||
|
|
||
| # do not try to make a model with no input args | ||
| if len(kwargs) == 0: | ||
| continue | ||
|
||
|
|
||
| # and use it to make the instance | ||
| kwargs.update(constant_args) | ||
| try: | ||
| oneof_instance = oneof_class(**kwargs) | ||
| break | ||
| oneof_instances.append(oneof_instance) | ||
| except Exception: | ||
| pass | ||
| if oneof_instance is None: | ||
| if len(oneof_instances) == 0: | ||
| raise ApiValueError( | ||
| "Invalid inputs given to generate an instance of %s. Unable to " | ||
| "make any instances of the classes in oneOf definition." % | ||
| self.__class__.__name__ | ||
| ) | ||
| return oneof_instance | ||
| elif len(oneof_instances) > 1: | ||
| raise ApiValueError( | ||
| "Invalid inputs given to generate an instance of %s. Multiple " | ||
| "oneOf instances were generated when a max of one is allowed." % | ||
| self.__class__.__name__ | ||
| ) | ||
| return oneof_instances[0] | ||
|
|
||
|
|
||
| def get_anyof_instances(self, model_args, constant_args): | ||
|
|
@@ -1012,6 +1022,10 @@ def get_anyof_instances(self, model_args, constant_args): | |
| if var_name in fixed_model_args: | ||
| kwargs[var_name] = fixed_model_args[var_name] | ||
|
|
||
| # do not try to make a model with no input args | ||
| if len(kwargs) == 0: | ||
| continue | ||
|
||
|
|
||
| # and use it to make the instance | ||
| kwargs.update(constant_args) | ||
| try: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| nulltype | ||
| certifi >= 14.05.14 | ||
| future; python_version<="2.7" | ||
| six >= 1.10 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So required variables have no default value set when we add them to our model
__init__function signatures. That means that they MUST be passed in by the user. If we have these schemas:Then we have propA, propB, and propC all as required variables in the
__init__signature ofcomposedModel. But our user will only pass in one of those properties because they are for oneOf schemas. Our solution here is change propA, propB, and propC to named arguments with default values of nulltype.Null