diff --git a/CHANGELOG.md b/CHANGELOG.md index 79042d818..9fe7ecc77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,34 @@ here.** ## [Unreleased] +## [1.2.1] — Python only + +_PyPI `1.2.1` only. npm, NuGet and Maven Central stay at `1.2.0` (`8.2.0`): none of them has a +product change. No vocabulary or `metamodelVersion` change._ + +### Security + +- **The generated Python routes passed raw request input to the repository.** The FastAPI + create, PATCH and PUT handlers validated the body against `Create` / `Patch` + and then handed the repository the raw body, not what was validated. Pydantic ignores a key a + model does not declare, so a client could write fields it must never touch: a client-sent `id` + on create (or on update, changing the row's identity), a `@mutability: readOnly` field, and any + made-up field such as `isAdmin` all reached the repository. Found by an external review of + 1.2.0. Every Python adopter serving the generated routes should upgrade and regenerate; an + ejected (owned) router needs the same change made by hand. + + The repository now receives only the validated input, as the TypeScript routes already do. + Each handler validates the body, then keeps only the keys the input model validated, at every + depth (a nested value object included), before any repository call, in the plain and the TPH + per-subtype handlers. A field the entity does not declare, a server-generated id, a `readOnly` + or derived field on create, and the id, `readOnly` and `writeOnce` fields on update are all + dropped silently. None of them is answered with a 400, so a client that sends an extra harmless + field keeps working; a malformed value on a field the client may set is still + `400 {"error": "validation"}`. An assigned key is still the caller's on create. Values are the + body's own, so a repository receives the same value types as in 1.2.0. + + Gated by `server/python/tests/integration/test_generated_router_validated_input.py`. + ## [1.2.0] — 2026-10-30 _npm `1.2.0` (full lockstep across all 14 `@metaobjectsdev/*` publish candidates)._ diff --git a/docs/features/generated-mutations.md b/docs/features/generated-mutations.md index 57aa6f409..3a367bbb6 100644 --- a/docs/features/generated-mutations.md +++ b/docs/features/generated-mutations.md @@ -41,7 +41,9 @@ but the semantics are one contract — call them **PATCH-1..7** (FR-035): simply omits every non-settable field, and Zod drops unknown keys — so `safeParse` succeeds and the key never reaches the `SET`. The TPH discriminator is additionally deleted by hand after parse, the same convention. A malformed - *value* on a settable key is still `400 {"error":"validation"}`. + *value* on a settable key is still `400 {"error":"validation"}`. A key the entity + does not declare at all is dropped the same way. The Python routes hand the + repository only the keys their input model validated. This matters for `writeOnce` specifically: the generated edit form submits EVERY registered field (`handleSubmit` passes all values, and 0.19.2 switched the resolver to `UpdateSchema` on edit rather than diff-and-omit), so rejecting a @@ -109,10 +111,10 @@ repo.patch(id) { it[Authors.bio] = "…" } // touches only what you set the controller binds the partial JSON body via `Patch.fromJson` (presence-tracked) and delegates to `repository.patch`, so an omitted key is never written. -**Python** — the generated FastAPI route binds the raw JSON body (the set of -*present* keys — the tristate signal) and validates the present values against the -generated `Patch` model before delegating to the runtime `update`, which -`SET`s only those keys. +**Python** — the generated FastAPI route binds the JSON body (the set of +*present* keys — the tristate signal), validates the present values against the +generated `Patch` model, and hands the repository only the keys that model +validated. The runtime `update` then `SET`s only those keys. **C#** — the generated route runs a per-field merge loop over the present JSON keys (the app's configured `JsonSerializerOptions`), writing only the columns the body diff --git a/examples/showcase/.metaobjects/.gen-state/.hashes.json b/examples/showcase/.metaobjects/.gen-state/.hashes.json index 8a7a4f6bb..131a0265b 100644 --- a/examples/showcase/.metaobjects/.gen-state/.hashes.json +++ b/examples/showcase/.metaobjects/.gen-state/.hashes.json @@ -10,7 +10,7 @@ "generated/python/__init__.py": "daa04cec586768866d0996c779f62073e6773fe63750ff668327991fac2fd0f5", "generated/python/subscriber_filter_allowlist.py": "b0414cc95ff3982c80a0e1a06839f090c0b423e41f5a94275df93426cea09df5", "generated/python/subscriber_names.py": "269637a02bf9b46f55658b58cb42f83196c03a877e1e2101337847fd406c1561", - "generated/python/subscriber_router.py": "5a354e50b1aab48239a4249b670a6d2351c1816cbca6d710ea688f252e9612e5", + "generated/python/subscriber_router.py": "f3c81be3c07a10a94afb6a6a0f87eb84b9bca40ead03dd989ebd019f71b97dfc", "generated/ts/Subscriber.queries.ts": "9f1a459c57fafc98bf074b4443b506d4a1e7023ed188f293021ae89c298d4562", "generated/ts/Subscriber.routes.ts": "147d32ae658f985e024c3de6e8894ca7359f767eeaa765cec70756802e3bd300", "generated/ts/Subscriber.ts": "a78c17f915222ee108290c28468313d77f77b5cff438f485324409302e389a96", diff --git a/examples/showcase/generated/python/subscriber_router.py b/examples/showcase/generated/python/subscriber_router.py index 47aaaab7c..fe148c5e3 100644 --- a/examples/showcase/generated/python/subscriber_router.py +++ b/examples/showcase/generated/python/subscriber_router.py @@ -91,6 +91,26 @@ async def _json_body(request: Request) -> dict[str, Any] | None: return None +def _validated_input(valid: BaseModel, raw: dict[str, Any]) -> dict[str, Any]: + """GENERATED — *raw* cut down to the keys *valid* validated, at every depth. A key the + input model does not declare (an undeclared name, a server-generated id, a readOnly field) + is dropped silently and never reaches the repository; values stay the body's own.""" + return { + _k: _validated_value(getattr(valid, _k), raw[_k]) + for _k in valid.model_fields_set + } + + +def _validated_value(valid: Any, raw: Any) -> Any: + if isinstance(valid, BaseModel) and isinstance(raw, dict): + return _validated_input(valid, raw) + if isinstance(valid, list) and isinstance(raw, list): + return [_validated_value(v, r) for v, r in zip(valid, raw)] + if isinstance(valid, dict) and isinstance(raw, dict): + return {k: _validated_value(v, raw[k]) for k, v in valid.items() if k in raw} + return raw + + _FROZEN_FIELDS: frozenset[str] = frozenset() @@ -198,9 +218,13 @@ def create_subscriber( # FR-036: run the generated field constraints (length / pattern / # numeric bounds) before persisting; a violation is the cross-port 400. try: - SubscriberCreate(**dto) + _valid = SubscriberCreate(**dto) except ValidationError: return JSONResponse(status_code=400, content={"error": "validation"}) + # SECURITY (1.2.1): only what the input model validated reaches the repository. A key + # it does not declare (an undeclared name, a server-generated id, a readOnly or derived + # field, a frozen one on update) is dropped silently, never 400'd. + dto = _validated_input(_valid, dto) # #203/ADR-0045: stamp @autoSet columns (server-owned; caller ignored). _asnow = _dt.datetime.now(_dt.timezone.utc) dto["createdAt"] = _asnow @@ -239,9 +263,13 @@ def update_subscriber( # FR-036: validate PRESENT, non-null values with the create rules (the # all-optional patch model never fires required-checks on ABSENT fields). try: - SubscriberPatch(**dto) + _valid = SubscriberPatch(**dto) except ValidationError: return JSONResponse(status_code=400, content={"error": "validation"}) + # SECURITY (1.2.1): only what the input model validated reaches the repository. A key + # it does not declare (an undeclared name, a server-generated id, a readOnly or derived + # field, a frozen one on update) is dropped silently, never 400'd. + dto = _validated_input(_valid, dto) dto.pop("createdAt", None) # onCreate @autoSet is write-once (server-owned) try: saved = repo.update(subscriber_id, dto) diff --git a/examples/showcase/site-payload.json b/examples/showcase/site-payload.json index cc09d97e2..237e16a88 100644 --- a/examples/showcase/site-payload.json +++ b/examples/showcase/site-payload.json @@ -1,7 +1,7 @@ { "registries": { "npm": "1.2.0", - "pypi": "1.2.0", + "pypi": "1.2.1", "nuget": "1.2.0", "maven": "8.2.0", "metamodel": "1.2" diff --git a/server/python/pyproject.toml b/server/python/pyproject.toml index 2854f66ca..2366d3f4b 100644 --- a/server/python/pyproject.toml +++ b/server/python/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "metaobjects" -version = "1.2.0" +version = "1.2.1" description = "Cross-language metadata standard: declare typed entities once, generate idiomatic drift-checked code across languages — Python port." readme = "README.md" requires-python = ">=3.11" diff --git a/server/python/src/metaobjects/codegen/KNOWN_GAPS.md b/server/python/src/metaobjects/codegen/KNOWN_GAPS.md index 2cb454b8d..1b300bfa6 100644 --- a/server/python/src/metaobjects/codegen/KNOWN_GAPS.md +++ b/server/python/src/metaobjects/codegen/KNOWN_GAPS.md @@ -36,9 +36,9 @@ The generated router takes `dto: dict[str, Any]` for `POST` / `PATCH` / (`from . import , Patch`) and constructs them to enforce field constraints over HTTP — POST validates `(**dto)`, PATCH validates the all-optional `Patch(**dto)` (present values only) → `400 {"error":"validation"}`. -The repository seam still receives the original `dict` (preserving the FR-035 -present-key tristate + wire values), so the strong-typing simplification below -remains for the repo/response types; only the validation import was added. +The repository seam receives only the keys the input model validated, as a `dict` +(the FR-035 present-key tristate + wire values are kept), so the strong-typing +simplification below remains for the repo/response types. This keeps the router module decoupled from sibling generated files (the entity-model generator emits one `@dataclass`/Pydantic model per diff --git a/server/python/src/metaobjects/codegen/generators/router_generator.py b/server/python/src/metaobjects/codegen/generators/router_generator.py index 227346832..9e0586bcd 100644 --- a/server/python/src/metaobjects/codegen/generators/router_generator.py +++ b/server/python/src/metaobjects/codegen/generators/router_generator.py @@ -434,10 +434,9 @@ def _frozen_names(entity: MetaObject) -> list[str]: ADR-0045: the OUTERMOST generated write artifact enforces the mode, and for FastAPI that is the ROUTER, not the ``Patch`` model — the handler binds - ``dto`` (the raw body as a dict) and passes THAT to the repository, so a - field merely absent from the patch model still reaches the row. @autoSet gets - away with it because the router OVERWRITES its key server-side; a writeOnce - field has no server value to overwrite, so it must be popped explicitly.""" + ``dto`` as a dict. Since 1.2.1 the router hands the repository only the keys the + patch model validated (:func:`_validated_input_lines`), which already drops these; + the explicit pop stays as the FR-037 seam an owned router edits.""" return [ f.name for f in entity.fields() @@ -461,6 +460,49 @@ def _frozen_strip_lines(entity: MetaObject, indent: str = " ") -> list[str]: ] +#: 1.2.1 security fix. The request body cut down to what the input model validated, emitted +#: once per writable router. 1.2.0 validated the body and then handed the RAW body to the +#: repository, so a client-sent server-generated id, a ``readOnly`` field or a name the entity +#: does not declare (an ``isAdmin``) reached the write. Pydantic ignores a key a model does not +#: declare, and the input models leave out every field a client may not set, so keeping only +#: the keys the model validated (at every depth: a nested value object included) drops all of +#: them silently, as the TypeScript routes do. The values stay the body's own (decimal-exact) +#: values, so a repository receives the same value types as in 1.2.0. +_VALIDATED_INPUT_LINES: list[str] = [ + "def _validated_input(valid: BaseModel, raw: dict[str, Any]) -> dict[str, Any]:", + ' """GENERATED — *raw* cut down to the keys *valid* validated, at every depth. A key the', + " input model does not declare (an undeclared name, a server-generated id, a readOnly field)", + ' is dropped silently and never reaches the repository; values stay the body\'s own."""', + " return {_k: _validated_value(getattr(valid, _k), raw[_k]) for _k in valid.model_fields_set}", + "", + "", + "def _validated_value(valid: Any, raw: Any) -> Any:", + " if isinstance(valid, BaseModel) and isinstance(raw, dict):", + " return _validated_input(valid, raw)", + " if isinstance(valid, list) and isinstance(raw, list):", + " return [_validated_value(v, r) for v, r in zip(valid, raw)]", + " if isinstance(valid, dict) and isinstance(raw, dict):", + " return {k: _validated_value(v, raw[k]) for k, v in valid.items() if k in raw}", + " return raw", +] + + +def _validated_input_lines(model: str, *comment: str) -> list[str]: + """Validate ``dto`` against the input *model*, then rebind it to what the model validated + (see :data:`_VALIDATED_INPUT_LINES`): the repository never sees any other key.""" + return [ + *(f" # {line}" for line in comment), + " try:", + f" _valid = {model}(**dto)", + " except ValidationError:", + ' return JSONResponse(status_code=400, content={"error": "validation"})', + " # SECURITY (1.2.1): only what the input model validated reaches the repository. A key", + " # it does not declare (an undeclared name, a server-generated id, a readOnly or derived", + " # field, a frozen one on update) is dropped silently, never 400'd.", + " dto = _validated_input(_valid, dto)", + ] + + def _auto_set_split(entity: MetaObject) -> tuple[list[MetaField], list[MetaField]]: """The entity's ``@autoSet`` temporal fields split into ``(onCreate, onUpdate)``. @@ -1083,12 +1125,11 @@ def _emit_route_handler( ") -> Any:", *_DECIMAL_COERCE, *denied_strip, - " # FR-036: run the generated field constraints (length / pattern /", - " # numeric bounds) before persisting; a violation is the cross-port 400.", - " try:", - f" {model_name}(**dto)", - " except ValidationError:", - ' return JSONResponse(status_code=400, content={"error": "validation"})', + *_validated_input_lines( + model_name, + "FR-036: run the generated field constraints (length / pattern /", + "numeric bounds) before persisting; a violation is the cross-port 400.", + ), # #203/ADR-0045: the generated router (the API surface) stamps @autoSet columns # ABOVE the consumer repo seam — onCreate + onUpdate both stamped from one instant # (a fresh row's createdAt == updatedAt), ignoring any caller-supplied value. @@ -1114,12 +1155,11 @@ def _emit_route_handler( " for _k in _REQUIRED_FIELDS:", " if _k in dto and dto[_k] is None:", ' return JSONResponse(status_code=400, content={"error": "validation"})', - " # FR-036: validate PRESENT, non-null values with the create rules (the", - " # all-optional patch model never fires required-checks on ABSENT fields).", - " try:", - f" {patch_model}(**dto)", - " except ValidationError:", - ' return JSONResponse(status_code=400, content={"error": "validation"})', + *_validated_input_lines( + patch_model, + "FR-036: validate PRESENT, non-null values with the create rules (the", + "all-optional patch model never fires required-checks on ABSENT fields).", + ), # #203/ADR-0045: bump onUpdate @autoSet columns (server-owned) on EVERY patch, # above the repo seam — a server-inserted present key (compatible with the PATCH # present-key tristate); onCreate columns are never touched here. @@ -1468,6 +1508,9 @@ def _render_tph_router( parts.extend(_JSON_BODY_LINES) parts.append("") parts.append("") + parts.extend(_VALIDATED_INPUT_LINES) + parts.append("") + parts.append("") # FR-037 R1 — the names a PATCH must never write. Emitted even when EMPTY so # the generated module always carries the seam (an entity that later gains a # writeOnce field changes one literal, not the module's shape). @@ -1547,11 +1590,9 @@ def list_sig(fn: str, route: str) -> list[str]: parts.append(") -> Any:") parts.extend(_DECIMAL_COERCE) parts.extend(denied_strip_tph) - parts.append(" # FR-036: run the subtype's field constraints before persisting.") - parts.append(" try:") - parts.append(f" {sub}Create(**dto)") - parts.append(" except ValidationError:") - parts.append(' return JSONResponse(status_code=400, content={"error": "validation"})') + parts.extend(_validated_input_lines( + f"{sub}Create", "FR-036: run the subtype's field constraints before persisting." + )) # #203/ADR-0045: stamp @autoSet columns ABOVE the repo seam (empty for a # non-@autoSet hierarchy — byte-identical output). parts.extend(create_autoset) @@ -1584,11 +1625,9 @@ def list_sig(fn: str, route: str) -> list[str]: parts.append(" for _k in _REQUIRED_FIELDS:") parts.append(" if _k in dto and dto[_k] is None:") parts.append(' return JSONResponse(status_code=400, content={"error": "validation"})') - parts.append(" # FR-036: validate PRESENT, non-null values with the subtype's create rules.") - parts.append(" try:") - parts.append(f" {sub}Patch(**dto)") - parts.append(" except ValidationError:") - parts.append(' return JSONResponse(status_code=400, content={"error": "validation"})') + parts.extend(_validated_input_lines( + f"{sub}Patch", "FR-036: validate PRESENT, non-null values with the subtype's create rules." + )) # #203/ADR-0045: pop the write-once onCreate column(s), bump onUpdate — ABOVE # the repo seam (empty for a non-@autoSet hierarchy — byte-identical output). parts.extend(update_autoset) @@ -1857,6 +1896,9 @@ def render_router( parts.extend(_JSON_BODY_LINES) parts.append("") parts.append("") + parts.extend(_VALIDATED_INPUT_LINES) + parts.append("") + parts.append("") # FR-037 R1 — the names a PATCH must never write. Emitted even when EMPTY so # the generated module always carries the seam (an entity that later gains a # writeOnce field changes one literal, not the module's shape). @@ -1892,8 +1934,8 @@ def render_router( create_autoset=create_autoset, update_autoset=update_autoset, # FR-037 R1 — ADR-0045: the ROUTER is the outermost write artifact for - # FastAPI (the handler binds the RAW body dict and hands THAT to the - # repository), so the strip has to live here, not in Patch. + # FastAPI (the handler binds the body as a dict), so the strip lives here, + # not in Patch. frozen_strip=_frozen_strip_lines(entity), denied_strip=_denied_strip_lines(denied_names), ) diff --git a/server/python/tests/integration/test_generated_router_validated_input.py b/server/python/tests/integration/test_generated_router_validated_input.py new file mode 100644 index 000000000..b43ed21c0 --- /dev/null +++ b/server/python/tests/integration/test_generated_router_validated_input.py @@ -0,0 +1,272 @@ +"""1.2.1 security fix: a generated create / update hands the repository ONLY the validated input. + +The 1.2.0 router validated the request body against ``Create`` / ``Patch`` and +then passed the RAW body to the repository. Pydantic ignores a key a model does not declare, and +the input models leave out the server-owned fields, so a client-sent ``id``, a ``readOnly`` field +and a made-up ``isAdmin`` all reached the write. Now the repository receives only what the input +model validated, at every depth, as with the TypeScript routes: a name the entity does not +declare, a server-generated id, a ``readOnly`` field and the id on update are all dropped +silently. None of them is a 400: a client that sends an extra harmless field keeps working. + +The model is generated with the real Python codegen (``run_gen``: entity + filter-allowlist + +routes), the emitted package is imported unmodified, and the only hand-written piece is a +repository that records what it was handed. No database: the router is the artifact, as in the +other generated-router lanes. +""" +from __future__ import annotations + +import importlib +import json +import sys +import tempfile +import uuid +from pathlib import Path +from typing import Any + +import pytest +from fastapi import FastAPI +from fastapi.testclient import TestClient + +from metaobjects import MetaDataLoader +from metaobjects.codegen.config import GenConfig +from metaobjects.codegen.generators.entity_model import entity_model +from metaobjects.codegen.generators.filter_allowlist_generator import filter_allowlist_generator +from metaobjects.codegen.generators.router_generator import router_generator +from metaobjects.codegen.runner import run_gen + +from .generated_tph_app import build_generated_tph_app + +_META = { + "metadata.root": { + "package": "acme::secfix", + "children": [ + {"object.value": { + "name": "Address", + "children": [ + {"field.string": {"name": "city", "@maxLength": 40}}, + ], + }}, + {"object.entity": { + "name": "Member", + "children": [ + {"source.rdb": {"@table": "members"}}, + {"field.long": {"name": "id"}}, + {"field.string": {"name": "name", "@required": True, "@maxLength": 80}}, + {"field.string": {"name": "role", "@maxLength": 20, "@mutability": "readOnly"}}, + {"field.string": {"name": "tenant", "@maxLength": 20, "@mutability": "writeOnce"}}, + {"field.object": {"name": "address", "@objectRef": "Address", "@storage": "jsonb"}}, + {"field.timestamp": {"name": "createdAt", "@autoSet": "onCreate"}}, + {"identity.primary": {"name": "id", "@fields": "id", "@generation": "increment"}}, + ], + }}, + {"object.entity": { + "name": "Account", + "children": [ + {"source.rdb": {"@table": "accounts"}}, + {"field.string": {"name": "code", "@maxLength": 20}}, + {"field.string": {"name": "label", "@maxLength": 80}}, + {"identity.primary": {"name": "code", "@fields": "code"}}, + ], + }}, + ], + } +} + + +class _RecordingRepo: + """The generated repository seam, recording every write it is handed.""" + + def __init__(self, pk: str) -> None: + self.pk = pk + self.writes: list[tuple[str, dict[str, Any]]] = [] + + def create(self, dto: Any) -> Any: + self.writes.append(("create", dict(dto))) + return {self.pk: 1, **dto} + + def update(self, id: Any, dto: Any) -> Any | None: + self.writes.append(("update", dict(dto))) + return {self.pk: id, **dto} + + +def _generate_package() -> str: + """Run the real Python codegen for ``_META`` into a temp package; return its name.""" + meta_dir = Path(tempfile.mkdtemp(prefix="secfix-meta-")) + (meta_dir / "meta.json").write_text(json.dumps(_META)) + loaded = MetaDataLoader.from_directory(str(meta_dir)) + assert not loaded.errors, "; ".join(f"{e.code}: {e.message}" for e in loaded.errors) + pkg = f"secfix_{uuid.uuid4().hex[:8]}" + root = Path(tempfile.mkdtemp(prefix="secfix-gen-")) + run_gen( + GenConfig(out_dir=str(root / pkg)), + loaded.root, + generators=[entity_model(), filter_allowlist_generator(), router_generator()], + ) + sys.path.insert(0, str(root)) + return pkg + + +_PKG = _generate_package() + + +def _client(router_module: str, pk: str) -> tuple[TestClient, _RecordingRepo, str]: + mod = importlib.import_module(f"{_PKG}.{router_module}") + repo = _RecordingRepo(pk) + app = FastAPI() + app.include_router(mod.router) + app.dependency_overrides[mod.get_repository] = lambda: repo + return TestClient(app), repo, mod.router.prefix + + +@pytest.fixture() +def member() -> tuple[TestClient, _RecordingRepo, str]: + return _client("member_router", "id") + + +@pytest.fixture() +def account() -> tuple[TestClient, _RecordingRepo, str]: + return _client("account_router", "code") + + +# --- the reported defect: a client-sent id and a made-up isAdmin reach the repository ------- + + +def test_create_never_hands_the_repository_a_client_id_or_an_undeclared_field(member) -> None: + client, repo, base = member + resp = client.post(base, json={"id": 999, "name": "Mallory", "isAdmin": True}) + assert resp.status_code == 201, resp.text + [(op, written)] = repo.writes + assert op == "create" + assert set(written) == {"name", "createdAt"} # createdAt is the router's own stamp + assert written["name"] == "Mallory" + + +@pytest.mark.parametrize("method", ["PATCH", "PUT"]) +def test_update_never_hands_the_repository_an_undeclared_field(member, method: str) -> None: + client, repo, base = member + resp = client.request(method, f"{base}/1", json={"name": "M", "isAdmin": True}) + assert resp.status_code == 200, resp.text + assert repo.writes == [("update", {"name": "M"})] + + +def test_an_undeclared_key_inside_a_value_object_never_reaches_the_repository(member) -> None: + client, repo, base = member + resp = client.post(base, json={"name": "Mallory", "address": {"city": "Oslo", "isAdmin": True}}) + assert resp.status_code == 201, resp.text + [(_op, written)] = repo.writes + assert written["address"] == {"city": "Oslo"} + + +def test_a_malformed_value_on_a_settable_field_is_still_a_400(member) -> None: + client, repo, base = member + resp = client.post(base, json={"name": "x" * 81, "isAdmin": True}) + assert resp.status_code == 400, resp.text + assert resp.json() == {"error": "validation"} + assert repo.writes == [] + + +# --- declared but not settable: dropped, never 400'd, and never handed to the repository ---- + + +def test_create_strips_the_client_id_and_a_read_only_field(member) -> None: + client, repo, base = member + resp = client.post( + base, + json={"id": 999, "name": "Mallory", "role": "admin", "tenant": "t1", + "address": {"city": "Oslo"}}, + ) + assert resp.status_code == 201, resp.text + [(op, written)] = repo.writes + assert op == "create" + # The server generates the id and owns `role`; `createdAt` is the router's own stamp. + assert set(written) == {"name", "tenant", "address", "createdAt"} + assert written["name"] == "Mallory" + assert written["tenant"] == "t1" # writeOnce is settable on create + assert written["address"] == {"city": "Oslo"} + + +@pytest.mark.parametrize("method", ["PATCH", "PUT"]) +def test_update_never_hands_the_repository_the_id_or_a_frozen_field(member, method: str) -> None: + client, repo, base = member + resp = client.request( + method, + f"{base}/1", + json={"id": 999, "name": "M2", "role": "admin", "tenant": "t2", "createdAt": "2000-01-01T00:00:00Z"}, + ) + assert resp.status_code == 200, resp.text + assert repo.writes == [("update", {"name": "M2"})] + + +def test_update_passes_a_present_null_through_as_a_clear(member) -> None: + """The PATCH present-key tristate is unchanged: an explicit null on a nullable field clears it.""" + client, repo, base = member + resp = client.patch(f"{base}/1", json={"address": None}) + assert resp.status_code == 200, resp.text + assert repo.writes == [("update", {"address": None})] + + +def test_an_assigned_key_is_still_the_callers_on_create_and_route_owned_on_update(account) -> None: + client, repo, base = account + created = client.post(base, json={"code": "ACME", "label": "Acme"}) + assert created.status_code == 201, created.text + updated = client.patch(f"{base}/ACME", json={"code": "EVIL", "label": "Acme 2"}) + assert updated.status_code == 200, updated.text + assert repo.writes == [ + ("create", {"code": "ACME", "label": "Acme"}), + ("update", {"label": "Acme 2"}), + ] + + +# --- TPH: the per-subtype create / update handlers are a separate code path ----------------- + + +def _find_tph_dir() -> Path: + cur = Path(__file__).resolve() + while cur != cur.parent: + candidate = cur / "fixtures" / "api-contract-conformance" / "tph" + if candidate.is_dir(): + return candidate + cur = cur.parent + raise RuntimeError("fixtures/api-contract-conformance/tph not found") + + +@pytest.fixture() +def tph() -> tuple[TestClient, list[tuple[str, str, dict[str, Any]]]]: + app, repo = build_generated_tph_app(_find_tph_dir()) + writes: list[tuple[str, str, dict[str, Any]]] = [] + create, update = repo.create, repo.update + + def recording_create(subtype: str, dto: Any) -> Any: + writes.append(("create", subtype, dict(dto))) + return create(subtype, dto) + + def recording_update(subtype: str, id: int, dto: Any) -> Any: + writes.append(("update", subtype, dict(dto))) + return update(subtype, id, dto) + + repo.create, repo.update = recording_create, recording_update + return TestClient(app), writes + + +def test_tph_subtype_create_drops_the_client_id_and_an_undeclared_field(tph) -> None: + client, writes = tph + resp = client.post( + "/api/auths/bridge", json={"id": 999, "reference": "R-1", "quantity": 2, "isAdmin": True} + ) + assert resp.status_code == 201, resp.text + [(op, subtype, written)] = writes + assert (op, subtype) == ("create", "Bridge") + assert "id" not in written + assert "isAdmin" not in written + assert {"reference", "quantity"} <= set(written) + + +def test_tph_subtype_update_drops_the_id_and_an_undeclared_field(tph) -> None: + client, writes = tph + # The store is unseeded, so the answer is a 404; what matters is what the write was handed. + client.patch("/api/auths/bridge/1", json={"id": 999, "quantity": 3, "isAdmin": True}) + [(op, subtype, written)] = writes + assert (op, subtype) == ("update", "Bridge") + assert "id" not in written + assert "isAdmin" not in written + assert written["quantity"] == 3 diff --git a/server/python/uv.lock b/server/python/uv.lock index 299970581..a3b21881c 100644 --- a/server/python/uv.lock +++ b/server/python/uv.lock @@ -399,7 +399,7 @@ wheels = [ [[package]] name = "metaobjects" -version = "1.2.0" +version = "1.2.1" source = { editable = "." } dependencies = [ { name = "pyyaml" },