Adding upstream version 0.5.1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
303fa6e9d8
commit
97e6d74bac
110 changed files with 12006 additions and 0 deletions
0
tests/util/__init__.py
Normal file
0
tests/util/__init__.py
Normal file
114
tests/util/test_optional_and_computed.py
Normal file
114
tests/util/test_optional_and_computed.py
Normal file
|
@ -0,0 +1,114 @@
|
|||
# mypy: ignore-errors
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import pytest
|
||||
|
||||
from openapi_pydantic import (
|
||||
Info,
|
||||
MediaType,
|
||||
OpenAPI,
|
||||
Operation,
|
||||
PathItem,
|
||||
RequestBody,
|
||||
Response,
|
||||
Schema,
|
||||
)
|
||||
from openapi_pydantic.compat import PYDANTIC_V2
|
||||
from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
|
||||
|
||||
|
||||
@pytest.mark.skipif(not PYDANTIC_V2, reason="computed fields require Pydantic V2")
|
||||
def test_optional_and_computed_fields() -> None:
|
||||
api = construct_sample_api()
|
||||
|
||||
result = construct_open_api_with_schema_class(api)
|
||||
assert result.components is not None
|
||||
assert result.components.schemas is not None
|
||||
|
||||
req_schema = result.components.schemas["SampleRequest"]
|
||||
assert isinstance(req_schema, Schema)
|
||||
assert req_schema.properties is not None
|
||||
assert req_schema.required is not None
|
||||
|
||||
resp_schema = result.components.schemas["SampleResponse"]
|
||||
assert isinstance(resp_schema, Schema)
|
||||
assert resp_schema.properties is not None
|
||||
assert resp_schema.required is not None
|
||||
|
||||
# When validating:
|
||||
# - required fields are still required
|
||||
# - optional fields are still optional
|
||||
# - computed fields don't exist
|
||||
assert "req" in req_schema.properties
|
||||
assert "opt" in req_schema.properties
|
||||
assert "comp" not in req_schema.properties
|
||||
assert set(req_schema.required) == {"req"}
|
||||
|
||||
# When serializing:
|
||||
# - required fields are still required
|
||||
# - optional fields are still optional
|
||||
# (except when json_schema_serialization_defaults_required is enabled)
|
||||
# - computed fields are required
|
||||
assert "req" in resp_schema.properties
|
||||
assert "opt" in resp_schema.properties
|
||||
assert "comp" in resp_schema.properties
|
||||
assert set(resp_schema.required) == {"req", "comp"}
|
||||
|
||||
|
||||
def construct_sample_api() -> OpenAPI:
|
||||
from typing import TYPE_CHECKING, Callable
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
||||
def computed_field(x: Callable) -> Callable: ...
|
||||
|
||||
else:
|
||||
from pydantic import computed_field
|
||||
|
||||
class SampleModel(BaseModel):
|
||||
req: bool
|
||||
opt: Optional[bool] = None
|
||||
|
||||
@computed_field
|
||||
@property
|
||||
def comp(self) -> bool:
|
||||
return True
|
||||
|
||||
class SampleRequest(SampleModel):
|
||||
model_config = {"json_schema_mode": "validation"}
|
||||
|
||||
class SampleResponse(SampleModel):
|
||||
model_config = {"json_schema_mode": "serialization"}
|
||||
|
||||
return OpenAPI(
|
||||
info=Info(
|
||||
title="Sample API",
|
||||
version="v0.0.1",
|
||||
),
|
||||
paths={
|
||||
"/callme": PathItem(
|
||||
post=Operation(
|
||||
requestBody=RequestBody(
|
||||
content={
|
||||
"application/json": MediaType(
|
||||
schema=PydanticSchema(schema_class=SampleRequest)
|
||||
)
|
||||
}
|
||||
),
|
||||
responses={
|
||||
"200": Response(
|
||||
description="resp",
|
||||
content={
|
||||
"application/json": MediaType(
|
||||
schema=PydanticSchema(schema_class=SampleResponse)
|
||||
)
|
||||
},
|
||||
)
|
||||
},
|
||||
)
|
||||
)
|
||||
},
|
||||
)
|
163
tests/util/test_pydantic_field.py
Normal file
163
tests/util/test_pydantic_field.py
Normal file
|
@ -0,0 +1,163 @@
|
|||
from typing import Dict, List, Union
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from typing_extensions import Literal
|
||||
|
||||
from openapi_pydantic import (
|
||||
Discriminator,
|
||||
Info,
|
||||
MediaType,
|
||||
OpenAPI,
|
||||
Operation,
|
||||
PathItem,
|
||||
Reference,
|
||||
RequestBody,
|
||||
Response,
|
||||
Schema,
|
||||
)
|
||||
from openapi_pydantic.compat import (
|
||||
DEFS_KEY,
|
||||
PYDANTIC_MINOR_VERSION,
|
||||
PYDANTIC_V2,
|
||||
models_json_schema,
|
||||
v1_schema,
|
||||
)
|
||||
from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
|
||||
|
||||
|
||||
class DataAModel(BaseModel):
|
||||
kind: Literal["a"]
|
||||
|
||||
|
||||
class DataBModel(BaseModel):
|
||||
kind: Literal["b"]
|
||||
|
||||
|
||||
class RequestModel(BaseModel):
|
||||
data: Union[DataAModel, DataBModel] = Field(discriminator="kind")
|
||||
|
||||
|
||||
def construct_base_open_api() -> 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=RequestModel
|
||||
)
|
||||
)
|
||||
}
|
||||
),
|
||||
responses={"200": Response(description="pong")},
|
||||
)
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_pydantic_discriminator_schema_generation() -> None:
|
||||
"""https://github.com/kuimono/openapi-schema-pydantic/issues/8"""
|
||||
|
||||
a_kind: Dict[str, Union[str, List[str]]] = {"title": "Kind", "type": "string"}
|
||||
b_kind: Dict[str, Union[str, List[str]]] = {"title": "Kind", "type": "string"}
|
||||
|
||||
if PYDANTIC_V2:
|
||||
_key_map, json_schema = models_json_schema([(RequestModel, "validation")])
|
||||
# In Pydantic v2, string literal types are mapped to consts.
|
||||
a_kind["const"] = "a"
|
||||
b_kind["const"] = "b"
|
||||
if PYDANTIC_MINOR_VERSION < 10:
|
||||
# Prior to 2.10, string literal types are also mapped to enums with a single entry.
|
||||
a_kind["enum"] = ["a"]
|
||||
b_kind["enum"] = ["b"]
|
||||
else:
|
||||
json_schema = v1_schema([RequestModel])
|
||||
# In Pydantic v1, string literal types are mapped to enums with a single entry.
|
||||
a_kind["enum"] = ["a"]
|
||||
b_kind["enum"] = ["b"]
|
||||
|
||||
assert json_schema == {
|
||||
DEFS_KEY: {
|
||||
"DataAModel": {
|
||||
"properties": {
|
||||
"kind": a_kind,
|
||||
},
|
||||
"required": ["kind"],
|
||||
"title": "DataAModel",
|
||||
"type": "object",
|
||||
},
|
||||
"DataBModel": {
|
||||
"properties": {
|
||||
"kind": b_kind,
|
||||
},
|
||||
"required": ["kind"],
|
||||
"title": "DataBModel",
|
||||
"type": "object",
|
||||
},
|
||||
"RequestModel": {
|
||||
"properties": {
|
||||
"data": {
|
||||
"oneOf": [
|
||||
{"$ref": f"#/{DEFS_KEY}/DataAModel"},
|
||||
{"$ref": f"#/{DEFS_KEY}/DataBModel"},
|
||||
],
|
||||
"discriminator": {
|
||||
"mapping": {
|
||||
"a": f"#/{DEFS_KEY}/DataAModel",
|
||||
"b": f"#/{DEFS_KEY}/DataBModel",
|
||||
},
|
||||
"propertyName": "kind",
|
||||
},
|
||||
"title": "Data",
|
||||
}
|
||||
},
|
||||
"required": ["data"],
|
||||
"title": "RequestModel",
|
||||
"type": "object",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_pydantic_discriminator_openapi_generation() -> None:
|
||||
"""https://github.com/kuimono/openapi-schema-pydantic/issues/8"""
|
||||
|
||||
open_api = construct_open_api_with_schema_class(construct_base_open_api())
|
||||
assert open_api.components is not None
|
||||
assert open_api.components.schemas is not None
|
||||
json_schema = open_api.components.schemas["RequestModel"]
|
||||
assert json_schema.properties == {
|
||||
"data": Schema(
|
||||
oneOf=[
|
||||
Reference(
|
||||
**{
|
||||
"$ref": "#/components/schemas/DataAModel",
|
||||
"summary": None,
|
||||
"description": None,
|
||||
}
|
||||
),
|
||||
Reference(
|
||||
**{
|
||||
"$ref": "#/components/schemas/DataBModel",
|
||||
"summary": None,
|
||||
"description": None,
|
||||
}
|
||||
),
|
||||
],
|
||||
title="Data",
|
||||
discriminator=Discriminator(
|
||||
propertyName="kind",
|
||||
mapping={
|
||||
"a": "#/components/schemas/DataAModel",
|
||||
"b": "#/components/schemas/DataBModel",
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
271
tests/util/test_util.py
Normal file
271
tests/util/test_util.py
Normal file
|
@ -0,0 +1,271 @@
|
|||
import logging
|
||||
from typing import Callable, Generic, TypeVar
|
||||
|
||||
import pytest
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from openapi_pydantic import (
|
||||
Info,
|
||||
MediaType,
|
||||
OpenAPI,
|
||||
Operation,
|
||||
PathItem,
|
||||
Reference,
|
||||
RequestBody,
|
||||
Response,
|
||||
)
|
||||
from openapi_pydantic.compat import PYDANTIC_V2
|
||||
from openapi_pydantic.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 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 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_v2() -> 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_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")
|
117
tests/util/test_validated_schema.py
Normal file
117
tests/util/test_validated_schema.py
Normal file
|
@ -0,0 +1,117 @@
|
|||
# mypy: ignore-errors
|
||||
|
||||
import sys
|
||||
from typing import Any, Optional
|
||||
|
||||
import pytest
|
||||
from openapi_spec_validator import validate
|
||||
from pydantic import BaseModel
|
||||
|
||||
from openapi_pydantic import (
|
||||
DataType,
|
||||
Header,
|
||||
Info,
|
||||
MediaType,
|
||||
OpenAPI,
|
||||
Operation,
|
||||
PathItem,
|
||||
RequestBody,
|
||||
Response,
|
||||
Schema,
|
||||
)
|
||||
from openapi_pydantic.compat import PYDANTIC_V2
|
||||
from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
|
||||
|
||||
if sys.version_info < (3, 9):
|
||||
from typing_extensions import Annotated, Literal
|
||||
else:
|
||||
from typing import Annotated, Literal
|
||||
|
||||
|
||||
def test_basic_schema() -> None:
|
||||
class SampleModel(BaseModel):
|
||||
required: bool
|
||||
optional: Optional[bool] = None
|
||||
one_literal_choice: Literal["only_choice"]
|
||||
multiple_literal_choices: Literal["choice1", "choice2"]
|
||||
|
||||
part_api = construct_sample_api(SampleModel)
|
||||
|
||||
api = construct_open_api_with_schema_class(part_api)
|
||||
assert api.components is not None
|
||||
assert api.components.schemas is not None
|
||||
|
||||
if PYDANTIC_V2:
|
||||
json_api: Any = api.model_dump(mode="json", by_alias=True, exclude_none=True)
|
||||
else:
|
||||
json_api: Any = api.dict(by_alias=True, exclude_none=True)
|
||||
validate(json_api)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not PYDANTIC_V2,
|
||||
reason="Field-level JSON examples not supported in Pydantic V1",
|
||||
)
|
||||
def test_field_json_schema_example() -> None:
|
||||
from pydantic import WithJsonSchema
|
||||
|
||||
Thing = Annotated[str, WithJsonSchema({"examples": ["thing1"]})]
|
||||
|
||||
class SampleModel(BaseModel):
|
||||
a: Thing
|
||||
|
||||
part_api = construct_sample_api(SampleModel)
|
||||
|
||||
api = construct_open_api_with_schema_class(part_api)
|
||||
assert api.components is not None
|
||||
assert api.components.schemas is not None
|
||||
|
||||
json_api: Any = api.model_dump(mode="json", by_alias=True, exclude_none=True)
|
||||
props = json_api["components"]["schemas"]["SampleRequest"]["properties"]
|
||||
assert props["a"]["examples"] == ["thing1"]
|
||||
|
||||
validate(json_api)
|
||||
|
||||
|
||||
def construct_sample_api(SampleModel) -> OpenAPI:
|
||||
class SampleRequest(SampleModel):
|
||||
model_config = {"json_schema_mode": "validation"}
|
||||
|
||||
class SampleResponse(SampleModel):
|
||||
model_config = {"json_schema_mode": "serialization"}
|
||||
|
||||
return OpenAPI(
|
||||
info=Info(
|
||||
title="Sample API",
|
||||
version="v0.0.1",
|
||||
),
|
||||
paths={
|
||||
"/callme": PathItem(
|
||||
post=Operation(
|
||||
requestBody=RequestBody(
|
||||
content={
|
||||
"application/json": MediaType(
|
||||
schema=PydanticSchema(schema_class=SampleRequest)
|
||||
)
|
||||
}
|
||||
),
|
||||
responses={
|
||||
"200": Response(
|
||||
description="resp",
|
||||
headers={
|
||||
"WWW-Authenticate": Header(
|
||||
description="Indicate how to authenticate",
|
||||
schema=Schema(type=DataType.STRING),
|
||||
)
|
||||
},
|
||||
content={
|
||||
"application/json": MediaType(
|
||||
schema=PydanticSchema(schema_class=SampleResponse)
|
||||
)
|
||||
},
|
||||
)
|
||||
},
|
||||
)
|
||||
)
|
||||
},
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue