Skip to content

Commit 46f4fab

Browse files
authored
Add JSON Schema validation test; fix typo (#2003)
* Add JSON Schema validation test; fix typo * Fix test * Fix flake8
1 parent a0f9d20 commit 46f4fab

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

src/confluent_kafka/schema_registry/_async/json_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ async def __init_impl(
272272
raise ValueError("schema.id.serializer must be callable")
273273

274274
self._validate = conf_copy.pop('validate')
275-
if not isinstance(self._normalize_schemas, bool):
275+
if not isinstance(self._validate, bool):
276276
raise ValueError("validate must be a boolean value")
277277

278278
if len(conf_copy) > 0:

src/confluent_kafka/schema_registry/_sync/json_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def __init_impl(
272272
raise ValueError("schema.id.serializer must be callable")
273273

274274
self._validate = conf_copy.pop('validate')
275-
if not isinstance(self._normalize_schemas, bool):
275+
if not isinstance(self._validate, bool):
276276
raise ValueError("validate must be a boolean value")
277277

278278
if len(conf_copy) > 0:

tests/schema_registry/_async/test_json_serdes.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ async def run_before_and_after_tests(tmpdir):
8787
async def test_json_basic_serialization():
8888
conf = {'url': _BASE_URL}
8989
client = AsyncSchemaRegistryClient.new_client(conf)
90-
ser_conf = {'auto.register.schemas': True}
90+
ser_conf = {'auto.register.schemas': True, 'validate': True}
9191
obj = {
9292
'intField': 123,
9393
'doubleField': 45.67,
@@ -121,6 +121,40 @@ async def test_json_basic_serialization():
121121
assert obj == obj2
122122

123123

124+
async def test_json_basic_failing_validation():
125+
conf = {'url': _BASE_URL}
126+
client = AsyncSchemaRegistryClient.new_client(conf)
127+
ser_conf = {'auto.register.schemas': True, 'validate': True}
128+
obj = {
129+
'intField': '123',
130+
'doubleField': 45.67,
131+
'stringField': 'hi',
132+
'booleanField': True,
133+
'bytesField': base64.b64encode(b'foobar').decode('utf-8'),
134+
}
135+
schema = {
136+
"type": "object",
137+
"properties": {
138+
"intField": {"type": "integer"},
139+
"doubleField": {"type": "number"},
140+
"stringField": {
141+
"type": "string",
142+
"confluent:tags": ["PII"]
143+
},
144+
"booleanField": {"type": "boolean"},
145+
"bytesField": {
146+
"type": "string",
147+
"contentEncoding": "base64",
148+
"confluent:tags": ["PII"]
149+
}
150+
}
151+
}
152+
ser = await AsyncJSONSerializer(json.dumps(schema), client, conf=ser_conf)
153+
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE)
154+
with pytest.raises(SerializationError):
155+
await ser(obj, ser_ctx)
156+
157+
124158
async def test_json_guid_in_header():
125159
conf = {'url': _BASE_URL}
126160
client = AsyncSchemaRegistryClient.new_client(conf)

tests/schema_registry/_sync/test_json_serdes.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def run_before_and_after_tests(tmpdir):
8787
def test_json_basic_serialization():
8888
conf = {'url': _BASE_URL}
8989
client = SchemaRegistryClient.new_client(conf)
90-
ser_conf = {'auto.register.schemas': True}
90+
ser_conf = {'auto.register.schemas': True, 'validate': True}
9191
obj = {
9292
'intField': 123,
9393
'doubleField': 45.67,
@@ -121,6 +121,40 @@ def test_json_basic_serialization():
121121
assert obj == obj2
122122

123123

124+
def test_json_basic_failing_validation():
125+
conf = {'url': _BASE_URL}
126+
client = SchemaRegistryClient.new_client(conf)
127+
ser_conf = {'auto.register.schemas': True, 'validate': True}
128+
obj = {
129+
'intField': '123',
130+
'doubleField': 45.67,
131+
'stringField': 'hi',
132+
'booleanField': True,
133+
'bytesField': base64.b64encode(b'foobar').decode('utf-8'),
134+
}
135+
schema = {
136+
"type": "object",
137+
"properties": {
138+
"intField": {"type": "integer"},
139+
"doubleField": {"type": "number"},
140+
"stringField": {
141+
"type": "string",
142+
"confluent:tags": ["PII"]
143+
},
144+
"booleanField": {"type": "boolean"},
145+
"bytesField": {
146+
"type": "string",
147+
"contentEncoding": "base64",
148+
"confluent:tags": ["PII"]
149+
}
150+
}
151+
}
152+
ser = JSONSerializer(json.dumps(schema), client, conf=ser_conf)
153+
ser_ctx = SerializationContext(_TOPIC, MessageField.VALUE)
154+
with pytest.raises(SerializationError):
155+
ser(obj, ser_ctx)
156+
157+
124158
def test_json_guid_in_header():
125159
conf = {'url': _BASE_URL}
126160
client = SchemaRegistryClient.new_client(conf)

0 commit comments

Comments
 (0)