Skip to content

Commit 0c35e5a

Browse files
authored
fix(apigateway): remove indentation in debug_mode (#987)
1 parent ffa4a35 commit 0c35e5a

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

Diff for: aws_lambda_powertools/event_handler/api_gateway.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,6 @@ def __init__(
446446
# Allow for a custom serializer or a concise json serialization
447447
self._serializer = serializer or partial(json.dumps, separators=(",", ":"), cls=Encoder)
448448

449-
if self._debug:
450-
# Always does a pretty print when in debug mode
451-
self._serializer = partial(json.dumps, indent=4, cls=Encoder)
452-
453449
def route(
454450
self,
455451
rule: str,
@@ -496,7 +492,7 @@ def resolve(self, event, context) -> Dict[str, Any]:
496492
Returns the dict response
497493
"""
498494
if self._debug:
499-
print(self._json_dump(event))
495+
print(self._json_dump(event), end="")
500496
BaseRouter.current_event = self._to_proxy_event(event)
501497
BaseRouter.lambda_context = context
502498
return self._resolve().build(self.current_event, self._cors)

Diff for: docs/core/event_handler/api_gateway.md

+2
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,8 @@ This will enable full tracebacks errors in the response, print request and respo
940940
???+ danger
941941
This might reveal sensitive information in your logs and relax CORS restrictions, use it sparingly.
942942

943+
It's best to use for local development only!
944+
943945
```python hl_lines="3" title="Enabling debug mode"
944946
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
945947

Diff for: tests/functional/event_handler/test_api_gateway.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
from tests.functional.utils import load_event
3333

3434

35+
@pytest.fixture
36+
def json_dump():
37+
# our serializers reduce length to save on costs; fixture to replicate separators
38+
return lambda obj: json.dumps(obj, separators=(",", ":"))
39+
40+
3541
def read_media(file_name: str) -> bytes:
3642
path = Path(str(Path(__file__).parent.parent.parent.parent) + "/docs/media/" + file_name)
3743
return path.read_bytes()
@@ -506,13 +512,10 @@ def custom_method():
506512
assert headers["Access-Control-Allow-Methods"] == "CUSTOM"
507513

508514

509-
def test_service_error_responses():
515+
def test_service_error_responses(json_dump):
510516
# SCENARIO handling different kind of service errors being raised
511517
app = ApiGatewayResolver(cors=CORSConfig())
512518

513-
def json_dump(obj):
514-
return json.dumps(obj, separators=(",", ":"))
515-
516519
# GIVEN an BadRequestError
517520
@app.get(rule="/bad-request-error", cors=False)
518521
def bad_request_error():
@@ -641,7 +644,7 @@ def test_debug_mode_environment_variable(monkeypatch):
641644
assert app._debug
642645

643646

644-
def test_debug_json_formatting():
647+
def test_debug_json_formatting(json_dump):
645648
# GIVEN debug is True
646649
app = ApiGatewayResolver(debug=True)
647650
response = {"message": "Foo"}
@@ -654,7 +657,7 @@ def foo():
654657
result = app({"path": "/foo", "httpMethod": "GET"}, None)
655658

656659
# THEN return a pretty print json in the body
657-
assert result["body"] == json.dumps(response, indent=4)
660+
assert result["body"] == json_dump(response)
658661

659662

660663
def test_debug_print_event(capsys):

0 commit comments

Comments
 (0)