@@ -2677,12 +2677,16 @@ def query_model_advanced(params: Annotated[QueryAdvanced, Query()]) -> Dict[str,
26772677def test_parse_form_data_exception (monkeypatch ):
26782678 """Test that _parse_form_data raises RequestValidationError on exception"""
26792679 import pytest
2680+
26802681 from aws_lambda_powertools .event_handler .middlewares .openapi_validation import OpenAPIRequestValidationMiddleware
26812682 from aws_lambda_powertools .event_handler .openapi .exceptions import RequestValidationError
2683+
26822684 class DummyEvent :
26832685 decoded_body = None
2686+
26842687 class DummyApp :
26852688 current_event = DummyEvent ()
2689+
26862690 # Correct monkeypatch: replace parse_qs with a function that raises Exception
26872691 def _raise (* a , ** kw ):
26882692 raise ValueError ("fail" )
@@ -2700,8 +2704,9 @@ def _raise(*a, **kw):
27002704
27012705def test_get_body_field_location_alias_omitted ():
27022706 """Test _get_body_field_location with field_alias_omitted True and False"""
2703- from aws_lambda_powertools .event_handler .middlewares .openapi_validation import _get_body_field_location
27042707 from typing import cast
2708+
2709+ from aws_lambda_powertools .event_handler .middlewares .openapi_validation import _get_body_field_location
27052710 from aws_lambda_powertools .event_handler .openapi .compat import ModelField
27062711
27072712 class DummyField :
@@ -2721,14 +2726,19 @@ def test_decode_request_body_fallback_on_invalid_base64():
27212726 current_event = SimpleNamespace (body = "not-base64!!" , is_base64_encoded = True )
27222727 app = SimpleNamespace (current_event = current_event )
27232728
2724- result = __import__ ("aws_lambda_powertools.event_handler.middlewares.openapi_validation" , fromlist = ["*" ]).OpenAPIRequestValidationMiddleware ()._decode_request_body (app )
2729+ result = (
2730+ __import__ ("aws_lambda_powertools.event_handler.middlewares.openapi_validation" , fromlist = ["*" ])
2731+ .OpenAPIRequestValidationMiddleware ()
2732+ ._decode_request_body (app )
2733+ )
27252734 assert isinstance (result , (bytes , bytearray ))
27262735 assert result == b"not-base64!!"
27272736
27282737
27292738def test_normalize_field_value_uploadfile_with_annotated_bytes ():
27302739 """If a field_info.annotation is Annotated[bytes,...], UploadFile should be converted to bytes."""
27312740 from typing import Annotated
2741+
27322742 from aws_lambda_powertools .event_handler .openapi .params import UploadFile
27332743
27342744 file_content = b"hello"
@@ -2767,22 +2777,26 @@ def test_get_embed_body_wraps_when_alias_omitted():
27672777 received_body = {"a" : 1 }
27682778
27692779 ov = __import__ ("aws_lambda_powertools.event_handler.middlewares.openapi_validation" , fromlist = ["*" ])
2770- new_body , alias_omitted = ov ._get_embed_body (field = field , required_params = required_params , received_body = received_body )
2780+ new_body , alias_omitted = ov ._get_embed_body (
2781+ field = field ,
2782+ required_params = required_params ,
2783+ received_body = received_body ,
2784+ )
27712785 assert alias_omitted is True
27722786 assert new_body == {"my_field" : received_body }
27732787
27742788
27752789def test_split_section_headers_and_content_handles_crlf_and_lf ():
27762790 # CRLF version
2777- section_crlf = b" Content-Disposition: form-data; name=\" f \ "\r \n Content-Type: text/plain\r \n \r \n hello"
2791+ section_crlf = b' Content-Disposition: form-data; name="f "\r \n Content-Type: text/plain\r \n \r \n hello'
27782792 ov = __import__ ("aws_lambda_powertools.event_handler.middlewares.openapi_validation" , fromlist = ["*" ])
27792793 middleware = ov .OpenAPIRequestValidationMiddleware ()
27802794 headers_part , content = middleware ._split_section_headers_and_content (section_crlf )
27812795 assert "Content-Disposition" in headers_part
27822796 assert content == b"hello"
27832797
27842798 # LF version
2785- section_lf = b" Content-Disposition: form-data; name=\" f \ "\n \n world"
2799+ section_lf = b' Content-Disposition: form-data; name="f "\n \n world'
27862800 headers_part2 , content2 = middleware ._split_section_headers_and_content (section_lf )
27872801 assert "Content-Disposition" in headers_part2
27882802 assert content2 == b"world"
@@ -2794,16 +2808,14 @@ def test_parse_multipart_sections_creates_uploadfile_and_field():
27942808 boundary = b"--boundary"
27952809 # Build sections: empty preamble, a file part, a field part, and closing
27962810 file_section = (
2797- b"\r \n " + boundary + b"\r \n "
2811+ b"\r \n "
2812+ + boundary
2813+ + b"\r \n "
27982814 + b'Content-Disposition: form-data; name="file"; filename="t.txt"\r \n '
27992815 + b"Content-Type: text/plain\r \n \r \n "
28002816 + b"file-content\r \n "
28012817 )
2802- field_section = (
2803- boundary + b"\r \n "
2804- + b'Content-Disposition: form-data; name="field"\r \n \r \n '
2805- + b"value\r \n "
2806- )
2818+ field_section = boundary + b"\r \n " + b'Content-Disposition: form-data; name="field"\r \n \r \n ' + b"value\r \n "
28072819 closing = boundary + b"--\r \n "
28082820 decoded = file_section + field_section + closing
28092821
@@ -2826,6 +2838,7 @@ def test_decode_form_field_content_falls_back_to_bytes_on_decode_error():
28262838
28272839def test_extract_field_value_from_body_handles_attribute_error ():
28282840 from types import SimpleNamespace
2841+
28292842 errors = []
28302843 field = SimpleNamespace (alias = "x" )
28312844 ov = __import__ ("aws_lambda_powertools.event_handler.middlewares.openapi_validation" , fromlist = ["*" ])
@@ -2839,7 +2852,7 @@ def test_resolve_field_type_returns_first_non_none_in_union():
28392852
28402853 ov = __import__ ("aws_lambda_powertools.event_handler.middlewares.openapi_validation" , fromlist = ["*" ])
28412854 resolved = ov ._resolve_field_type (Union [int , None ])
2842- assert resolved == int
2855+ assert resolved is int
28432856
28442857
28452858def test_normalize_field_value_list_to_single_when_not_sequence ():
0 commit comments