Closes: #390 - Schema validation/apply endpoints#454
Closes: #390 - Schema validation/apply endpoints#454bctiemann wants to merge 6 commits into389-upgrade-executorfrom
Conversation
# Conflicts: # netbox_custom_objects/tests/test_schema_format.py
|
@claude /review |
|
Claude finished @bctiemann's task in 1m 46s —— View job Code Review: Schema validation/apply endpoints (#390)
OverviewThis PR adds two REST API endpoints (
|
|
Added recommended tests and addressed all code-level suggestions. Also added a guard against |
Closes: #390
Adds two new API endpoints for previewing and applying COT schema documents.
New endpoints
POST/api/plugins/custom-objects/schema/preview/POST/api/plugins/custom-objects/schema/apply/POST /schema/preview/Request body — a raw COT schema document:
{ "schema_version": "1", "types": [ ... ] }Response
200:{ "diffs": [ { "slug": "my-cot", "name": "my_cot", "is_new": false, "has_changes": true, "has_destructive_changes": false, "cot_changes": { "description": ["old", "new"] }, "field_changes": [ { "op": "add", "schema_id": 5, "db_name": null, "schema_def": { "id": 5, "name": "new_field", "type": "text" } } ], "warnings": [] } ] }Preview never modifies the database and never returns
409— destructive changes are flagged viahas_destructive_changes: trueso the caller can decide whether to proceed.POST /schema/apply/Request body — schema document wrapped with an optional
allow_destructiveflag:{ "allow_destructive": false, "schema": { "schema_version": "1", "types": [ ... ] } }Response
200(success):{ "applied": true, "diffs": [ ... ] }Response
409 Conflict— document containsREMOVEoperations andallow_destructivewas not set:{ "error": "destructive_changes", "detail": "Schema contains destructive field removals for COT(s): my-cot. Pass allow_destructive=True to apply them.", "destructive_slugs": ["my-cot"] }Response
400 Bad Request— circular COT dependency, unresolvable FK / choice-set reference, missingschemakey, or invalid schema document structure:{ "error": "circular_dependency" | "unresolvable_reference", "detail": "..." }Implementation notes
diff_document) and executor (apply_document) from issues Custom Object Type state comparator functionality #387 and Custom Object Type upgrade executor #389.cot_schema_v1.jsonusingjsonschema.Draft202012Validatorbefore any DB access. Errors return400with{"schema_errors": [{"path": [...], "message": "..."}]}. Degrades gracefully ifjsonschemais not installed._ignore_model_permissions = True; authentication is still enforced.schema_documentis persisted on each affected COT after a successful apply, so tombstones are available for future export/diff cycles.Tests —
tests/test_schema_api.py(24 tests)SchemaPreviewTestCase(10): 200 response, diff keys present, no DB mutation, field-add/alter detection, destructive preview returns 200 (not 409), auth enforcement, invalid schema returns 400.SchemaApplyTestCase(14): new COT creation, field add, 409 guard without flag, 200 withallow_destructive, missing/non-dictschemakey returns 400, unresolvable FK/choice-set returns 400, auth enforcement,schema_documentpersisted.