Skip to content

Commit ded045c

Browse files
committed
fix(wasm): support pydantic v1.x fallback for pyodide compatibility
The pyodide WASM environment resolves Pydantic 1.x instead of 2.x, leading to an AttributeError on `model_dump`. - Add `hasattr(spec, 'model_dump')` check in `emit_openapi_dict` to seamlessly fallback to `.dict()` for Pydantic v1. - Update `OpenAPIBase` in `models.py` to dynamically configure Pydantic `model_config` (v2) or `class Config` (v1) based on the active runtime.
1 parent e97d12b commit ded045c

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,4 @@ celerybeat.pid
169169
/update_tests*.py
170170
/fix_*.py
171171

172+
/test_pydantic_v1.py

‎src/openapi_client/models.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
from __future__ import annotations
44

55
from typing import Any, Union
6-
from pydantic import BaseModel, ConfigDict, Field
6+
from pydantic import BaseModel, ConfigDict, Field, VERSION as PYDANTIC_VERSION
77

88

99
class OpenAPIBase(BaseModel):
1010
"""OpenAPI OpenAPIBase model."""
1111

12-
model_config = ConfigDict(
13-
extra="allow",
14-
populate_by_name=True,
15-
)
12+
if PYDANTIC_VERSION.startswith("2"):
13+
model_config = ConfigDict(
14+
extra="allow",
15+
populate_by_name=True,
16+
)
17+
else:
18+
class Config:
19+
extra = "allow"
20+
allow_population_by_field_name = True
1621

1722

1823
class Contact(OpenAPIBase):

‎src/openapi_client/openapi/emit.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
def emit_openapi_dict(spec: OpenAPI) -> dict[str, Any]:
1414
"""Emit an OpenAPI model as a dictionary."""
15-
return spec.model_dump(by_alias=True, exclude_none=True)
15+
if hasattr(spec, "model_dump"):
16+
return spec.model_dump(by_alias=True, exclude_none=True)
17+
return spec.dict(by_alias=True, exclude_none=True)
1618

1719

1820
def emit_openapi_json(spec: OpenAPI, indent: int = 2) -> str:

0 commit comments

Comments
 (0)