You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2021/12/21 17:20:15 UTC

[avro] branch branch-1.11 updated: AVRO-3229: Raise Exception on Invalid Enum Default (#1433)

This is an automated email from the ASF dual-hosted git repository.

rskraba pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.11 by this push:
     new ffc04f4  AVRO-3229: Raise Exception on Invalid Enum Default (#1433)
ffc04f4 is described below

commit ffc04f434f6be74ef6edd7c87eaa03fe2a3331e6
Author: Michael A. Smith <mi...@smith-li.com>
AuthorDate: Tue Dec 21 12:18:02 2021 -0500

    AVRO-3229: Raise Exception on Invalid Enum Default (#1433)
---
 lang/py/avro/errors.py           | 4 ++++
 lang/py/avro/schema.py           | 5 +++++
 lang/py/avro/test/test_schema.py | 1 +
 3 files changed, 10 insertions(+)

diff --git a/lang/py/avro/errors.py b/lang/py/avro/errors.py
index 5ff3603..b8828d0 100644
--- a/lang/py/avro/errors.py
+++ b/lang/py/avro/errors.py
@@ -40,6 +40,10 @@ class InvalidName(SchemaParseException):
     """User attempted to parse a schema with an invalid name."""
 
 
+class InvalidDefault(SchemaParseException):
+    """User attempted to parse a schema with an invalid default."""
+
+
 class AvroWarning(UserWarning):
     """Base class for warnings."""
 
diff --git a/lang/py/avro/schema.py b/lang/py/avro/schema.py
index 73371d5..0793ce6 100644
--- a/lang/py/avro/schema.py
+++ b/lang/py/avro/schema.py
@@ -583,6 +583,11 @@ class EnumSchema(EqualByPropsMixin, NamedSchema):
         if doc is not None:
             self.set_prop("doc", doc)
 
+        if other_props and "default" in other_props:
+            default = other_props["default"]
+            if default not in symbols:
+                raise avro.errors.InvalidDefault(f"Enum default '{default}' is not a valid member of symbols '{symbols}'")
+
     @property
     def symbols(self) -> Sequence[str]:
         symbols = self.get_prop("symbols")
diff --git a/lang/py/avro/test/test_schema.py b/lang/py/avro/test/test_schema.py
index 2542617..b5906d2 100644
--- a/lang/py/avro/test/test_schema.py
+++ b/lang/py/avro/test/test_schema.py
@@ -85,6 +85,7 @@ FIXED_EXAMPLES = [
 ENUM_EXAMPLES = [
     ValidTestSchema({"type": "enum", "name": "Test", "symbols": ["A", "B"]}),
     ValidTestSchema({"type": "enum", "name": "AVRO2174", "symbols": ["nowhitespace"]}),
+    InvalidTestSchema({"type": "enum", "name": "bad_default", "symbols": ["A"], "default": "B"}, comment="AVRO-3229"),
     InvalidTestSchema({"type": "enum", "name": "Status", "symbols": "Normal Caution Critical"}),
     InvalidTestSchema({"type": "enum", "name": [0, 1, 1, 2, 3, 5, 8], "symbols": ["Golden", "Mean"]}),
     InvalidTestSchema({"type": "enum", "symbols": ["I", "will", "fail", "no", "name"]}),