modify schema to show many to many as list of nested models, check openapi generation in tests
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
from typing import TYPE_CHECKING, Type
|
from typing import Dict, TYPE_CHECKING, Type
|
||||||
|
|
||||||
from ormar.fields import BaseField
|
from ormar.fields import BaseField
|
||||||
from ormar.fields.foreign_key import ForeignKeyField
|
from ormar.fields.foreign_key import ForeignKeyField
|
||||||
@ -6,6 +6,8 @@ from ormar.fields.foreign_key import ForeignKeyField
|
|||||||
if TYPE_CHECKING: # pragma no cover
|
if TYPE_CHECKING: # pragma no cover
|
||||||
from ormar.models import Model
|
from ormar.models import Model
|
||||||
|
|
||||||
|
REF_PREFIX = "#/components/schemas/"
|
||||||
|
|
||||||
|
|
||||||
def ManyToMany(
|
def ManyToMany(
|
||||||
to: Type["Model"],
|
to: Type["Model"],
|
||||||
@ -31,6 +33,9 @@ def ManyToMany(
|
|||||||
pydantic_only=False,
|
pydantic_only=False,
|
||||||
default=None,
|
default=None,
|
||||||
server_default=None,
|
server_default=None,
|
||||||
|
__pydantic_model__=to,
|
||||||
|
# __origin__=List,
|
||||||
|
# __args__=[Optional[to]]
|
||||||
)
|
)
|
||||||
|
|
||||||
return type("ManyToMany", (ManyToManyField, BaseField), namespace)
|
return type("ManyToMany", (ManyToManyField, BaseField), namespace)
|
||||||
@ -38,3 +43,10 @@ def ManyToMany(
|
|||||||
|
|
||||||
class ManyToManyField(ForeignKeyField):
|
class ManyToManyField(ForeignKeyField):
|
||||||
through: Type["Model"]
|
through: Type["Model"]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __modify_schema__(cls, field_schema: Dict) -> None:
|
||||||
|
field_schema["type"] = "array"
|
||||||
|
field_schema["title"] = cls.name.title()
|
||||||
|
field_schema["definitions"] = {f"{cls.to.__name__}": cls.to.schema()}
|
||||||
|
field_schema["items"] = {"$ref": f"{REF_PREFIX}{cls.to.__name__}"}
|
||||||
|
|||||||
@ -112,10 +112,10 @@ class ModelTableProxy:
|
|||||||
return self_fields
|
return self_fields
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def resolve_relation_name(
|
def resolve_relation_name( # noqa CCR001
|
||||||
item: Union["NewBaseModel", Type["NewBaseModel"]],
|
item: Union["NewBaseModel", Type["NewBaseModel"]],
|
||||||
related: Union["NewBaseModel", Type["NewBaseModel"]],
|
related: Union["NewBaseModel", Type["NewBaseModel"]],
|
||||||
register_missing: bool = True
|
register_missing: bool = True,
|
||||||
) -> str:
|
) -> str:
|
||||||
for name, field in item.Meta.model_fields.items():
|
for name, field in item.Meta.model_fields.items():
|
||||||
if issubclass(field, ForeignKeyField):
|
if issubclass(field, ForeignKeyField):
|
||||||
@ -126,8 +126,10 @@ class ModelTableProxy:
|
|||||||
return name
|
return name
|
||||||
# fallback for not registered relation
|
# fallback for not registered relation
|
||||||
if register_missing:
|
if register_missing:
|
||||||
expand_reverse_relationships(related.__class__)
|
expand_reverse_relationships(related.__class__) # type: ignore
|
||||||
return ModelTableProxy.resolve_relation_name(item, related, register_missing=False)
|
return ModelTableProxy.resolve_relation_name(
|
||||||
|
item, related, register_missing=False
|
||||||
|
)
|
||||||
|
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"No relation between {item.get_name()} and {related.get_name()}"
|
f"No relation between {item.get_name()} and {related.get_name()}"
|
||||||
@ -147,7 +149,7 @@ class ModelTableProxy:
|
|||||||
return to_field
|
return to_field
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def translate_columns_to_aliases(cls, new_kwargs: dict) -> dict:
|
def translate_columns_to_aliases(cls, new_kwargs: Dict) -> Dict:
|
||||||
for field_name, field in cls.Meta.model_fields.items():
|
for field_name, field in cls.Meta.model_fields.items():
|
||||||
if (
|
if (
|
||||||
field_name in new_kwargs
|
field_name in new_kwargs
|
||||||
@ -158,7 +160,7 @@ class ModelTableProxy:
|
|||||||
return new_kwargs
|
return new_kwargs
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def translate_aliases_to_columns(cls, new_kwargs: dict) -> dict:
|
def translate_aliases_to_columns(cls, new_kwargs: Dict) -> Dict:
|
||||||
for field_name, field in cls.Meta.model_fields.items():
|
for field_name, field in cls.Meta.model_fields.items():
|
||||||
if field.name in new_kwargs and field.name != field_name:
|
if field.name in new_kwargs and field.name != field_name:
|
||||||
new_kwargs[field_name] = new_kwargs.pop(field.name)
|
new_kwargs[field_name] = new_kwargs.pop(field.name)
|
||||||
|
|||||||
@ -100,7 +100,7 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
|||||||
)
|
)
|
||||||
|
|
||||||
def __setattr__(self, name: str, value: Any) -> None: # noqa CCR001
|
def __setattr__(self, name: str, value: Any) -> None: # noqa CCR001
|
||||||
if name in self.__slots__:
|
if name in ("_orm_id", "_orm_saved", "_orm"):
|
||||||
object.__setattr__(self, name, value)
|
object.__setattr__(self, name, value)
|
||||||
elif name == "pk":
|
elif name == "pk":
|
||||||
object.__setattr__(self, self.Meta.pkname, value)
|
object.__setattr__(self, self.Meta.pkname, value)
|
||||||
|
|||||||
@ -6,7 +6,7 @@ from ormar.relations.querysetproxy import QuerysetProxy
|
|||||||
|
|
||||||
if TYPE_CHECKING: # pragma no cover
|
if TYPE_CHECKING: # pragma no cover
|
||||||
from ormar import Model
|
from ormar import Model
|
||||||
from ormar.relations import Relation, register_missing_relation
|
from ormar.relations import Relation
|
||||||
from ormar.queryset import QuerySet
|
from ormar.queryset import QuerySet
|
||||||
|
|
||||||
|
|
||||||
@ -72,6 +72,6 @@ class RelationProxy(list):
|
|||||||
if self.relation._type == ormar.RelationType.MULTIPLE:
|
if self.relation._type == ormar.RelationType.MULTIPLE:
|
||||||
await self.queryset_proxy.create_through_instance(item)
|
await self.queryset_proxy.create_through_instance(item)
|
||||||
rel_name = item.resolve_relation_name(item, self._owner)
|
rel_name = item.resolve_relation_name(item, self._owner)
|
||||||
if not rel_name in item._orm:
|
if rel_name not in item._orm:
|
||||||
item._orm._add_relation(item.Meta.model_fields[rel_name])
|
item._orm._add_relation(item.Meta.model_fields[rel_name])
|
||||||
setattr(item, rel_name, self._owner)
|
setattr(item, rel_name, self._owner)
|
||||||
|
|||||||
@ -15,10 +15,10 @@ class Child(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(name='child_id', primary_key=True)
|
id: ormar.Integer(name="child_id", primary_key=True)
|
||||||
first_name: ormar.String(name='fname', max_length=100)
|
first_name: ormar.String(name="fname", max_length=100)
|
||||||
last_name: ormar.String(name='lname', max_length=100)
|
last_name: ormar.String(name="lname", max_length=100)
|
||||||
born_year: ormar.Integer(name='year_born', nullable=True)
|
born_year: ormar.Integer(name="year_born", nullable=True)
|
||||||
|
|
||||||
|
|
||||||
class ArtistChildren(ormar.Model):
|
class ArtistChildren(ormar.Model):
|
||||||
@ -34,10 +34,10 @@ class Artist(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(name='artist_id', primary_key=True)
|
id: ormar.Integer(name="artist_id", primary_key=True)
|
||||||
first_name: ormar.String(name='fname', max_length=100)
|
first_name: ormar.String(name="fname", max_length=100)
|
||||||
last_name: ormar.String(name='lname', max_length=100)
|
last_name: ormar.String(name="lname", max_length=100)
|
||||||
born_year: ormar.Integer(name='year')
|
born_year: ormar.Integer(name="year")
|
||||||
children: ormar.ManyToMany(Child, through=ArtistChildren)
|
children: ormar.ManyToMany(Child, through=ArtistChildren)
|
||||||
|
|
||||||
|
|
||||||
@ -47,9 +47,9 @@ class Album(ormar.Model):
|
|||||||
metadata = metadata
|
metadata = metadata
|
||||||
database = database
|
database = database
|
||||||
|
|
||||||
id: ormar.Integer(name='album_id', primary_key=True)
|
id: ormar.Integer(name="album_id", primary_key=True)
|
||||||
name: ormar.String(name='album_name', max_length=100)
|
name: ormar.String(name="album_name", max_length=100)
|
||||||
artist: ormar.ForeignKey(Artist, name='artist_id')
|
artist: ormar.ForeignKey(Artist, name="artist_id")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="module")
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
@ -62,70 +62,87 @@ def create_test_database():
|
|||||||
|
|
||||||
|
|
||||||
def test_table_structure():
|
def test_table_structure():
|
||||||
assert 'album_id' in [x.name for x in Album.Meta.table.columns]
|
assert "album_id" in [x.name for x in Album.Meta.table.columns]
|
||||||
assert 'album_name' in [x.name for x in Album.Meta.table.columns]
|
assert "album_name" in [x.name for x in Album.Meta.table.columns]
|
||||||
assert 'fname' in [x.name for x in Artist.Meta.table.columns]
|
assert "fname" in [x.name for x in Artist.Meta.table.columns]
|
||||||
assert 'lname' in [x.name for x in Artist.Meta.table.columns]
|
assert "lname" in [x.name for x in Artist.Meta.table.columns]
|
||||||
assert 'year' in [x.name for x in Artist.Meta.table.columns]
|
assert "year" in [x.name for x in Artist.Meta.table.columns]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_working_with_aliases():
|
async def test_working_with_aliases():
|
||||||
async with database:
|
async with database:
|
||||||
async with database.transaction(force_rollback=True):
|
async with database.transaction(force_rollback=True):
|
||||||
artist = await Artist.objects.create(first_name='Ted', last_name='Mosbey', born_year=1975)
|
artist = await Artist.objects.create(
|
||||||
|
first_name="Ted", last_name="Mosbey", born_year=1975
|
||||||
|
)
|
||||||
await Album.objects.create(name="Aunt Robin", artist=artist)
|
await Album.objects.create(name="Aunt Robin", artist=artist)
|
||||||
|
|
||||||
await artist.children.create(first_name='Son', last_name='1', born_year=1990)
|
await artist.children.create(
|
||||||
await artist.children.create(first_name='Son', last_name='2', born_year=1995)
|
first_name="Son", last_name="1", born_year=1990
|
||||||
|
)
|
||||||
|
await artist.children.create(
|
||||||
|
first_name="Son", last_name="2", born_year=1995
|
||||||
|
)
|
||||||
|
|
||||||
album = await Album.objects.select_related('artist').first()
|
album = await Album.objects.select_related("artist").first()
|
||||||
assert album.artist.last_name == 'Mosbey'
|
assert album.artist.last_name == "Mosbey"
|
||||||
|
|
||||||
assert album.artist.id is not None
|
assert album.artist.id is not None
|
||||||
assert album.artist.first_name == 'Ted'
|
assert album.artist.first_name == "Ted"
|
||||||
assert album.artist.born_year == 1975
|
assert album.artist.born_year == 1975
|
||||||
|
|
||||||
assert album.name == 'Aunt Robin'
|
assert album.name == "Aunt Robin"
|
||||||
|
|
||||||
artist = await Artist.objects.select_related('children').get()
|
artist = await Artist.objects.select_related("children").get()
|
||||||
assert len(artist.children) == 2
|
assert len(artist.children) == 2
|
||||||
assert artist.children[0].first_name == 'Son'
|
assert artist.children[0].first_name == "Son"
|
||||||
assert artist.children[1].last_name == '2'
|
assert artist.children[1].last_name == "2"
|
||||||
|
|
||||||
await artist.update(last_name='Bundy')
|
await artist.update(last_name="Bundy")
|
||||||
await Artist.objects.filter(pk=artist.pk).update(born_year=1974)
|
await Artist.objects.filter(pk=artist.pk).update(born_year=1974)
|
||||||
|
|
||||||
artist = await Artist.objects.select_related('children').get()
|
artist = await Artist.objects.select_related("children").get()
|
||||||
assert artist.last_name == 'Bundy'
|
assert artist.last_name == "Bundy"
|
||||||
assert artist.born_year == 1974
|
assert artist.born_year == 1974
|
||||||
|
|
||||||
artist = await Artist.objects.select_related('children').fields(
|
artist = (
|
||||||
['first_name', 'last_name', 'born_year', 'child__first_name', 'child__last_name']).get()
|
await Artist.objects.select_related("children")
|
||||||
|
.fields(
|
||||||
|
[
|
||||||
|
"first_name",
|
||||||
|
"last_name",
|
||||||
|
"born_year",
|
||||||
|
"child__first_name",
|
||||||
|
"child__last_name",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.get()
|
||||||
|
)
|
||||||
assert artist.children[0].born_year is None
|
assert artist.children[0].born_year is None
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_bulk_operations_and_fields():
|
async def test_bulk_operations_and_fields():
|
||||||
async with database:
|
async with database:
|
||||||
d1 = Child(first_name='Daughter', last_name='1', born_year=1990)
|
d1 = Child(first_name="Daughter", last_name="1", born_year=1990)
|
||||||
d2 = Child(first_name='Daughter', last_name='2', born_year=1991)
|
d2 = Child(first_name="Daughter", last_name="2", born_year=1991)
|
||||||
await Child.objects.bulk_create([d1, d2])
|
await Child.objects.bulk_create([d1, d2])
|
||||||
|
|
||||||
children = await Child.objects.filter(first_name='Daughter').all()
|
children = await Child.objects.filter(first_name="Daughter").all()
|
||||||
assert len(children) == 2
|
assert len(children) == 2
|
||||||
assert children[0].last_name == '1'
|
assert children[0].last_name == "1"
|
||||||
|
|
||||||
for child in children:
|
for child in children:
|
||||||
child.born_year = child.born_year - 100
|
child.born_year = child.born_year - 100
|
||||||
|
|
||||||
await Child.objects.bulk_update(children)
|
await Child.objects.bulk_update(children)
|
||||||
|
|
||||||
children = await Child.objects.filter(first_name='Daughter').all()
|
children = await Child.objects.filter(first_name="Daughter").all()
|
||||||
assert len(children) == 2
|
assert len(children) == 2
|
||||||
assert children[0].born_year == 1890
|
assert children[0].born_year == 1890
|
||||||
|
|
||||||
children = await Child.objects.fields(['first_name', 'last_name']).all()
|
children = await Child.objects.fields(["first_name", "last_name"]).all()
|
||||||
assert len(children) == 2
|
assert len(children) == 2
|
||||||
for child in children:
|
for child in children:
|
||||||
assert child.born_year is None
|
assert child.born_year is None
|
||||||
@ -140,17 +157,21 @@ async def test_bulk_operations_and_fields():
|
|||||||
async def test_working_with_aliases_get_or_create():
|
async def test_working_with_aliases_get_or_create():
|
||||||
async with database:
|
async with database:
|
||||||
async with database.transaction(force_rollback=True):
|
async with database.transaction(force_rollback=True):
|
||||||
artist = await Artist.objects.get_or_create(first_name='Teddy', last_name='Bear', born_year=2020)
|
artist = await Artist.objects.get_or_create(
|
||||||
|
first_name="Teddy", last_name="Bear", born_year=2020
|
||||||
|
)
|
||||||
assert artist.pk is not None
|
assert artist.pk is not None
|
||||||
|
|
||||||
artist2 = await Artist.objects.get_or_create(first_name='Teddy', last_name='Bear', born_year=2020)
|
artist2 = await Artist.objects.get_or_create(
|
||||||
|
first_name="Teddy", last_name="Bear", born_year=2020
|
||||||
|
)
|
||||||
assert artist == artist2
|
assert artist == artist2
|
||||||
|
|
||||||
art3 = artist2.dict()
|
art3 = artist2.dict()
|
||||||
art3['born_year'] = 2019
|
art3["born_year"] = 2019
|
||||||
await Artist.objects.update_or_create(**art3)
|
await Artist.objects.update_or_create(**art3)
|
||||||
|
|
||||||
artist3 = await Artist.objects.get(last_name='Bear')
|
artist3 = await Artist.objects.get(last_name="Bear")
|
||||||
assert artist3.born_year == 2019
|
assert artist3.born_year == 2019
|
||||||
|
|
||||||
artists = await Artist.objects.all()
|
artists = await Artist.objects.all()
|
||||||
|
|||||||
@ -44,7 +44,7 @@ class Category(ormar.Model):
|
|||||||
|
|
||||||
class ItemsXCategories(ormar.Model):
|
class ItemsXCategories(ormar.Model):
|
||||||
class Meta(LocalMeta):
|
class Meta(LocalMeta):
|
||||||
tablename = 'items_x_categories'
|
tablename = "items_x_categories"
|
||||||
|
|
||||||
|
|
||||||
class Item(ormar.Model):
|
class Item(ormar.Model):
|
||||||
@ -96,9 +96,7 @@ def test_all_endpoints():
|
|||||||
response = client.post("/categories/", json={"name": "test cat2"})
|
response = client.post("/categories/", json={"name": "test cat2"})
|
||||||
category2 = response.json()
|
category2 = response.json()
|
||||||
|
|
||||||
response = client.post(
|
response = client.post("/items/", json={"name": "test", "id": 1})
|
||||||
"/items/", json={"name": "test", "id": 1}
|
|
||||||
)
|
|
||||||
item = Item(**response.json())
|
item = Item(**response.json())
|
||||||
assert item.pk is not None
|
assert item.pk is not None
|
||||||
|
|
||||||
@ -107,7 +105,7 @@ def test_all_endpoints():
|
|||||||
)
|
)
|
||||||
item = Item(**response.json())
|
item = Item(**response.json())
|
||||||
assert len(item.categories) == 1
|
assert len(item.categories) == 1
|
||||||
assert item.categories[0].name == 'test cat'
|
assert item.categories[0].name == "test cat"
|
||||||
|
|
||||||
client.post(
|
client.post(
|
||||||
"/items/add_category/", json={"item": item.dict(), "category": category2}
|
"/items/add_category/", json={"item": item.dict(), "category": category2}
|
||||||
@ -117,9 +115,21 @@ def test_all_endpoints():
|
|||||||
items = [Item(**item) for item in response.json()]
|
items = [Item(**item) for item in response.json()]
|
||||||
assert items[0] == item
|
assert items[0] == item
|
||||||
assert len(items[0].categories) == 2
|
assert len(items[0].categories) == 2
|
||||||
assert items[0].categories[0].name == 'test cat'
|
assert items[0].categories[0].name == "test cat"
|
||||||
assert items[0].categories[1].name == 'test cat2'
|
assert items[0].categories[1].name == "test cat2"
|
||||||
|
|
||||||
response = client.get("/docs/")
|
response = client.get("/docs/")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b'<title>FastAPI - Swagger UI</title>' in response.content
|
assert b"<title>FastAPI - Swagger UI</title>" in response.content
|
||||||
|
|
||||||
|
|
||||||
|
def test_schema_modification():
|
||||||
|
schema = Item.schema()
|
||||||
|
assert schema["properties"]["categories"]["type"] == "array"
|
||||||
|
assert schema["properties"]["categories"]["title"] == "Categories"
|
||||||
|
|
||||||
|
|
||||||
|
def test_schema_gen():
|
||||||
|
schema = app.openapi()
|
||||||
|
assert "Category" in schema["components"]["schemas"]
|
||||||
|
assert "Item" in schema["components"]["schemas"]
|
||||||
|
|||||||
@ -437,4 +437,3 @@ async def test_start_and_end_filters():
|
|||||||
|
|
||||||
users = await User.objects.filter(name__endswith="igo").all()
|
users = await User.objects.filter(name__endswith="igo").all()
|
||||||
assert len(users) == 2
|
assert len(users) == 2
|
||||||
|
|
||||||
|
|||||||
@ -119,9 +119,7 @@ def test_all_endpoints():
|
|||||||
items = response.json()
|
items = response.json()
|
||||||
assert len(items) == 0
|
assert len(items) == 0
|
||||||
|
|
||||||
client.post(
|
client.post("/items/", json={"name": "test_2", "id": 2, "category": category})
|
||||||
"/items/", json={"name": "test_2", "id": 2, "category": category}
|
|
||||||
)
|
|
||||||
response = client.get("/items/")
|
response = client.get("/items/")
|
||||||
items = response.json()
|
items = response.json()
|
||||||
assert len(items) == 1
|
assert len(items) == 1
|
||||||
@ -132,4 +130,3 @@ def test_all_endpoints():
|
|||||||
|
|
||||||
response = client.get("/docs/")
|
response = client.get("/docs/")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user