Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion python/sentry_kafka_schemas/codecs/json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import pathlib
from typing import Any, Optional, TypeVar, cast

import fastjsonschema
Expand All @@ -8,14 +9,24 @@

T = TypeVar("T")

BASE_DIR = pathlib.Path(__file__).parent.parent / "schemas"


def file_handler(uri):
absolute_path = BASE_DIR / uri[7:]
if not absolute_path.exists():
raise FileNotFoundError(f"Schema file not found: {absolute_path}")
with open(absolute_path) as f:
return rapidjson.load(f)


class JsonCodec(Codec[T]):
def __init__(
self,
json_schema: Optional[object],
) -> None:
if json_schema is not None:
self.__validate = fastjsonschema.compile(json_schema)
self.__validate = fastjsonschema.compile(json_schema, handlers={"file": file_handler})
else:
self.__validate = lambda _: None

Expand Down
3 changes: 2 additions & 1 deletion python/sentry_kafka_schemas/codecs/msgpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fastjsonschema
import msgpack
from sentry_kafka_schemas.codecs import Codec, ValidationError
from sentry_kafka_schemas.codecs.json import file_handler

T = TypeVar("T")

Expand All @@ -14,7 +15,7 @@ class MsgpackCodec(Codec[T]):

def __init__(self, json_schema: Optional[object]) -> None:
if json_schema is not None:
self.__validate = fastjsonschema.compile(json_schema)
self.__validate = fastjsonschema.compile(json_schema, handlers={"file": file_handler})
else:
self.__validate = lambda _: None

Expand Down
12 changes: 10 additions & 2 deletions python/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import pytest
import rapidjson
from sentry_kafka_schemas import iter_examples
from sentry_kafka_schemas.codecs.json import file_handler, BASE_DIR
from sentry_kafka_schemas.sentry_kafka_schemas import _get_schema, get_codec, get_topic, list_topics
from sentry_kafka_schemas.types import Example
from jsonschema import RefResolver


def get_all_examples() -> Iterator[Tuple[str, int, Example]]:
Expand Down Expand Up @@ -65,11 +67,17 @@ def test_json_examples(
example_data = example.load()

if jsonschema_library == "fastjsonschema":
compiled = fastjsonschema.compile(schema)
compiled = fastjsonschema.compile(schema, handlers={"file": file_handler})
compiled(example_data)
elif jsonschema_library == "jsonschema":
try:
jsonschema.validate(example_data, schema)
#TODO: Is this even necessary? Looks like we removed usage of jsonschema?
resolver = RefResolver(
base_uri=f"file:/{BASE_DIR}",
referrer=schema,
handlers={"file": file_handler} # Use the custom_resolver function
)
jsonschema.validate(example_data, schema, resolver=resolver)
except jsonschema.ValidationError as e:
_get_most_specific_jsonschema_error(e)
elif jsonschema_library == "rapidjson":
Expand Down
Loading