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