Skip to content

Commit e7a96da

Browse files
sudosubinyanyongyu
andauthored
✨ Feature: Support pydantic v2 (#45)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
1 parent 22baa80 commit e7a96da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2029
-1579
lines changed

codegen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def load_config() -> Config:
4040
pyproject = tomli.loads(Path("./pyproject.toml").read_text())
4141
config_dict: Dict[str, Any] = pyproject.get("tool", {}).get("codegen", {})
4242

43-
return Config.parse_obj(config_dict)
43+
return Config.model_validate(config_dict)
4444

4545

4646
def build_rest_api(data: OpenAPIData, rest: RestConfig):

codegen/parser/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def parse_openapi_spec(source: Source, rest: RestConfig, config: Config) -> Open
5757
_st = _schemas.set({})
5858

5959
try:
60-
openapi = OpenAPI.parse_obj(source.root)
60+
openapi = OpenAPI.model_validate(source.root)
6161

6262
# cache /components/schemas first
6363
if openapi.components and openapi.components.schemas:

codegen/parser/endpoints/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
def parse_endpoint(source: Source, path: str) -> List[EndpointData]:
1616
data = source.data
17-
data = oas.PathItem.parse_obj(data)
17+
data = oas.PathItem.model_validate(data)
1818

1919
endpoints: List[EndpointData] = []
2020

codegen/parser/endpoints/parameter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Union, Literal
22

33
import openapi_pydantic as oas
4-
from pydantic import parse_obj_as
4+
from pydantic import TypeAdapter
55

66
from ...source import Source
77
from ..utils import build_prop_name, concat_snake_name
@@ -15,14 +15,14 @@ class Parameter(Property):
1515
def build_param(source: Source, prefix: str) -> Parameter:
1616
data = source.data
1717
try:
18-
data = parse_obj_as(Union[oas.Reference, oas.Parameter], data)
18+
data = TypeAdapter(Union[oas.Reference, oas.Parameter]).validate_python(data)
1919
except Exception as e:
2020
raise TypeError(f"Invalid Parameter from {source.uri}") from e
2121

2222
if isinstance(data, oas.Reference):
2323
source = source.resolve_ref(data.ref)
2424
try:
25-
data = oas.Parameter.parse_obj(source.data)
25+
data = oas.Parameter.model_validate(source.data)
2626
except Exception as e:
2727
raise TypeError(f"Invalid Parameter from {source.uri}") from e
2828

@@ -38,5 +38,5 @@ def build_param(source: Source, prefix: str) -> Parameter:
3838
prop_name=build_prop_name(data.name),
3939
required=data.required,
4040
schema_data=schema,
41-
param_in=data.param_in, # type: ignore
41+
param_in=data.param_in.value,
4242
)

codegen/parser/endpoints/request_body.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Set, List, Union, Literal
22

33
import openapi_pydantic as oas
4-
from pydantic import BaseModel, parse_obj_as
4+
from pydantic import BaseModel, TypeAdapter
55

66
from ...source import Source
77
from ..utils import concat_snake_name
@@ -58,14 +58,14 @@ def get_using_imports(self) -> Set[str]:
5858
def build_request_body(source: Source, prefix: str) -> RequestBodyData:
5959
data = source.data
6060
try:
61-
data = parse_obj_as(Union[oas.Reference, oas.RequestBody], data)
61+
data = TypeAdapter(Union[oas.Reference, oas.RequestBody]).validate_python(data)
6262
except Exception as e:
6363
raise TypeError(f"Invalid RequestBody from {source.uri}") from e
6464

6565
if isinstance(data, oas.Reference):
6666
source = source.resolve_ref(data.ref)
6767
try:
68-
data = oas.RequestBody.parse_obj(source.data)
68+
data = oas.RequestBody.model_validate(source.data)
6969
except Exception as e:
7070
raise TypeError(f"Invalid RequestBody from {source.uri}") from e
7171

codegen/parser/endpoints/response.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Set, Union, Optional
22

33
import openapi_pydantic as oas
4-
from pydantic import BaseModel, parse_obj_as
4+
from pydantic import BaseModel, TypeAdapter
55

66
from ...source import Source
77
from ..schemas import SchemaData, parse_schema
@@ -20,14 +20,14 @@ def get_using_imports(self) -> Set[str]:
2020
def build_response(source: Source, prefix: str) -> ResponseData:
2121
data = source.data
2222
try:
23-
data = parse_obj_as(Union[oas.Reference, oas.Response], data)
23+
data = TypeAdapter(Union[oas.Reference, oas.Response]).validate_python(data)
2424
except Exception as e:
2525
raise TypeError(f"Invalid Response from {source.uri}") from e
2626

2727
if isinstance(data, oas.Reference):
2828
source = source.resolve_ref(data.ref)
2929
try:
30-
data = oas.Response.parse_obj(source.data)
30+
data = oas.Response.model_validate(source.data)
3131
except Exception as e:
3232
raise TypeError(f"Invalid Response from {source.uri}") from e
3333

codegen/parser/schemas/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Union, Optional
22

33
import openapi_pydantic as oas
4-
from pydantic import parse_obj_as
4+
from pydantic import TypeAdapter
55

