Skip to content

Commit 4daaa28

Browse files
committed
make format
1 parent bdf2602 commit 4daaa28

File tree

3 files changed

+61
-68
lines changed

3 files changed

+61
-68
lines changed

examples/event_handler_rest/src/handling_file_uploads.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
@app.post("/upload")
1010
def upload_file(
1111
file: Annotated[bytes, File(description="File to upload")],
12-
filename: Annotated[str, Form(description="Name of the file")]
12+
filename: Annotated[str, Form(description="Name of the file")],
1313
):
1414
# file contains the binary data of the uploaded file
1515
# filename contains the form field value
16-
return {
17-
"message": f"Uploaded {filename}",
18-
"size": len(file)
19-
}
16+
return {"message": f"Uploaded {filename}", "size": len(file)}
2017

2118

2219
def lambda_handler(event, context):

examples/event_handler_rest/src/handling_multiple_file_uploads.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
@app.post("/upload-multiple")
1010
def upload_multiple_files(
1111
files: Annotated[List[bytes], File(description="Files to upload")],
12-
description: Annotated[str, Form(description="Upload description")]
12+
description: Annotated[str, Form(description="Upload description")],
1313
):
1414
return {
1515
"message": f"Uploaded {len(files)} files",
1616
"description": description,
17-
"total_size": sum(len(file) for file in files)
17+
"total_size": sum(len(file) for file in files),
1818
}
1919

2020

