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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
import logging
from typing import Callable, Generic, Literal, TypeVar
import pytest
from pydantic import BaseModel, Field
from openapi_pydantic.compat import PYDANTIC_V2
from openapi_pydantic.v3.v3_0 import (
Info,
MediaType,
OpenAPI,
Operation,
PathItem,
Reference,
RequestBody,
Response,
Schema,
)
from openapi_pydantic.v3.v3_0.util import (
PydanticSchema,
construct_open_api_with_schema_class,
)
def test_construct_open_api_with_schema_class_1() -> None:
open_api = construct_base_open_api_1()
result_open_api_1 = construct_open_api_with_schema_class(open_api)
result_open_api_2 = construct_open_api_with_schema_class(
open_api, [PingRequest, PingResponse]
)
assert result_open_api_1.components == result_open_api_2.components
assert result_open_api_1 == result_open_api_2
dump_json = getattr(result_open_api_1, "model_dump_json" if PYDANTIC_V2 else "json")
open_api_json = dump_json(by_alias=True, exclude_none=True, indent=2)
logging.debug(open_api_json)
def test_construct_open_api_with_schema_class_2() -> None:
open_api_1 = construct_base_open_api_1()
open_api_2 = construct_base_open_api_2()
result_open_api_1 = construct_open_api_with_schema_class(open_api_1)
result_open_api_2 = construct_open_api_with_schema_class(
open_api_2, [PingRequest, PingResponse]
)
assert result_open_api_1 == result_open_api_2
def test_construct_open_api_with_schema_class_3() -> None:
open_api_3 = construct_base_open_api_3()
result_with_alias_1 = construct_open_api_with_schema_class(open_api_3)
assert result_with_alias_1.components is not None
assert result_with_alias_1.components.schemas is not None
schema_with_alias = result_with_alias_1.components.schemas["PongResponse"]
assert isinstance(schema_with_alias, Schema)
assert schema_with_alias.properties is not None
assert "pong_foo" in schema_with_alias.properties
assert "pong_bar" in schema_with_alias.properties
result_with_alias_2 = construct_open_api_with_schema_class(
open_api_3, by_alias=True
)
assert result_with_alias_1 == result_with_alias_2
result_without_alias = construct_open_api_with_schema_class(
open_api_3, by_alias=False
)
assert result_without_alias.components is not None
assert result_without_alias.components.schemas is not None
schema_without_alias = result_without_alias.components.schemas["PongResponse"]
assert isinstance(schema_without_alias, Schema)
assert schema_without_alias.properties is not None
assert "resp_foo" in schema_without_alias.properties
assert "resp_bar" in schema_without_alias.properties
@pytest.mark.skipif(PYDANTIC_V2, reason="generic type for Pydantic V1")
def test_construct_open_api_with_schema_class_4_generic_response_v1() -> None:
DataT = TypeVar("DataT")
from pydantic.v1.generics import GenericModel
class GenericResponse(GenericModel, Generic[DataT]):
msg: str = Field(description="message of the generic response")
data: DataT = Field(description="data value of the generic response")
open_api_4 = construct_base_open_api_4_generic_response(
GenericResponse[PongResponse]
)
result = construct_open_api_with_schema_class(open_api_4)
assert result.components is not None
assert result.components.schemas is not None
assert "GenericResponse_PongResponse_" in result.components.schemas
@pytest.mark.skipif(not PYDANTIC_V2, reason="generic type for Pydantic V2")
def test_construct_open_api_with_schema_class_4_generic_response() -> None:
DataT = TypeVar("DataT")
class GenericResponse(BaseModel, Generic[DataT]):
msg: str = Field(description="message of the generic response")
data: DataT = Field(description="data value of the generic response")
open_api_4 = construct_base_open_api_4_generic_response(
GenericResponse[PongResponse]
)
result = construct_open_api_with_schema_class(open_api_4)
assert result.components is not None
assert result.components.schemas is not None
assert "GenericResponse_PongResponse_" in result.components.schemas
def construct_base_open_api_1() -> OpenAPI:
model_validate: Callable[[dict], OpenAPI] = getattr(
OpenAPI, "model_validate" if PYDANTIC_V2 else "parse_obj"
)
return model_validate(
{
"info": {"title": "My own API", "version": "v0.0.1"},
"paths": {
"/ping": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": PydanticSchema(schema_class=PingRequest)
}
}
},
"responses": {
"200": {
"description": "pong",
"content": {
"application/json": {
"schema": PydanticSchema(
schema_class=PingResponse
)
}
},
}
},
}
}
},
}
)
def construct_base_open_api_2() -> OpenAPI:
return OpenAPI(
info=Info(title="My own API", version="v0.0.1"),
paths={
"/ping": PathItem(
post=Operation(
requestBody=RequestBody(
content={
"application/json": MediaType(
media_type_schema=Reference(
**{"$ref": "#/components/schemas/PingRequest"}
)
)
}
),
responses={
"200": Response(
description="pong",
content={
"application/json": MediaType(
media_type_schema=Reference(
**{"$ref": "#/components/schemas/PingResponse"}
)
)
},
)
},
)
)
},
)
def construct_base_open_api_3() -> OpenAPI:
return OpenAPI(
info=Info(
title="My own API",
version="v0.0.1",
),
paths={
"/ping": PathItem(
post=Operation(
requestBody=RequestBody(
content={
"application/json": MediaType(
media_type_schema=PydanticSchema(
schema_class=PingRequest
)
)
}
),
responses={
"200": Response(
description="pong",
content={
"application/json": MediaType(
media_type_schema=PydanticSchema(
schema_class=PongResponse
)
)
},
)
},
)
)
},
)
def construct_base_open_api_3_plus() -> OpenAPI:
return OpenAPI(
info=Info(
title="My own API",
version="v0.0.1",
),
paths={
"/ping": PathItem(
post=Operation(
requestBody=RequestBody(
content={
"application/json": MediaType(
media_type_schema=PydanticSchema(
schema_class=PingPlusRequest
)
)
}
),
responses={
"200": Response(
description="pong",
content={
"application/json": MediaType(
media_type_schema=PydanticSchema(
schema_class=PongResponse
)
)
},
)
},
)
)
},
)
def construct_base_open_api_4_generic_response(response_schema: type) -> OpenAPI:
return OpenAPI(
info=Info(
title="My own API",
version="v0.0.1",
),
paths={
"/ping": PathItem(
post=Operation(
requestBody=RequestBody(
content={
"application/json": MediaType(
media_type_schema=PydanticSchema(
schema_class=PingRequest
)
)
}
),
responses={
"200": Response(
description="pong",
content={
"application/json": MediaType(
media_type_schema=PydanticSchema(
schema_class=response_schema
)
)
},
)
},
)
)
},
)
class PingRequest(BaseModel):
"""Ping Request"""
req_foo: str = Field(description="foo value of the request")
req_bar: str = Field(description="bar value of the request")
class PingResponse(BaseModel):
"""Ping response"""
resp_foo: str = Field(description="foo value of the response")
resp_bar: str = Field(description="bar value of the response")
class PongResponse(BaseModel):
"""Pong response"""
resp_foo: str = Field(alias="pong_foo", description="foo value of the response")
resp_bar: str = Field(alias="pong_bar", description="bar value of the response")
class PingPlusRequest(BaseModel):
"""Ping Request with extra"""
req_foo: str
req_bar: str
req_single_choice: Literal["one"]
def test_enum_with_single_choice() -> None:
api_obj = construct_open_api_with_schema_class(construct_base_open_api_3_plus())
model_dump = getattr(api_obj, "model_dump" if PYDANTIC_V2 else "dict")
api = model_dump(by_alias=True, exclude_none=True)
schema = api["components"]["schemas"]["PingPlusRequest"]
prop = schema["properties"]["req_single_choice"]
# OpenAPI 3.0 does not support "const", so make sure the enum is not
# rendered that way.
assert not prop.get("const")
assert prop["enum"] == ["one"]
|