|
| 1 | +""" |
| 2 | +karapace - unit tests for request models |
| 3 | +
|
| 4 | +Copyright (c) 2025 Aiven Ltd |
| 5 | +See LICENSE for details |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | +from pydantic import ValidationError |
| 10 | + |
| 11 | +from karapace.api.routers.requests import SchemaRequest |
| 12 | + |
| 13 | + |
| 14 | +class TestSchemaRequestExtraFields: |
| 15 | + """Extra fields must be silently ignored to match v4 behavior. |
| 16 | +
|
| 17 | + v4 parsed the request body as a plain dict and only read the keys it |
| 18 | + needed. The 5.x Pydantic model must not reject unknown keys so that |
| 19 | + existing clients that send extra properties (e.g. ``compatibility``) |
| 20 | + continue to work after upgrading. |
| 21 | + """ |
| 22 | + |
| 23 | + def test_extra_fields_are_ignored(self) -> None: |
| 24 | + req = SchemaRequest.model_validate( |
| 25 | + { |
| 26 | + "schema": '{"type": "string"}', |
| 27 | + "compatibility": "BACKWARD", |
| 28 | + } |
| 29 | + ) |
| 30 | + assert req.schema_str == '{"type": "string"}' |
| 31 | + assert not hasattr(req, "compatibility") |
| 32 | + |
| 33 | + def test_multiple_extra_fields_are_ignored(self) -> None: |
| 34 | + req = SchemaRequest.model_validate( |
| 35 | + { |
| 36 | + "schema": '{"type": "string"}', |
| 37 | + "compatibility": "BACKWARD", |
| 38 | + "unknown_prop": 123, |
| 39 | + "another": True, |
| 40 | + } |
| 41 | + ) |
| 42 | + assert req.schema_str == '{"type": "string"}' |
| 43 | + |
| 44 | + def test_required_field_still_validated(self) -> None: |
| 45 | + with pytest.raises(ValidationError) as exc_info: |
| 46 | + SchemaRequest.model_validate( |
| 47 | + { |
| 48 | + "compatibility": "BACKWARD", |
| 49 | + } |
| 50 | + ) |
| 51 | + errors = exc_info.value.errors() |
| 52 | + assert any(e["type"] == "missing" for e in errors) |
| 53 | + |
| 54 | + def test_valid_request_with_all_fields(self) -> None: |
| 55 | + req = SchemaRequest.model_validate( |
| 56 | + { |
| 57 | + "schema": '{"type": "string"}', |
| 58 | + "schemaType": "AVRO", |
| 59 | + "references": None, |
| 60 | + } |
| 61 | + ) |
| 62 | + assert req.schema_str == '{"type": "string"}' |
| 63 | + assert req.schema_type.value == "AVRO" |
0 commit comments