Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eventsourcingdb/handlers/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# CloudEvent field names
SPECVERSION_FIELD = "specversion"
TYPE_FIELD = "type"
PING_RECEIVED_TYPE = "io.eventsourcingdb.ping-received"
PING_RECEIVED_TYPE = "io.eventsourcingdb.api.ping-received"


async def ping(client: AbstractBaseClient) -> None:
Expand Down
15 changes: 10 additions & 5 deletions eventsourcingdb/handlers/read_event_types/event_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import TypeVar
from typing import TypeVar, Any

from ...errors.validation_error import ValidationError

Expand All @@ -10,7 +10,7 @@
class EventType:
event_type: str
is_phantom: bool
schema: str | None = None
schema: str | dict[str, Any] | None = None
Comment thread
atstaeff marked this conversation as resolved.
Outdated

@staticmethod
def parse(unknown_object: dict) -> Self:
Expand All @@ -27,16 +27,21 @@ def parse(unknown_object: dict) -> Self:
)

schema = unknown_object.get('schema')
if schema is not None and not isinstance(schema, str):
if schema is not None and not isinstance(schema, (str, dict)):
Comment thread
atstaeff marked this conversation as resolved.
Outdated
raise ValidationError(
f"Failed to parse schema '{schema}' to str."
f"Failed to parse schema '{schema}'. Schema must be either str or dict."
Comment thread
atstaeff marked this conversation as resolved.
Outdated
)

return EventType(
event_type=event_type,
is_phantom=is_phantom,
schema=schema,
)
) # type: ignore
Comment thread
atstaeff marked this conversation as resolved.
Outdated

def __hash__(self):
# Convert dictionary schema to a hashable form (tuple of items)
if isinstance(self.schema, dict):
# Sort items to ensure consistent hashing
schema_items = tuple(sorted((k, str(v)) for k, v in self.schema.items()))
Comment thread
atstaeff marked this conversation as resolved.
return hash((self.event_type, self.is_phantom, schema_items))
return hash((self.event_type, self.is_phantom, self.schema))
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from http import HTTPStatus
from typing import Any

from ...abstract_base_client import AbstractBaseClient
from ...errors.custom_error import CustomError
Expand All @@ -14,7 +15,7 @@
async def register_event_schema(
client: AbstractBaseClient,
event_type: str,
json_schema: str,
json_schema: str | dict[str, Any],
) -> None:
try:
validate_type(event_type)
Expand All @@ -25,9 +26,21 @@ async def register_event_schema(
except Exception as other_error:
raise InternalError(str(other_error)) from other_error

# Handle both string and dictionary schema formats
Comment thread
atstaeff marked this conversation as resolved.
Outdated
# If json_schema is a string, parse it to ensure it's valid JSON
# If it's already a dict, use it directly
schema_obj = json_schema
Comment thread
atstaeff marked this conversation as resolved.
Outdated
if isinstance(json_schema, str):
try:
schema_obj = json.loads(json_schema)
except json.JSONDecodeError as json_error:
raise InvalidParameterError(
'json_schema', f'Invalid JSON schema: {str(json_error)}'
) from json_error

request_body = json.dumps({
'eventType': event_type,
'schema': json_schema,
'schema': schema_obj,
})

response: Response
Expand Down
14 changes: 7 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ packages = [{include = "eventsourcingdb"}]

[tool.poetry.dependencies]
python = "^3.13"
aiohttp = "3.11.16"
aiohttp = "^3.11.16"

[tool.poetry.group.dev.dependencies]
pytest = "8.3.5"
flask = "3.1.0"
pylint = "3.3.5"
autopep8 = "2.3.2"
pytest-timeout = "2.3.1"
pytest-asyncio = "0.25.3"
pytest = "^8.3.5"
flask = "^3.1.0"
pylint = "^3.3.5"
autopep8 = "^2.3.2"
pytest-timeout = "^2.3.1"
pytest-asyncio = "^0.25.3"
2 changes: 1 addition & 1 deletion tests/shared/docker/eventsourcingdb/Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FROM thenativeweb/eventsourcingdb:0.103.1
FROM thenativeweb/eventsourcingdb:0.113.2
15 changes: 11 additions & 4 deletions tests/test_read_event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ async def test_reads_all_types_of_existing_events_and_registered_schemas(
),
])

await client.register_event_schema("org.ban.ban", '{"type":"object"}')
await client.register_event_schema("org.bing.chilling", '{"type":"object"}')
# Use Python dictionaries instead of JSON strings
Comment thread
atstaeff marked this conversation as resolved.
Outdated
await client.register_event_schema(
"org.ban.ban",
{"type": "object"}
)
await client.register_event_schema(
"org.bing.chilling",
{"type": "object"}
)

actual_event_types: set[EventType] = set()
async for event_type in client.read_event_types():
Expand Down Expand Up @@ -81,12 +88,12 @@ async def test_reads_all_types_of_existing_events_and_registered_schemas(
EventType(
event_type="org.ban.ban",
is_phantom=True,
schema='{"type":"object"}',
schema={"type": "object"},
),
EventType(
event_type="org.bing.chilling",
is_phantom=True,
schema='{"type":"object"}',
schema={"type": "object"},
),
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_write_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ async def test_throws_error_if_event_does_not_match_schema(

await client.register_event_schema(
"com.super.duper",
'{"type":"object","additionalProperties":false}'
{"type": "object", "additionalProperties": False}
)

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