66
from ...source import Source
77
from ..utils import schema_from_source
@@ -21,14 +21,15 @@
2121
from .schema import StringSchema as StringSchema
2222
from .. import add_schema, get_schema, get_schemas
2323
from .schema import DateTimeSchema as DateTimeSchema
24+
from .schema import UniqueListSchema as UniqueListSchema
2425

2526

2627
def parse_schema(
2728
source: Source, class_name: str, base_source: Optional[Source] = None
2829
) -> SchemaData:
2930
data = source.data
3031
try:
31-
data = parse_obj_as(Union[oas.Reference, oas.Schema], data)
32+
data = TypeAdapter(Union[oas.Reference, oas.Schema]).validate_python(data)
3233
except Exception as e:
3334
raise TypeError(f"Invalid Schema from {source.uri}") from e
3435

codegen/parser/schemas/any_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def build_any_schema(source: Source) -> AnySchema:
88
try:
9-
data = oas.Schema.parse_obj(source.data)
9+
data = oas.Schema.model_validate(source.data)
1010
except Exception as e:
1111
raise TypeError(f"Invalid Schema from {source.uri}") from e
1212

codegen/parser/schemas/bool_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
def build_bool_schema(source: Source) -> BoolSchema:
99
try:
10-
data = oas.Schema.parse_obj(source.data)
10+
data = oas.Schema.model_validate(source.data)
1111
except Exception as e:
1212
raise TypeError(f"Invalid Schema from {source.uri}") from e
1313

codegen/parser/schemas/list_schema.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
from typing import Optional
1+
from typing import Union, Optional
22

33
from . import parse_schema
44
from ...source import Source
5-
from .schema import ListSchema
5+
from .schema import ListSchema, UniqueListSchema
66
from ..utils import concat_snake_name, schema_from_source
77

88

99
def build_list_schema(
1010
source: Source, class_name: str, base_source: Optional[Source] = None
11-
) -> ListSchema:
11+
) -> Union[ListSchema, UniqueListSchema]:
1212
data = schema_from_source(source)
1313
base_schema = schema_from_source(base_source) if base_source else None
1414

15-
return ListSchema(
15+
if data.uniqueItems or (base_schema and base_schema.uniqueItems):
16+
schema_class = UniqueListSchema
17+
else:
18+
schema_class = ListSchema
19+
20+
return schema_class(
1621
title=data.title,
1722
description=data.description,
1823
default=data.default,
1924
examples=data.examples or (data.example and [data.example]),
2025
item_schema=parse_schema(
2126
source / "items", concat_snake_name(class_name, "items")
2227
),
23-
min_items=data.minItems or (base_schema and base_schema.minItems),
24-
max_items=data.maxItems or (base_schema and base_schema.maxItems),
25-
unique_items=data.uniqueItems or (base_schema and base_schema.uniqueItems),
28+
min_length=data.minItems or (base_schema and base_schema.minItems),
29+
max_length=data.maxItems or (base_schema and base_schema.maxItems),
2630
)

codegen/parser/schemas/model_schema.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
UnionSchema,
2727
StringSchema,
2828
DateTimeSchema,
29+
UniqueListSchema,
2930
)
3031

3132
ST = TypeVar("ST", bound=SchemaData)
@@ -61,7 +62,7 @@ def _is_union_subset(first: SchemaData, second: SchemaData) -> Optional[SchemaDa
6162
second_schemas if len(first_schemas) <= len(second_schemas) else first_schemas
6263
)
6364
for schema in one_schemas:
64-
if schema not in another_schemas:
65+
if schema.model_dump() not in [s.model_dump() for s in another_schemas]:
6566
return
6667
return first if len(first_schemas) <= len(second_schemas) else second
6768

@@ -157,6 +158,27 @@ def _is_list_merge(
157158
)
158159

159160

161+
def _is_unique_list_merge(
162+
source: Source, name: str, prefix: str, first: SchemaData, second: SchemaData
163+
) -> Optional[UniqueListSchema]:
164+
if (
165+
(isinstance(first, UniqueListSchema) and isinstance(second, ListSchema))
166+
or (isinstance(first, ListSchema) and isinstance(second, UniqueListSchema))
167+
or (
168+
isinstance(first, UniqueListSchema) and isinstance(second, UniqueListSchema)
169+
)
170+
):
171+
return UniqueListSchema(
172+
title=first.title,
173+
description=first.description,
174+
default=first.default,
175+
examples=first.examples,
176+
item_schema=_merge_schema(
177+
source, name, prefix, first.item_schema, second.item_schema
178+
),
179+
)
180+
181+
160182
def _merge_schema(
161183
source: Source, name: str, prefix: str, first: SchemaData, second: SchemaData
162184
):
@@ -167,6 +189,7 @@ def _merge_schema(
167189
or _is_enum_subset(first, second)
168190
or _is_model_merge(source, name, prefix, first, second)
169191
or _is_list_merge(source, name, prefix, first, second)
192+
or _is_unique_list_merge(source, name, prefix, first, second)
170193
):
171194
return schema
172195
raise RuntimeError(f"Cannot merge schema for {name}: {first!r}; {second!r}")

codegen/parser/schemas/none_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def build_none_schema(source: Source) -> NoneSchema:
88
try:
9-
data = oas.Schema.parse_obj(source.data)
9+
data = oas.Schema.model_validate(source.data)
1010
except Exception as e:
1111
raise TypeError(f"Invalid Schema from {source.uri}") from e
1212

0 commit comments

Comments
 (0)