Skip to content

Commit

Permalink
fix: enable self-referencing schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
eliax1996 committed Sep 18, 2023
1 parent a33b4e1 commit be5c0db
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions karapace/protobuf/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ def verify(self) -> DependencyVerifierResult:
declared_index = set(self.declared_types)
for used_type in self.used_types:
delimiter = used_type.rfind(";")
father_child_type = None
used_type_with_scope = ""
if delimiter != -1:
used_type_with_scope = used_type[:delimiter] + "." + used_type[delimiter + 1 :]
father_delimiter = used_type[:delimiter].find(".")
if father_delimiter != -1:
father_child_type = used_type[:father_delimiter] + "." + used_type[delimiter + 1 :]
used_type = used_type[delimiter + 1 :]

if used_type in DependenciesHardcoded.index:
Expand All @@ -54,6 +58,7 @@ def verify(self) -> DependencyVerifierResult:
if (
used_type in declared_index
or (delimiter != -1 and used_type_with_scope in declared_index)
or (father_child_type is not None and father_child_type in declared_index)
or "." + used_type in declared_index
):
continue
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/protobuf/test_protobuf_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,44 @@ def test_protobuf_field_compatible_alter_to_oneof():
protobuf_schema1.compare(protobuf_schema2, result)

assert result.is_compatible()


def test_protobuf_self_referencing_schema():
proto1 = """\
syntax = "proto3";
package fancy.company.in.party.v1;
message MyFirstMessage {
string my_fancy_string = 1;
}
message AnotherMessage {
message WowANestedMessage {
enum BamFancyEnum {
// Hei! This is a comment!
MY_AWESOME_FIELD = 0;
}
BamFancyEnum im_tricky_im_referring_to_the_previous_enum = 1;
}
}
"""

assert isinstance(ValidatedTypedSchema.parse(SchemaType.PROTOBUF, proto1).schema, ProtobufSchema)

proto2 = """\
syntax = "proto3";
package fancy.company.in.party.v1;
message AnotherMessage {
enum BamFancyEnum {
// Hei! This is a comment!
MY_AWESOME_FIELD = 0;
}
message WowANestedMessage {
message DeeplyNestedMsg {
BamFancyEnum im_tricky_im_referring_to_the_previous_enum = 1;
}
}
}
"""

assert type(ValidatedTypedSchema.parse(SchemaType.PROTOBUF, proto2).schema, ProtobufSchema)

0 comments on commit be5c0db

Please sign in to comment.