Skip to content

Commit

Permalink
Merge pull request #599 from aiven/fix-references-validation
Browse files Browse the repository at this point in the history
Fix references validation from request
  • Loading branch information
aiven-anton authored May 5, 2023
2 parents b86a0ef + 6302c35 commit 0b6685c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
12 changes: 12 additions & 0 deletions karapace/schema_registry_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,18 @@ def _validate_schema_key(self, content_type: str, body: dict) -> None:

def _validate_references(self, content_type: str, schema_type: SchemaType, body: JsonData) -> list[Reference] | None:
references = body.get("references", [])
# Allow passing `null` as value for compatibility
if references is None:
return None
if not isinstance(references, list):
self.r(
body={
"error_code": SchemaErrorCodes.HTTP_BAD_REQUEST.value,
"message": "Expected array of `references`",
},
content_type=content_type,
status=HTTPStatus.BAD_REQUEST,
)
if references and schema_type != SchemaType.PROTOBUF:
self.r(
body={
Expand Down
53 changes: 53 additions & 0 deletions tests/integration/test_dependencies_compatibility_protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,56 @@ async def test_protobuf_schema_compatibility_dependencies2(registry_async_client
)
assert res.status_code == 200
assert res.json() == {"is_compatible": False}


SIMPLE_SCHEMA = """\
syntax = "proto3";
message Msg {
string name = 1;
}
"""


async def test_protobuf_schema_references_rejected_values(registry_async_client: Client) -> None:
subject = create_subject_name_factory("test_protobuf_schema_references_values")()
res = await registry_async_client.put(f"config/{subject}", json={"compatibility": "BACKWARD"})
assert res.status_code == 200

res = await registry_async_client.post(
f"subjects/{subject}/versions", json={"schemaType": "PROTOBUF", "schema": SIMPLE_SCHEMA, "references": 1}
)
assert res.status_code == 400

res = await registry_async_client.post(
f"subjects/{subject}/versions", json={"schemaType": "PROTOBUF", "schema": SIMPLE_SCHEMA, "references": "foo"}
)
assert res.status_code == 400

res = await registry_async_client.post(
f"subjects/{subject}/versions", json={"schemaType": "PROTOBUF", "schema": SIMPLE_SCHEMA, "references": False}
)
assert res.status_code == 400

res = await registry_async_client.post(
f"subjects/{subject}/versions",
json={"schemaType": "PROTOBUF", "schema": SIMPLE_SCHEMA, "references": {"this_is_object": True}},
)
assert res.status_code == 400


async def test_protobuf_schema_references_valid_values(registry_async_client: Client) -> None:
subject = create_subject_name_factory("test_protobuf_schema_references_values")()
res = await registry_async_client.put(f"config/{subject}", json={"compatibility": "BACKWARD"})
assert res.status_code == 200

# null value accepted for compatibility, same as empty list
res = await registry_async_client.post(
f"subjects/{subject}/versions", json={"schemaType": "PROTOBUF", "schema": SIMPLE_SCHEMA, "references": None}
)
assert res.status_code == 200

res = await registry_async_client.post(
f"subjects/{subject}/versions", json={"schemaType": "PROTOBUF", "schema": SIMPLE_SCHEMA, "references": []}
)
assert res.status_code == 200

0 comments on commit 0b6685c

Please sign in to comment.