tests/functional/event_handler/_pydantic/test_openapi_params.py

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -654,174 +654,170 @@ def handler(
654654
def test_openapi_file_upload_parameters():
655655
"""Test File parameter generates correct OpenAPI schema for file uploads."""
656656
from aws_lambda_powertools.event_handler.openapi.params import _File, _Form
657-
657+
658658
app = APIGatewayRestResolver(enable_validation=True)
659659

660660
@app.post("/upload")
661661
def upload_file(
662662
file: Annotated[bytes, _File(description="File to upload")],
663-
filename: Annotated[str, _Form(description="Name of the file")]
663+
filename: Annotated[str, _Form(description="Name of the file")],
664664
):
665665
return {"message": f"Uploaded {filename}", "size": len(file)}
666666

667667
schema = app.get_openapi_schema()
668-
668+
669669
# Check that the endpoint is present
670670
assert "/upload" in schema.paths
671-
671+
672672
post_op = schema.paths["/upload"].post
673673
assert post_op is not None
674-
674+
675675
# Check request body
676676
request_body = post_op.requestBody
677677
assert request_body is not None
678678
assert request_body.required is True
679-
679+
680680
# Check content type is multipart/form-data
681681
assert "multipart/form-data" in request_body.content
682-
682+
683683
# Get the schema reference
684684
multipart_content = request_body.content["multipart/form-data"]
685685
assert multipart_content.schema_ is not None
686-
686+
687687
# Check that it references a component schema
688688
schema_ref = multipart_content.schema_.ref
689689
assert schema_ref is not None
690690
assert schema_ref.startswith("#/components/schemas/")
691-
691+
692692
# Get the component schema name
693693
component_name = schema_ref.split("/")[-1]
694694
assert component_name in schema.components.schemas
695-
695+
696696
# Check the component schema properties
697697
component_schema = schema.components.schemas[component_name]
698698
properties = component_schema.properties
699-
699+
700700
# Check file parameter
701701
assert "file" in properties
702702
file_prop = properties["file"]
703703
assert file_prop.type == "string"
704704
assert file_prop.format == "binary" # This is the key assertion
705705
assert file_prop.title == "File"
706706
assert file_prop.description == "File to upload"
707-
707+
708708
# Check form parameter
709709
assert "filename" in properties
710710
filename_prop = properties["filename"]
711711
assert filename_prop.type == "string"
712712
assert filename_prop.title == "Filename"
713713
assert filename_prop.description == "Name of the file"
714-
714+
715715
# Check required fields
716716
assert component_schema.required == ["file", "filename"]
717717

718718

719719
def test_openapi_form_only_parameters():
720720
"""Test Form parameters generate application/x-www-form-urlencoded content type."""
721721
from aws_lambda_powertools.event_handler.openapi.params import _Form
722-
722+
723723
app = APIGatewayRestResolver(enable_validation=True)
724724

725725
@app.post("/form-data")
726726
def create_form_data(
727727
name: Annotated[str, _Form(description="User name")],
728-
email: Annotated[str, _Form(description="User email")] = "[email protected]"
728+
email: Annotated[str, _Form(description="User email")] = "[email protected]",
729729
):
730730
return {"name": name, "email": email}
731731

732732
schema = app.get_openapi_schema()
733-
733+
734734
# Check that the endpoint is present
735735
assert "/form-data" in schema.paths
736-
736+
737737
post_op = schema.paths["/form-data"].post
738738
assert post_op is not None
739-
739+
740740
# Check request body
741741
request_body = post_op.requestBody
742742
assert request_body is not None
743-
743+
744744
# Check content type is application/x-www-form-urlencoded
745745
assert "application/x-www-form-urlencoded" in request_body.content
746-
746+
747747
# Get the schema reference
748748
form_content = request_body.content["application/x-www-form-urlencoded"]
749749
assert form_content.schema_ is not None
750-
750+
751751
# Check that it references a component schema
752752
schema_ref = form_content.schema_.ref
753753
assert schema_ref is not None
754754
assert schema_ref.startswith("#/components/schemas/")
755-
755+
756756
# Get the component schema
757757
component_name = schema_ref.split("/")[-1]
758758
assert component_name in schema.components.schemas
759-
759+
760760
component_schema = schema.components.schemas[component_name]
761761
properties = component_schema.properties
762-
762+
763763
# Check form parameters
764764
assert "name" in properties
765765
name_prop = properties["name"]
766766
assert name_prop.type == "string"
767767
assert name_prop.description == "User name"
768-
768+
769769
assert "email" in properties
770770
email_prop = properties["email"]
771771
assert email_prop.type == "string"
772772
assert email_prop.description == "User email"
773773
assert email_prop.default == "[email protected]"
774-
774+
775775
# Check required fields (only name should be required since email has default)
776776
assert component_schema.required == ["name"]
777777

778778

779779
def test_openapi_mixed_file_and_form_parameters():
780780
"""Test mixed File and Form parameters use multipart/form-data."""
781781
from aws_lambda_powertools.event_handler.openapi.params import _File, _Form
782-
782+
783783
app = APIGatewayRestResolver(enable_validation=True)
784784

785785
@app.post("/mixed")
786786
def upload_with_metadata(
787787
file: Annotated[bytes, _File(description="Document to upload")],
788788
title: Annotated[str, _Form(description="Document title")],
789-
category: Annotated[str, _Form(description="Document category")] = "general"
789+
category: Annotated[str, _Form(description="Document category")] = "general",
790790
):
791-
return {
792-
"title": title,
793-
"category": category,
794-
"file_size": len(file)
795-
}
791+
return {"title": title, "category": category, "file_size": len(file)}
796792

797793
schema = app.get_openapi_schema()
798-
794+
799795
# Check that the endpoint is present
800796
assert "/mixed" in schema.paths
801-
797+
802798
post_op = schema.paths["/mixed"].post
803799
request_body = post_op.requestBody
804-
800+
805801
# When both File and Form parameters are present, should use multipart/form-data
806802
assert "multipart/form-data" in request_body.content
807-
803+
808804
# Get the component schema
809805
multipart_content = request_body.content["multipart/form-data"]
810806
schema_ref = multipart_content.schema_.ref
811807
component_name = schema_ref.split("/")[-1]
812808
component_schema = schema.components.schemas[component_name]
813-
809+
814810
properties = component_schema.properties
815-
811+
816812
# Check file parameter has binary format
817813
assert "file" in properties
818814
file_prop = properties["file"]
819815
assert file_prop.format == "binary"
820-
816+
821817
# Check form parameters are present
822818
assert "title" in properties
823819
assert "category" in properties
824-
820+
825821
# Check required fields
826822
assert "file" in component_schema.required
827823
assert "title" in component_schema.required
@@ -831,48 +827,48 @@ def upload_with_metadata(
831827
def test_openapi_multiple_file_uploads():
832828
"""Test multiple file uploads with List[bytes] type."""
833829
from aws_lambda_powertools.event_handler.openapi.params import _File, _Form
834-
830+
835831
app = APIGatewayRestResolver(enable_validation=True)
836832

837833
@app.post("/upload-multiple")
838834
def upload_multiple_files(
839835
files: Annotated[List[bytes], _File(description="Files to upload")],
840-
description: Annotated[str, _Form(description="Upload description")]
836+
description: Annotated[str, _Form(description="Upload description")],
841837
):
842838
return {
843839
"message": f"Uploaded {len(files)} files",
844840
"description": description,
845-
"total_size": sum(len(file) for file in files)
841+
"total_size": sum(len(file) for file in files),
846842
}
847843

848844
schema = app.get_openapi_schema()
849-
845+
850846
# Check that the endpoint is present
851847
assert "/upload-multiple" in schema.paths
852-
848+
853849
post_op = schema.paths["/upload-multiple"].post
854850
request_body = post_op.requestBody
855-
851+
856852
# Should use multipart/form-data for file uploads
857853
assert "multipart/form-data" in request_body.content
858-
854+
859855
# Get the component schema
860856
multipart_content = request_body.content["multipart/form-data"]
861857
schema_ref = multipart_content.schema_.ref
862858
component_name = schema_ref.split("/")[-1]
863859
component_schema = schema.components.schemas[component_name]
864-
860+
865861
properties = component_schema.properties
866-
862+
867863
# Check files parameter
868864
assert "files" in properties
869865
files_prop = properties["files"]
870-
866+
871867
# For List[bytes] with File annotation, should be array of strings with binary format
872868
assert files_prop.type == "array"
873869
assert files_prop.items.type == "string"
874870
assert files_prop.items.format == "binary"
875-
871+
876872
# Check form parameter
877873
assert "description" in properties
878874
description_prop = properties["description"]
@@ -882,38 +878,38 @@ def upload_multiple_files(
882878
def test_openapi_public_file_form_exports():
883879
"""Test that File and Form are properly exported for public use."""
884880
from aws_lambda_powertools.event_handler.openapi.params import File, Form
885-
881+
886882
app = APIGatewayRestResolver(enable_validation=True)
887883

888884
@app.post("/public-api")
889885
def upload_with_public_types(
890886
file: File, # Using the public export
891-
name: Form # Using the public export
887+
name: Form, # Using the public export
892888
):
893889
return {"status": "uploaded"}
894890

895891
schema = app.get_openapi_schema()
896-
892+
897893
# Check that the endpoint works with public exports
898894
assert "/public-api" in schema.paths
899-
895+
900896
post_op = schema.paths["/public-api"].post
901897
request_body = post_op.requestBody
902-
898+
903899
# Should generate multipart/form-data
904900
assert "multipart/form-data" in request_body.content
905-
901+
906902
# Get the component schema
907903
multipart_content = request_body.content["multipart/form-data"]
908904
schema_ref = multipart_content.schema_.ref
909905
component_name = schema_ref.split("/")[-1]
910906
component_schema = schema.components.schemas[component_name]
911-
907+
912908
properties = component_schema.properties
913-
909+
914910
# Check that both parameters are present and correctly typed
915911
assert "file" in properties
916912
assert properties["file"].format == "binary"
917-
913+
918914
assert "name" in properties
919915
assert properties["name"].type == "string"

0 commit comments

Comments
 (0)