|
| 1 | +import json |
| 2 | +import warnings |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydantic import BaseModel, Field |
| 7 | + |
| 8 | +from aws_lambda_powertools.event_handler import APIGatewayRestResolver |
| 9 | +from aws_lambda_powertools.event_handler.openapi.models import Contact, License, Server |
| 10 | +from aws_lambda_powertools.event_handler.openapi.params import Query |
| 11 | +from aws_lambda_powertools.event_handler.openapi.types import OpenAPIResponse |
| 12 | +from aws_lambda_powertools.shared.types import Annotated, Literal |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.usefixtures("pydanticv1_only") |
| 16 | +def test_openapi_3_0_simple_handler(openapi30_schema): |
| 17 | + # GIVEN APIGatewayRestResolver is initialized with enable_validation=True |
| 18 | + app = APIGatewayRestResolver(enable_validation=True) |
| 19 | + |
| 20 | + # WHEN we have a simple handler |
| 21 | + @app.get("/") |
| 22 | + def handler(): |
| 23 | + pass |
| 24 | + |
| 25 | + # WHEN we get the schema |
| 26 | + schema = json.loads(app.get_openapi_json_schema()) |
| 27 | + |
| 28 | + # THEN the schema should be valid |
| 29 | + assert openapi30_schema(schema) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.usefixtures("pydanticv1_only") |
| 33 | +def test_openapi_3_1_with_pydantic_v1(): |
| 34 | + # GIVEN APIGatewayRestResolver is initialized with enable_validation=True |
| 35 | + app = APIGatewayRestResolver(enable_validation=True) |
| 36 | + |
| 37 | + # WHEN we get the schema |
| 38 | + with warnings.catch_warnings(record=True) as w: |
| 39 | + warnings.simplefilter("default") |
| 40 | + app.get_openapi_json_schema(openapi_version="3.1.0") |
| 41 | + assert len(w) == 1 |
| 42 | + assert str(w[-1].message) == ( |
| 43 | + "You are using Pydantic v1, which is incompatible with OpenAPI schema 3.1. Forcing OpenAPI 3.0" |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.usefixtures("pydanticv1_only") |
| 48 | +def test_openapi_3_0_complex_handler(openapi30_schema): |
| 49 | + # GIVEN APIGatewayRestResolver is initialized with enable_validation=True |
| 50 | + app = APIGatewayRestResolver(enable_validation=True) |
| 51 | + |
| 52 | + # GIVEN a complex pydantic model |
| 53 | + class TodoAttributes(BaseModel): |
| 54 | + userId: int |
| 55 | + id_: Optional[int] = Field(alias="id", default=None) |
| 56 | + title: str |
| 57 | + completed: bool |
| 58 | + |
| 59 | + class Todo(BaseModel): |
| 60 | + type: Literal["ingest"] |
| 61 | + attributes: TodoAttributes |
| 62 | + |
| 63 | + class TodoEnvelope(BaseModel): |
| 64 | + data: Annotated[Todo, Field(description="The todo")] |
| 65 | + |
| 66 | + # WHEN we have a complex handler |
| 67 | + @app.get( |
| 68 | + "/", |
| 69 | + summary="This is a summary", |
| 70 | + description="Gets todos", |
| 71 | + tags=["users", "operations", "todos"], |
| 72 | + responses={ |
| 73 | + 204: OpenAPIResponse( |
| 74 | + description="Successful creation", |
| 75 | + content={"": {"schema": {}}}, |
| 76 | + ), |
| 77 | + }, |
| 78 | + ) |
| 79 | + def handler( |
| 80 | + name: Annotated[str, Query(description="The name", min_length=10, max_length=20)] = "John Doe Junior", |
| 81 | + ) -> TodoEnvelope: ... |
| 82 | + |
| 83 | + @app.post( |
| 84 | + "/todos", |
| 85 | + tags=["todo"], |
| 86 | + responses={ |
| 87 | + 204: OpenAPIResponse( |
| 88 | + description="Successful creation", |
| 89 | + content={"": {"schema": {}}}, |
| 90 | + ), |
| 91 | + }, |
| 92 | + ) |
| 93 | + def create_todo(todo: TodoEnvelope): ... |
| 94 | + |
| 95 | + # WHEN we get the schema |
| 96 | + schema = json.loads( |
| 97 | + app.get_openapi_json_schema( |
| 98 | + title="My little API", |
| 99 | + version="69", |
| 100 | + openapi_version="3.1.0", |
| 101 | + summary="API Summary", |
| 102 | + description="API description", |
| 103 | + tags=["api"], |
| 104 | + servers=[Server(url="http://localhost")], |
| 105 | + terms_of_service="Yes", |
| 106 | + contact=Contact(name="John Smith"), |
| 107 | + license_info=License(name="MIT"), |
| 108 | + ), |
| 109 | + ) |
| 110 | + |
| 111 | + # THEN the schema should be valid |
| 112 | + assert openapi30_schema(schema) |
0 commit comments