diff --git a/CHANGELOG.md b/CHANGELOG.md index 9afc9577..adb89edf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Release notes Unreleased ------------------------ * Fix undeclared variables, found via linting. +* Allow subclassing Builder to always initialize optional arrays with empty lists version 1.0.0, 1.0.0-beta.1, 1.0.0-beta.2 (2021-09-13) ------------------------ diff --git a/README.md b/README.md index 4e9749ae..762ab27a 100644 --- a/README.md +++ b/README.md @@ -418,6 +418,25 @@ within the WSDL import each other. * username:: username for HTTP authentication. * password:: password for HTTP authentication. +### Custom behavior through swapping components + +Various ways that suds behaves can be customized by swapping out custom + +#### Initializing optional arrays with lists + +In some unreleased versions of suds-jurko, all children elements were populated with empty lists. This was fixed in suds-community as a regression. This can be desired behavior as it simplifies constructing complex requests. To obtain the prior behavior, use a custom `Builder` class, for example: + +``` +from suds.client import Client, Builder + +class AlwaysInitializeBuilder(Builder): + def skip_value(self, type): + return False # always set value + +client = Client(url) +client.factory.builder = AlwaysInitializeBuilder(client.factory.builder.resolver) +``` + ## Enumerations Enumerations are handled as follows: diff --git a/suds/builder.py b/suds/builder.py index a3b0ef3c..e4eadc57 100644 --- a/suds/builder.py +++ b/suds/builder.py @@ -84,7 +84,7 @@ def process(self, data, type, history): md.sxtype = resolved md.ordering = self.ordering(resolved) - setattr(data, type.name, value if not type.optional() or type.multi_occurrence() else None) + setattr(data, type.name, None if self.skip_value(type) else value) if value is not None: data = value if not isinstance(data, list): @@ -109,6 +109,10 @@ def skip_child(self, child, ancestry): return True return False + def skip_value(self, type): + """ whether or not to skip setting the value """ + return type.optional() and not type.multi_occurrence() + def ordering(self, type): """ get the ordering """ result = []