Skip to content

Commit c5cf8f1

Browse files
committed
feat(schema): provide a way set _ignore_default_error to True (default to false)
1 parent 76bb939 commit c5cf8f1

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

schema_registry/client/schema.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
class BaseSchema(ABC):
1818
"""Abstract class for schema wrapper"""
1919

20-
def __init__(self, schema: typing.Union[str, typing.Dict[str, typing.Any]]) -> None:
20+
def __init__(self, schema: typing.Union[str, typing.Dict[str, typing.Any]], ignore_default_error: bool = False) -> None:
2121
if isinstance(schema, str):
2222
schema = json.loads(schema)
2323
self.raw_schema = typing.cast(typing.Dict, schema)
24+
self.ignore_default_error = ignore_default_error
2425
self.schema = self.parse_schema(self.raw_schema)
2526
self.generate_hash()
2627

@@ -66,10 +67,10 @@ def __eq__(self, other: typing.Any) -> bool:
6667
class AvroSchema(BaseSchema):
6768
"""Integrate BaseSchema for Avro schema."""
6869

69-
def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None:
70+
def __init__(self, *args: typing.Any, ignore_default_error: bool = False, **kwargs: typing.Any) -> None:
7071
self._expanded_schema: typing.Optional[typing.Dict] = None
7172
self._flat_schema: typing.Optional[typing.Dict] = None
72-
73+
self.ignore_default_error: typing.Optional[bool] = ignore_default_error
7374
super().__init__(*args, **kwargs)
7475

7576
@property
@@ -103,28 +104,28 @@ def flat_schema(self) -> typing.Dict:
103104
# NOTE: Dict expected when we pass a dict
104105
self._flat_schema = typing.cast(
105106
typing.Dict,
106-
fastavro.parse_schema(self.raw_schema, _write_hint=False, _force=True),
107+
fastavro.parse_schema(self.raw_schema, _write_hint=False, _force=True, _ignore_default_error=self.ignore_default_error),
107108
)
108109

109110
return self._flat_schema
110111

111112
def parse_schema(self, schema: typing.Dict) -> typing.Dict:
112113
# NOTE: Dict expected when we pass a dict
113-
return typing.cast(typing.Dict, fastavro.parse_schema(schema, _force=True))
114+
return typing.cast(typing.Dict, fastavro.parse_schema(schema, _force=True, _ignore_default_error=self.ignore_default_error))
114115

115116
@staticmethod
116-
def load(fp: str) -> AvroSchema:
117+
def load(fp: str, ignore_default_error=False) -> AvroSchema:
117118
"""Parse an avro schema from a file path."""
118119
with open(fp, mode="r") as f:
119120
content = f.read()
120-
return AvroSchema(content)
121+
return AvroSchema(content, ignore_default_error)
121122

122123
@staticmethod
123-
async def async_load(fp: str) -> AvroSchema:
124+
async def async_load(fp: str, ignore_default_error=False) -> AvroSchema:
124125
"""Parse an avro schema from a file path."""
125126
async with aiofiles.open(fp, mode="r") as f:
126127
content = await f.read()
127-
return AvroSchema(content)
128+
return AvroSchema(content, ignore_default_error)
128129

129130

130131
class JsonSchema(BaseSchema):
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "basic",
3+
"type": "record",
4+
"doc": "basic schema for tests",
5+
"namespace": "python.test.basic",
6+
"fields": [
7+
{
8+
"name": "number",
9+
"doc": "age",
10+
"type": [
11+
"long",
12+
"null"
13+
],
14+
"default": "null"
15+
},
16+
{
17+
"name": "name",
18+
"doc": "a name",
19+
"type": [
20+
"string"
21+
]
22+
}
23+
]
24+
}

tests/client/async_client/test_schema.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ async def test_avro_schema_load_parse_error():
2323
with pytest.raises(fastavro.schema.UnknownType):
2424
await schema.AvroSchema.async_load(data_gen.get_schema_path("invalid_schema.avsc"))
2525

26+
@pytest.mark.asyncio
27+
async def test_avro_schema_load_parse_default_error():
28+
with pytest.raises(fastavro.schema.SchemaParseException):
29+
await schema.AvroSchema.async_load(data_gen.get_schema_path("default_schema.avsc"))
30+
31+
@pytest.mark.asyncio
32+
async def test_avro_schema_load_parse_default_error_ignored():
33+
parsed = await schema.AvroSchema.async_load(data_gen.get_schema_path("default_schema.avsc"), ignore_default_error=True)
34+
assert isinstance(parsed, schema.AvroSchema)
35+
2636

2737
def test_json_schema_from_string():
2838
parsed = schema.JsonSchema(data_gen.JSON_BASIC_SCHEMA)

0 commit comments

Comments
 (0)