check that the scale and precision are integers
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -85,6 +85,22 @@ cdef _parse_schema(schema, namespace, _write_hint):
}
parsed_schema["type"] = schema_type
+ # Correctness checks for logical types
+ logical_type = parsed_schema.get("logicalType")
+ if logical_type == "decimal":
+ scale = parsed_schema.get("scale")
+ if scale and not isinstance(scale, int):
+ raise SchemaParseException(
+ "decimal scale must be a postive integer, " +
+ "not {}".format(scale)
+ )
+ precision = parsed_schema.get("precision")
+ if precision and not isinstance(precision, int):
+ raise SchemaParseException(
+ "decimal precision must be a postive integer, " +
+ "not {}".format(precision)
+ )
+
if schema_type == "array":
parsed_schema["items"] = _parse_schema(
schema["items"],

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -110,6 +110,22 @@ def _parse_schema(schema, namespace, _write_hint):
}
parsed_schema["type"] = schema_type
+ # Correctness checks for logical types
+ logical_type = parsed_schema.get("logicalType")
+ if logical_type == "decimal":
+ scale = parsed_schema.get("scale")
+ if scale and not isinstance(scale, int):
+ raise SchemaParseException(
+ "decimal scale must be a postive integer, " +
+ "not {}".format(scale)
+ )
+ precision = parsed_schema.get("precision")
+ if precision and not isinstance(precision, int):
+ raise SchemaParseException(
+ "decimal precision must be a postive integer, " +
+ "not {}".format(precision)
+ )
+
if schema_type == "array":
parsed_schema["items"] = _parse_schema(
schema["items"],

MODIFY
tests/test_schema.py
tests/test_schema.py
@@ -105,3 +105,47 @@ def test_aliases_is_a_list():
with pytest.raises(SchemaParseException):
parse_schema(schema)
+
+
+def test_scale_is_an_int():
+ """https://github.com/fastavro/fastavro/issues/262"""
+ schema = {
+ "type": "record",
+ "name": "test_scale_is_an_int",
+ "fields": [{
+ "name": "field",
+ "type": {
+ "logicalType": "decimal",
+ "precision": 5,
+ "scale": "2",
+ "type": "bytes",
+ },
+ }],
+ }
+
+ with pytest.raises(SchemaParseException) as exc:
+ parse_schema(schema)
+
+ assert "decimal scale must be a postive integer" in str(exc)
+
+
+def test_precision_is_an_int():
+ """https://github.com/fastavro/fastavro/issues/262"""
+ schema = {
+ "type": "record",
+ "name": "test_scale_is_an_int",
+ "fields": [{
+ "name": "field",
+ "type": {
+ "logicalType": "decimal",
+ "precision": "5",
+ "scale": 2,
+ "type": "bytes",
+ },
+ }],
+ }
+
+ with pytest.raises(SchemaParseException) as exc:
+ parse_schema(schema)
+
+ assert "decimal precision must be a postive integer" in str(exc)

check that the scale and precision are integers