From 4c83f10cdbd18c7a806061e87eeee024dc9bb5d8 Mon Sep 17 00:00:00 2001 From: Harshal Sheth Date: Thu, 29 Feb 2024 16:13:43 -0800 Subject: [PATCH] feat(ingest): throw codegen error on duplicate class names (#9960) --- metadata-ingestion/scripts/avro_codegen.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/metadata-ingestion/scripts/avro_codegen.py b/metadata-ingestion/scripts/avro_codegen.py index bd4988f990..fbf45c08bb 100644 --- a/metadata-ingestion/scripts/avro_codegen.py +++ b/metadata-ingestion/scripts/avro_codegen.py @@ -148,11 +148,27 @@ def merge_schemas(schemas_obj: List[dict]) -> str: # Combine schemas as a "union" of all of the types. merged = ["null"] + schemas_obj + # Check that we don't have the same class name in multiple namespaces. + names_to_spaces: Dict[str, str] = {} + # Patch add_name method to NOT complain about duplicate names. class NamesWithDups(avro.schema.Names): def add_name(self, name_attr, space_attr, new_schema): + to_add = avro.schema.Name(name_attr, space_attr, self.default_namespace) + assert to_add.name + assert to_add.space assert to_add.fullname + + if to_add.name in names_to_spaces: + if names_to_spaces[to_add.name] != to_add.space: + raise ValueError( + f"Duplicate name {to_add.name} in namespaces {names_to_spaces[to_add.name]} and {to_add.space}. " + "This will cause conflicts in the generated code." + ) + else: + names_to_spaces[to_add.name] = to_add.space + self.names[to_add.fullname] = new_schema return to_add