Skip to content

Commit

Permalink
Allow subclasses of Builder to determine if children are initialized.
Browse files Browse the repository at this point in the history
In some unreleased versions of suds-jurko, all elements that were populated with empty lists. This was fixed in suds-commnunity as a regression, see #14, #15, and #16.

This changes allows consumers of suds to have the suds-jurko behavior by doing the following:
```
from suds.client import Client, Builder

class MyBuilder(Builder):
    def skip_value(self, type):
        return False # always set value

client = Client('...')
client.factory.builder = MyBuilder(client.factory.builder.resolver)
```
  • Loading branch information
phillbaker committed Jan 29, 2022
1 parent b33964b commit 366f7f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)
------------------------
Expand Down
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion suds/builder.py
Expand Up @@ -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):
Expand All @@ -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 = []
Expand Down

0 comments on commit 366f7f1

Please sign in to comment.