Skip to content

Commit

Permalink
refactor: create BaseModel with registry
Browse files Browse the repository at this point in the history
  • Loading branch information
codectl committed Mar 26, 2023
1 parent 5072540 commit 957f36a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/apispec_plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from importlib import metadata

from .utils import spec_from
from .ext.pydantic import PydanticPlugin
from .ext.pydantic import BaseModel, PydanticPlugin
from .webframeworks.flask import FlaskPlugin
from .base.mixin import DataclassSchemaMixin, RegistryMixin

__version__ = metadata.version("apispec-plugins")
__all__ = (
BaseModel,
DataclassSchemaMixin,
FlaskPlugin,
PydanticPlugin,
Expand Down
10 changes: 7 additions & 3 deletions src/apispec_plugins/ext/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

from apispec import BasePlugin, APISpec
from apispec.exceptions import APISpecError, DuplicateComponentNameError
from apispec_plugins.base.registry import Registry
from pydantic import BaseModel
from apispec_plugins.base.mixin import RegistryMixin
from pydantic import BaseModel as PBaseModel


class BaseModel(PBaseModel, RegistryMixin):
"""Extend BaseModel with Registry"""


class PydanticPlugin(BasePlugin):
Expand Down Expand Up @@ -152,7 +156,7 @@ def resolve_schema_instance(
elif isinstance(schema, BaseModel):
return schema.__class__
elif isinstance(schema, str):
return Registry.get_cls(schema)
return BaseModel.get_cls(schema)
return None

@classmethod
Expand Down
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Optional

from apispec_plugins.base.mixin import RegistryMixin
from pydantic import BaseModel
from apispec_plugins.ext.pydantic import BaseModel


class Pet(BaseModel, RegistryMixin):
class Pet(BaseModel):
id: Optional[int]
name: str

0 comments on commit 957f36a

Please sign in to comment.