Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have schema_factory ignore fields when allow_mutation=False #113

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions fastapi_crudrouter/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ def schema_factory(
"""
Is used to create a CreateSchema which does not contain pk
"""

fields = {
f.name: (f.type_, ...)
for f in schema_cls.__fields__.values()
if f.name != pk_field_name
if f.name != pk_field_name and f.field_info.allow_mutation
}

name = schema_cls.__name__ + name
Expand Down
19 changes: 19 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pydantic import BaseModel, Field
from fastapi_crudrouter.core._utils import schema_factory


class Onion(BaseModel):
id: int = Field(primary_key=True)
variety: str
expire_on: str = Field(allow_mutation=False)

class Config:
validate_assignment = True


class TestAllowMutation:
def test_schema_factory_update(self):
"""Field annotation allow_mutation=False are removed from schema."""
schema = schema_factory(Onion, "Update")
assert "variety" in schema.__fields__
assert "expire_on" not in schema.__fields__