Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions domaintools/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ def wrapper(self, *args, **kwargs):

if "domains" in arguments.keys():
domains = arguments.pop("domains")
arguments["domain"] = (
",".join(domains) if isinstance(domains, (list, tuple)) else domains
)
arguments["domain"] = ",".join(domains) if isinstance(domains, (list, tuple)) else domains

if spec:
# Determine which HTTP method is currently being executed.
# If the function allows dynamic methods (e.g. method="POST"), use that.
# Otherwise, default to the first method defined in the decorator.
current_method = kwargs.get("method", normalized_methods[0])

filtered_arguments = {k: v for k, v in arguments.items() if v is not None}

# Run Validation
# This will raise a ValueError and stop execution if validation fails.
try:
RequestValidator.validate(
spec=spec,
path=path,
method=current_method,
parameters=arguments,
parameters=filtered_arguments,
)
except ValueError as e:
print(f"[Validation Error] {e}")
Expand Down
32 changes: 32 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,35 @@ def create_user(name=None, body=None):
call_kwargs = mock_validate.call_args[1]

assert call_kwargs.get("parameters") == {"name": "test-name"}

def test_none_arguments_are_filtered_before_validation(self, mock_client):
"""
Test that parameters with None values are excluded from the arguments
passed to the validator.
"""

@api_endpoint(spec_name="v1", path="/users", methods="POST")
def create_user(name=None, status=None):
return "Success"

with patch("domaintools.request_validator.RequestValidator.validate") as mock_validate:
create_user(mock_client, name="Alice", status=None)

call_kwargs = mock_validate.call_args[1]
assert call_kwargs.get("parameters") == {"name": "Alice"}
assert "status" not in call_kwargs.get("parameters", {})

def test_all_none_arguments_passes_empty_parameters(self, mock_client):
"""
Test that when all arguments are None, an empty dict is passed to the validator.
"""

@api_endpoint(spec_name="v1", path="/users", methods="POST")
def create_user(name=None, body=None):
return "Success"

with patch("domaintools.request_validator.RequestValidator.validate") as mock_validate:
create_user(mock_client, name=None, body=None)

call_kwargs = mock_validate.call_args[1]
assert call_kwargs.get("parameters") == {}
Loading