Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/requirements/types-jsonschema…
Browse files Browse the repository at this point in the history
…-4.23.0.20240712
  • Loading branch information
nosahama authored Aug 7, 2024
2 parents 020c480 + fa5a1b4 commit 84d3399
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion karapace/schema_registry_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ def _validate_schema_request_body(self, content_type: str, body: dict | Any) ->
status=HTTPStatus.BAD_REQUEST,
)
for field in body:
if field not in {"schema", "schemaType", "references"}:
if field not in {"schema", "schemaType", "references", "metadata", "ruleSet"}:
self.r(
body={
"error_code": SchemaErrorCodes.HTTP_UNPROCESSABLE_ENTITY.value,
Expand Down
4 changes: 3 additions & 1 deletion karapace/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from enum import Enum, unique
from karapace.errors import InvalidVersion
from typing import ClassVar, Dict, List, Mapping, NewType, Sequence, Union
from typing import Any, ClassVar, Dict, List, Mapping, NewType, Sequence, Union
from typing_extensions import TypeAlias

import functools
Expand All @@ -23,6 +23,8 @@

Subject = NewType("Subject", str)
VersionTag = Union[str, int]
SchemaMetadata = NewType("SchemaMetadata", Dict[str, Any])
SchemaRuleSet = NewType("SchemaRuleSet", Dict[str, Any])

# note: the SchemaID is a unique id among all the schemas (and each version should be assigned to a different id)
# basically the same SchemaID refer always to the same TypedSchema.
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ sniffio==1.3.1
# anyio
sortedcontainers==2.4.0
# via hypothesis
tenacity==8.3.0
tenacity==9.0.0
# via -r requirements.txt
tomli==2.0.1
# via
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-typing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ certifi==2024.7.4
# via
# -c requirements-dev.txt
# sentry-sdk
mypy==1.10.0
mypy==1.11.1
# via -r requirements-typing.in
mypy-extensions==1.0.0
# via mypy
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pyjwt>=2.4.0<3
python-dateutil<3
python-snappy
rich~=13.7.1
tenacity<9
tenacity<10
typing-extensions
ujson<6
watchfiles<1
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ six==1.16.0
# python-dateutil
sniffio==1.3.1
# via anyio
tenacity==8.3.0
tenacity==9.0.0
# via -r requirements.in
typing-extensions==4.12.1
# via
Expand Down
13 changes: 12 additions & 1 deletion tests/integration/schema_registry/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from karapace.client import Client
from karapace.compatibility import CompatibilityModes
from karapace.schema_reader import SchemaType
from karapace.typing import SchemaMetadata, SchemaRuleSet
from tests.schemas.json_schemas import (
A_DINT_B_DINT_OBJECT_SCHEMA,
A_DINT_B_INT_OBJECT_SCHEMA,
Expand Down Expand Up @@ -234,8 +235,14 @@ async def not_schemas_are_backward_compatible(

@pytest.mark.parametrize("trail", ["", "/"])
@pytest.mark.parametrize("compatibility", [CompatibilityModes.FORWARD, CompatibilityModes.BACKWARD, CompatibilityModes.FULL])
@pytest.mark.parametrize("metadata", [None, {}])
@pytest.mark.parametrize("rule_set", [None, {}])
async def test_same_jsonschema_must_have_same_id(
registry_async_client: Client, compatibility: CompatibilityModes, trail: str
registry_async_client: Client,
compatibility: CompatibilityModes,
trail: str,
metadata: SchemaMetadata,
rule_set: SchemaRuleSet,
) -> None:
for schema in ALL_SCHEMAS:
subject = new_random_name("subject")
Expand All @@ -248,6 +255,8 @@ async def test_same_jsonschema_must_have_same_id(
json={
"schema": json.dumps(schema.schema),
"schemaType": SchemaType.JSONSCHEMA.value,
"metadata": metadata,
"ruleSet": rule_set,
},
)
assert first_res.status_code == 200
Expand All @@ -259,6 +268,8 @@ async def test_same_jsonschema_must_have_same_id(
json={
"schema": json.dumps(schema.schema),
"schemaType": SchemaType.JSONSCHEMA.value,
"metadata": metadata,
"ruleSet": rule_set,
},
)
assert second_res.status_code == 200
Expand Down
15 changes: 12 additions & 3 deletions tests/integration/test_schema_protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from karapace.errors import InvalidTest
from karapace.protobuf.kotlin_wrapper import trim_margin
from karapace.schema_type import SchemaType
from karapace.typing import JsonData
from karapace.typing import JsonData, SchemaMetadata, SchemaRuleSet
from tests.base_testcase import BaseTestCase
from tests.utils import create_subject_name_factory
from typing import List, Optional, Union
Expand Down Expand Up @@ -963,11 +963,20 @@ class ReferenceTestCase(BaseTestCase):
],
ids=str,
)
async def test_references(testcase: ReferenceTestCase, registry_async_client: Client):
@pytest.mark.parametrize("metadata", [None, {}])
@pytest.mark.parametrize("rule_set", [None, {}])
async def test_references(
testcase: ReferenceTestCase, registry_async_client: Client, metadata: SchemaMetadata, rule_set: SchemaRuleSet
):
for testdata in testcase.schemas:
if isinstance(testdata, TestCaseSchema):
print(f"Adding new schema, subject: '{testdata.subject}'\n{testdata.schema_str}")
body = {"schemaType": testdata.schema_type, "schema": testdata.schema_str}
body = {
"schemaType": testdata.schema_type,
"schema": testdata.schema_str,
"metadata": metadata,
"ruleSet": rule_set,
}
if testdata.references:
body["references"] = testdata.references
res = await registry_async_client.post(f"subjects/{testdata.subject}/versions", json=body)
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_schema_registry_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@
from unittest.mock import ANY, AsyncMock, Mock, patch, PropertyMock

import asyncio
import pytest


async def test_validate_schema_request_body():
controller = KarapaceSchemaRegistryController(config=set_config_defaults(DEFAULTS))

controller._validate_schema_request_body( # pylint: disable=W0212
"application/json", {"schema": "{}", "schemaType": "JSON", "references": [], "metadata": {}, "ruleSet": {}}
)

with pytest.raises(HTTPResponse) as exc_info:
controller._validate_schema_request_body( # pylint: disable=W0212
"application/json",
{"schema": "{}", "schemaType": "JSON", "references": [], "unexpected_field_name": {}, "ruleSet": {}},
)
assert exc_info.type is HTTPResponse
assert str(exc_info.value) == "HTTPResponse 422"


async def test_forward_when_not_ready():
Expand Down

0 comments on commit 84d3399

Please sign in to comment.