Skip to content

Commit

Permalink
Fix base_model setting for AutoSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSuperiorStanislav committed Apr 8, 2024
1 parent 296f2bc commit 0797732
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions saritasa_sqlalchemy_tools/auto_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ def get_schema(
model_config = getattr(
cls.Meta,
"model_config",
pydantic.ConfigDict(from_attributes=True),
None,
)
# Only config or base model could be passed to create_model
if base_model:
if base_model and model_config:
raise ValueError(
"Only config or base model could be passed to create_model",
)
if base_model is None and model_config is None:
model_config = pydantic.ConfigDict(from_attributes=True)

extra_fields_config = getattr(
cls.Meta,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_auto_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ class Meta:
AutoSchema.get_schema()


async def test_auto_schema_use_model() -> None:
"""Test schema generation works when base model is specified."""

class AutoSchema(saritasa_sqlalchemy_tools.ModelAutoSchema):
class Meta:
model = models.TestModel
base_model = pydantic.BaseModel
fields = (
"id",
"created",
"modified",
)

AutoSchema.get_schema()


def custom_validator(
cls, # noqa: ANN001
value: typing.Any,
Expand Down

0 comments on commit 0797732

Please sign in to comment.