From 8416fff568100ab3a7300a775ee88fc86d8ea1e7 Mon Sep 17 00:00:00 2001 From: Maksym Melnychok Date: Fri, 9 Oct 2020 19:38:15 +0200 Subject: [PATCH] [python-experimental] implement `in` operator for model classes (#7637) * implement operator for model classes * refresh samples --- .../methods_setattr_getattr_composed.mustache | 18 +++++++++- .../methods_setattr_getattr_normal.mustache | 9 ++++- .../petstore_api/model_utils.py | 33 +++++++++++++++++++ .../x_auth_id_alias/model_utils.py | 33 +++++++++++++++++++ .../dynamic_servers/model_utils.py | 33 +++++++++++++++++++ .../petstore_api/model_utils.py | 33 +++++++++++++++++++ 6 files changed, 157 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/model_templates/methods_setattr_getattr_composed.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/model_templates/methods_setattr_getattr_composed.mustache index dd118d30f91f..1a01fc472cd7 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/model_templates/methods_setattr_getattr_composed.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/model_templates/methods_setattr_getattr_composed.mustache @@ -68,4 +68,20 @@ "at self and self's composed instances. All values must be " "the same".format(name, type(self).__name__), path_to_item - ) \ No newline at end of file + ) + + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + + if name in self.required_properties: + return name in self.__dict__ + + model_instances = self._var_name_to_model_instances.get( + name, self._additional_properties_model_instances) + + if model_instances: + for model_instance in model_instances: + if name in model_instance._data_store: + return True + + return False diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/model_templates/methods_setattr_getattr_normal.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/model_templates/methods_setattr_getattr_normal.mustache index 9fc634490893..32566dc684e6 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/model_templates/methods_setattr_getattr_normal.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/model_templates/methods_setattr_getattr_normal.mustache @@ -22,4 +22,11 @@ "{0} has no attribute '{1}'".format( type(self).__name__, name), [name] - ) \ No newline at end of file + ) + + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + if name in self.required_properties: + return name in self.__dict__ + + return name in self.__dict__['_data_store'] diff --git a/samples/client/petstore/python-experimental/petstore_api/model_utils.py b/samples/client/petstore/python-experimental/petstore_api/model_utils.py index de9fc5c3a272..88a7e8ba38b9 100644 --- a/samples/client/petstore/python-experimental/petstore_api/model_utils.py +++ b/samples/client/petstore/python-experimental/petstore_api/model_utils.py @@ -316,6 +316,14 @@ class ModelSimple(OpenApiModel): [name] ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + if name in self.required_properties: + return name in self.__dict__ + + return name in self.__dict__['_data_store'] + + def to_str(self): """Returns the string representation of the model""" return str(self.value) @@ -364,6 +372,14 @@ class ModelNormal(OpenApiModel): [name] ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + if name in self.required_properties: + return name in self.__dict__ + + return name in self.__dict__['_data_store'] + + def to_dict(self): """Returns the model properties as a dict""" return model_to_dict(self, serialize=False) @@ -488,6 +504,23 @@ class ModelComposed(OpenApiModel): path_to_item ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + + if name in self.required_properties: + return name in self.__dict__ + + model_instances = self._var_name_to_model_instances.get( + name, self._additional_properties_model_instances) + + if model_instances: + for model_instance in model_instances: + if name in model_instance._data_store: + return True + + return False + + def to_dict(self): """Returns the model properties as a dict""" return model_to_dict(self, serialize=False) diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/model_utils.py b/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/model_utils.py index 6be24a8fc54a..efdb666e7a79 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/model_utils.py +++ b/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/model_utils.py @@ -316,6 +316,14 @@ class ModelSimple(OpenApiModel): [name] ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + if name in self.required_properties: + return name in self.__dict__ + + return name in self.__dict__['_data_store'] + + def to_str(self): """Returns the string representation of the model""" return str(self.value) @@ -364,6 +372,14 @@ class ModelNormal(OpenApiModel): [name] ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + if name in self.required_properties: + return name in self.__dict__ + + return name in self.__dict__['_data_store'] + + def to_dict(self): """Returns the model properties as a dict""" return model_to_dict(self, serialize=False) @@ -488,6 +504,23 @@ class ModelComposed(OpenApiModel): path_to_item ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + + if name in self.required_properties: + return name in self.__dict__ + + model_instances = self._var_name_to_model_instances.get( + name, self._additional_properties_model_instances) + + if model_instances: + for model_instance in model_instances: + if name in model_instance._data_store: + return True + + return False + + def to_dict(self): """Returns the model properties as a dict""" return model_to_dict(self, serialize=False) diff --git a/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/model_utils.py b/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/model_utils.py index 685b622e6f0a..a50552534179 100644 --- a/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/model_utils.py +++ b/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/model_utils.py @@ -316,6 +316,14 @@ class ModelSimple(OpenApiModel): [name] ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + if name in self.required_properties: + return name in self.__dict__ + + return name in self.__dict__['_data_store'] + + def to_str(self): """Returns the string representation of the model""" return str(self.value) @@ -364,6 +372,14 @@ class ModelNormal(OpenApiModel): [name] ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + if name in self.required_properties: + return name in self.__dict__ + + return name in self.__dict__['_data_store'] + + def to_dict(self): """Returns the model properties as a dict""" return model_to_dict(self, serialize=False) @@ -488,6 +504,23 @@ class ModelComposed(OpenApiModel): path_to_item ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + + if name in self.required_properties: + return name in self.__dict__ + + model_instances = self._var_name_to_model_instances.get( + name, self._additional_properties_model_instances) + + if model_instances: + for model_instance in model_instances: + if name in model_instance._data_store: + return True + + return False + + def to_dict(self): """Returns the model properties as a dict""" return model_to_dict(self, serialize=False) diff --git a/samples/openapi3/client/petstore/python-experimental/petstore_api/model_utils.py b/samples/openapi3/client/petstore/python-experimental/petstore_api/model_utils.py index de9fc5c3a272..88a7e8ba38b9 100644 --- a/samples/openapi3/client/petstore/python-experimental/petstore_api/model_utils.py +++ b/samples/openapi3/client/petstore/python-experimental/petstore_api/model_utils.py @@ -316,6 +316,14 @@ class ModelSimple(OpenApiModel): [name] ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + if name in self.required_properties: + return name in self.__dict__ + + return name in self.__dict__['_data_store'] + + def to_str(self): """Returns the string representation of the model""" return str(self.value) @@ -364,6 +372,14 @@ class ModelNormal(OpenApiModel): [name] ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + if name in self.required_properties: + return name in self.__dict__ + + return name in self.__dict__['_data_store'] + + def to_dict(self): """Returns the model properties as a dict""" return model_to_dict(self, serialize=False) @@ -488,6 +504,23 @@ class ModelComposed(OpenApiModel): path_to_item ) + def __contains__(self, name): + """this allows us to use `in` operator: `'attr' in instance`""" + + if name in self.required_properties: + return name in self.__dict__ + + model_instances = self._var_name_to_model_instances.get( + name, self._additional_properties_model_instances) + + if model_instances: + for model_instance in model_instances: + if name in model_instance._data_store: + return True + + return False + + def to_dict(self): """Returns the model properties as a dict""" return model_to_dict(self, serialize=False)