Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apigateway): remove indentation in debug_mode #987

Merged
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
6 changes: 1 addition & 5 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,6 @@ def __init__(
# Allow for a custom serializer or a concise json serialization
self._serializer = serializer or partial(json.dumps, separators=(",", ":"), cls=Encoder)

if self._debug:
# Always does a pretty print when in debug mode
self._serializer = partial(json.dumps, indent=4, cls=Encoder)

def route(
self,
rule: str,
Expand Down Expand Up @@ -496,7 +492,7 @@ def resolve(self, event, context) -> Dict[str, Any]:
Returns the dict response
"""
if self._debug:
print(self._json_dump(event))
print(self._json_dump(event), end="")
BaseRouter.current_event = self._to_proxy_event(event)
BaseRouter.lambda_context = context
return self._resolve().build(self.current_event, self._cors)
Expand Down
2 changes: 2 additions & 0 deletions docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ This will enable full tracebacks errors in the response, print request and respo
???+ danger
This might reveal sensitive information in your logs and relax CORS restrictions, use it sparingly.

It's best to use for local development only!

```python hl_lines="3" title="Enabling debug mode"
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver

Expand Down
15 changes: 9 additions & 6 deletions tests/functional/event_handler/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
from tests.functional.utils import load_event


@pytest.fixture
def json_dump():
# our serializers reduce length to save on costs; fixture to replicate separators
return lambda obj: json.dumps(obj, separators=(",", ":"))


def read_media(file_name: str) -> bytes:
path = Path(str(Path(__file__).parent.parent.parent.parent) + "/docs/media/" + file_name)
return path.read_bytes()
Expand Down Expand Up @@ -506,13 +512,10 @@ def custom_method():
assert headers["Access-Control-Allow-Methods"] == "CUSTOM"


def test_service_error_responses():
def test_service_error_responses(json_dump):
# SCENARIO handling different kind of service errors being raised
app = ApiGatewayResolver(cors=CORSConfig())

def json_dump(obj):
return json.dumps(obj, separators=(",", ":"))

# GIVEN an BadRequestError
@app.get(rule="/bad-request-error", cors=False)
def bad_request_error():
Expand Down Expand Up @@ -641,7 +644,7 @@ def test_debug_mode_environment_variable(monkeypatch):
assert app._debug


def test_debug_json_formatting():
def test_debug_json_formatting(json_dump):
# GIVEN debug is True
app = ApiGatewayResolver(debug=True)
response = {"message": "Foo"}
Expand All @@ -654,7 +657,7 @@ def foo():
result = app({"path": "/foo", "httpMethod": "GET"}, None)

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


def test_debug_print_event(capsys):
Expand Down