|
1 | 1 | import json
|
2 |
| -from typing import Dict |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Dict, Optional, Set |
3 | 4 |
|
4 | 5 | import pytest
|
| 6 | +from pydantic import BaseModel |
5 | 7 |
|
6 | 8 | from aws_lambda_powertools.event_handler import APIGatewayRestResolver
|
7 | 9 |
|
8 | 10 |
|
| 11 | +@dataclass |
| 12 | +class Person: |
| 13 | + name: str |
| 14 | + birth_date: str |
| 15 | + scores: Set[int] |
| 16 | + |
| 17 | + |
9 | 18 | def test_openapi_duplicated_serialization():
|
10 | 19 | # GIVEN APIGatewayRestResolver is initialized with enable_validation=True
|
11 | 20 | app = APIGatewayRestResolver(enable_validation=True)
|
@@ -61,3 +70,124 @@ def handler():
|
61 | 70 |
|
62 | 71 | # THEN we the custom serializer should be used
|
63 | 72 | assert response["body"] == "hello world"
|
| 73 | + |
| 74 | + |
| 75 | +def test_valid_model_returned_for_optional_type(gw_event): |
| 76 | + # GIVEN an APIGatewayRestResolver with validation enabled |
| 77 | + app = APIGatewayRestResolver(enable_validation=True) |
| 78 | + |
| 79 | + class Model(BaseModel): |
| 80 | + name: str |
| 81 | + age: int |
| 82 | + |
| 83 | + @app.get("/valid_optional") |
| 84 | + def handler_valid_optional() -> Optional[Model]: |
| 85 | + return Model(name="John", age=30) |
| 86 | + |
| 87 | + # WHEN returning a valid model for an Optional type |
| 88 | + gw_event["path"] = "/valid_optional" |
| 89 | + result = app(gw_event, {}) |
| 90 | + |
| 91 | + # THEN it should succeed and return the serialized model |
| 92 | + assert result["statusCode"] == 200 |
| 93 | + assert json.loads(result["body"]) == {"name": "John", "age": 30} |
| 94 | + |
| 95 | + |
| 96 | +def test_serialize_response_without_field(gw_event): |
| 97 | + # GIVEN an APIGatewayRestResolver with validation enabled |
| 98 | + app = APIGatewayRestResolver(enable_validation=True) |
| 99 | + |
| 100 | + # WHEN a handler is defined without return type annotation |
| 101 | + @app.get("/test") |
| 102 | + def handler(): |
| 103 | + return {"message": "Hello, World!"} |
| 104 | + |
| 105 | + gw_event["path"] = "/test" |
| 106 | + |
| 107 | + # THEN the handler should be invoked and return 200 |
| 108 | + # AND the body must be a JSON object |
| 109 | + response = app(gw_event, None) |
| 110 | + assert response["statusCode"] == 200 |
| 111 | + assert response["body"] == '{"message":"Hello, World!"}' |
| 112 | + |
| 113 | + |
| 114 | +def test_serialize_response_list(gw_event): |
| 115 | + """Test serialization of list responses containing complex types""" |
| 116 | + # GIVEN an APIGatewayRestResolver with validation enabled |
| 117 | + app = APIGatewayRestResolver(enable_validation=True) |
| 118 | + |
| 119 | + # WHEN a handler returns a list containing various types |
| 120 | + @app.get("/test") |
| 121 | + def handler(): |
| 122 | + return [{"set": [1, 2, 3]}, {"simple": "value"}] |
| 123 | + |
| 124 | + gw_event["path"] = "/test" |
| 125 | + |
| 126 | + # THEN the response should be properly serialized |
| 127 | + response = app(gw_event, None) |
| 128 | + assert response["statusCode"] == 200 |
| 129 | + assert response["body"] == '[{"set":[1,2,3]},{"simple":"value"}]' |
| 130 | + |
| 131 | + |
| 132 | +def test_serialize_response_nested_dict(gw_event): |
| 133 | + """Test serialization of nested dictionary responses""" |
| 134 | + # GIVEN an APIGatewayRestResolver with validation enabled |
| 135 | + app = APIGatewayRestResolver(enable_validation=True) |
| 136 | + |
| 137 | + # WHEN a handler returns a nested dictionary with complex types |
| 138 | + @app.get("/test") |
| 139 | + def handler(): |
| 140 | + return {"nested": {"date": "2000-01-01", "set": [1, 2, 3]}, "simple": "value"} |
| 141 | + |
| 142 | + gw_event["path"] = "/test" |
| 143 | + |
| 144 | + # THEN the response should be properly serialized |
| 145 | + response = app(gw_event, None) |
| 146 | + assert response["statusCode"] == 200 |
| 147 | + assert response["body"] == '{"nested":{"date":"2000-01-01","set":[1,2,3]},"simple":"value"}' |
| 148 | + |
| 149 | + |
| 150 | +def test_serialize_response_dataclass(gw_event): |
| 151 | + """Test serialization of dataclass responses""" |
| 152 | + # GIVEN an APIGatewayRestResolver with validation enabled |
| 153 | + app = APIGatewayRestResolver(enable_validation=True) |
| 154 | + |
| 155 | + # WHEN a handler returns a dataclass instance |
| 156 | + @app.get("/test") |
| 157 | + def handler(): |
| 158 | + return Person(name="John Doe", birth_date="1990-01-01", scores=[95, 87, 91]) |
| 159 | + |
| 160 | + gw_event["path"] = "/test" |
| 161 | + |
| 162 | + # THEN the response should be properly serialized |
| 163 | + response = app(gw_event, None) |
| 164 | + assert response["statusCode"] == 200 |
| 165 | + assert response["body"] == '{"name":"John Doe","birth_date":"1990-01-01","scores":[95,87,91]}' |
| 166 | + |
| 167 | + |
| 168 | +def test_serialize_response_mixed_types(gw_event): |
| 169 | + """Test serialization of mixed type responses""" |
| 170 | + # GIVEN an APIGatewayRestResolver with validation enabled |
| 171 | + app = APIGatewayRestResolver(enable_validation=True) |
| 172 | + |
| 173 | + # WHEN a handler returns a response with mixed types |
| 174 | + @app.get("/test") |
| 175 | + def handler(): |
| 176 | + person = Person(name="John Doe", birth_date="1990-01-01", scores=[95, 87, 91]) |
| 177 | + return { |
| 178 | + "person": person, |
| 179 | + "records": [{"date": "2000-01-01"}, {"set": [1, 2, 3]}], |
| 180 | + "metadata": {"processed_at": "2050-01-01", "tags": ["tag1", "tag2"]}, |
| 181 | + } |
| 182 | + |
| 183 | + gw_event["path"] = "/test" |
| 184 | + |
| 185 | + # THEN the response should be properly serialized |
| 186 | + response = app(gw_event, None) |
| 187 | + assert response["statusCode"] == 200 |
| 188 | + expected = { |
| 189 | + "person": {"name": "John Doe", "birth_date": "1990-01-01", "scores": [95, 87, 91]}, |
| 190 | + "records": [{"date": "2000-01-01"}, {"set": [1, 2, 3]}], |
| 191 | + "metadata": {"processed_at": "2050-01-01", "tags": ["tag1", "tag2"]}, |
| 192 | + } |
| 193 | + assert json.loads(response["body"]) == expected |
0 commit comments