From cd542cd9e7256d09c450bc21382eff0225bc58d5 Mon Sep 17 00:00:00 2001 From: Clinton James Date: Tue, 5 Oct 2021 19:02:12 -0500 Subject: [PATCH] Add #110 schema_factory ignores allow_mutation=False schema_factory currently ignores the primary field 'id'. This allows any field annotated with allow_mutation=False the same ability. --- fastapi_crudrouter/core/_utils.py | 3 +-- tests/test_schema.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 tests/test_schema.py diff --git a/fastapi_crudrouter/core/_utils.py b/fastapi_crudrouter/core/_utils.py index 801ba563..096fa30f 100644 --- a/fastapi_crudrouter/core/_utils.py +++ b/fastapi_crudrouter/core/_utils.py @@ -27,11 +27,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__