Skip to content

Commit 49a725d

Browse files
committed
fix: register event schema
1 parent 9d8bd2c commit 49a725d

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

eventsourcingdb/handlers/register_event_schema/register_event_schema.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from http import HTTPStatus
3+
from typing import Union, Dict, Any
34

45
from ...abstract_base_client import AbstractBaseClient
56
from ...errors.custom_error import CustomError
@@ -14,7 +15,7 @@
1415
async def register_event_schema(
1516
client: AbstractBaseClient,
1617
event_type: str,
17-
json_schema: str,
18+
json_schema: Union[str, Dict[str, Any]],
1819
) -> None:
1920
try:
2021
validate_type(event_type)
@@ -24,10 +25,22 @@ async def register_event_schema(
2425
) from validation_error
2526
except Exception as other_error:
2627
raise InternalError(str(other_error)) from other_error
28+
29+
# Handle both string and dictionary schema formats
30+
# If json_schema is a string, parse it to ensure it's valid JSON
31+
# If it's already a dict, use it directly
32+
schema_obj = json_schema
33+
if isinstance(json_schema, str):
34+
try:
35+
schema_obj = json.loads(json_schema)
36+
except json.JSONDecodeError as json_error:
37+
raise InvalidParameterError(
38+
'json_schema', f'Invalid JSON schema: {str(json_error)}'
39+
) from json_error
2740

2841
request_body = json.dumps({
2942
'eventType': event_type,
30-
'schema': json_schema,
43+
'schema': schema_obj,
3144
})
3245

3346
response: Response

tests/test_write_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ async def test_throws_error_if_event_does_not_match_schema(
202202

203203
await client.register_event_schema(
204204
"com.super.duper",
205-
'{"type":"object","additionalProperties":false}'
205+
{"type": "object", "additionalProperties": False}
206206
)
207207

208208
with pytest.raises(ClientError, match="event candidate does not match schema"):

0 commit comments

Comments
 (0)