Skip to content

Commit 3740f8d

Browse files
committed
Bugfix
1 parent 47984cf commit 3740f8d

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

backend/datamodel.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import hashlib
44
import json
55
from datetime import datetime
6-
from typing import Any, Dict, Optional
6+
from typing import Any, Optional
7+
78
from pydantic import BaseModel, Field, model_validator
89

910
ALLOWED_FIELD_TYPES = {
@@ -15,10 +16,11 @@
1516
"datetime",
1617
}
1718

19+
1820
def compute_schema_hash(
19-
name: Optional[str],
20-
version: Optional[str],
21-
content: Optional[Dict[str, Any]],
21+
name: str | None,
22+
version: str | None,
23+
content: dict[str, Any] | None,
2224
) -> str:
2325
"""
2426
Compute a deterministic SHA-256 hash from the canonical JSON of {name, version, content}.
@@ -32,16 +34,16 @@ def compute_schema_hash(
3234

3335
class SchemaDefinition(BaseModel):
3436
id: str = Field(..., description="Unique identifier for the schema (content hash)")
35-
name: Optional[str] = Field(
37+
name: str | None = Field(
3638
None,
3739
description="Human-readable name of the schema",
3840
min_length=3,
3941
)
40-
version: Optional[str] = Field(None, description="Version of the schema")
41-
content: Optional[Dict[str, Any]] = Field(
42+
version: str | None = Field(None, description="Version of the schema")
43+
content: dict[str, Any] | None = Field(
4244
None, description="The actual schema content as a dictionary"
4345
)
44-
updated_at: Optional[datetime] = Field(
46+
updated_at: datetime | None = Field(
4547
None, description="Timestamp of the last update"
4648
)
4749

backend/main.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11

22
# main.py
33
from datetime import datetime
4-
from typing import Any, Dict, Optional
4+
from typing import Any, Optional
55

6+
from database import schemas_collection
7+
from datamodel import ALLOWED_FIELD_TYPES, SchemaDefinition, compute_schema_hash
68
from fastapi import FastAPI, HTTPException
79
from fastapi.encoders import jsonable_encoder
810
from fastapi.middleware.cors import CORSMiddleware
911

10-
from database import schemas_collection
11-
from datamodel import SchemaDefinition, compute_schema_hash, ALLOWED_FIELD_TYPES
12-
1312
# ---- FastAPI app & CORS ----
1413
app = FastAPI()
1514
app.add_middleware(
@@ -21,7 +20,7 @@
2120
)
2221

2322
# ---- Helpers ----
24-
def _merge_flat_content(existing: Optional[Dict[str, Any]], incoming: Optional[Dict[str, Any]]) -> Dict[str, Any]:
23+
def _merge_flat_content(existing: dict[str, Any] | None, incoming: dict[str, Any] | None) -> dict[str, Any]:
2524
"""
2625
Merge two flat dicts for schema `content`.
2726
- Existing values are overwritten by incoming values for the same key.
@@ -112,7 +111,7 @@ async def update_schema(id: str, update: SchemaDefinition) -> dict[str, str]:
112111
merged_version = payload.get("version") if payload.get("version") is not None else existing.get("version")
113112

114113
# --- Merge content (flat dict) ---
115-
incoming_content = payload.get("content")
114+
incoming_content: dict[str, Any] | None = payload.get("content")
116115
merged_content = _merge_flat_content(existing.get("content"), incoming_content)
117116

118117
# Optional: validate merged content types
@@ -141,7 +140,7 @@ async def update_schema(id: str, update: SchemaDefinition) -> dict[str, str]:
141140

142141

143142
@app.patch("/schemas/{id}/content", response_model=dict[str, str])
144-
async def patch_schema_content(id: str, content_updates: Dict[str, str]) -> dict[str, str]:
143+
async def patch_schema_content(id: str, content_updates: dict[str, str]) -> dict[str, str]:
145144
"""
146145
Convenience endpoint to ONLY upsert fields in `content`:
147146
- Adds new fields.
@@ -195,6 +194,7 @@ async def patch_schema_content(id: str, content_updates: Dict[str, str]) -> dict
195194
return {"message": "Content patched", "id": new_id}
196195

197196

197+
198198
@app.delete("/schemas/{id}", response_model=dict[str, str])
199199
async def delete_schema(id: str) -> dict[str, str]:
200200
"""
@@ -207,4 +207,6 @@ async def delete_schema(id: str) -> dict[str, str]:
207207
result = schemas_collection.delete_one({"id": id})
208208
if result.deleted_count == 0:
209209
raise HTTPException(status_code=404, detail="Schema not found")
210-
return {"message": "Schema deleted"}
210+
211+
# ✅ Return a simple success message
212+
return {"message": "Schema deleted", "id": id}

0 commit comments

Comments
 (0)