diff --git a/fastapi_crudrouter/core/_utils.py b/fastapi_crudrouter/core/_utils.py index ef3562e4..4ac482e5 100644 --- a/fastapi_crudrouter/core/_utils.py +++ b/fastapi_crudrouter/core/_utils.py @@ -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 diff --git a/tests/test_schema.py b/tests/test_schema.py new file mode 100644 index 00000000..5edaf6a8 --- /dev/null +++ b/tests/test_schema.py @@ -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__