Skip to content

Commit e7133ad

Browse files
authored
fix: map field edge-case
This change ensures a parent is a nested type when checking if a field is a map.
1 parent 204e04d commit e7133ad

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/betterproto/plugin/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ def is_map(
339339
) -> bool:
340340
"""True if proto_field_obj is a map, otherwise False."""
341341
if proto_field_obj.type == FieldDescriptorProtoType.TYPE_MESSAGE:
342+
if not hasattr(parent_message, "nested_type"):
343+
return False
344+
342345
# This might be a map...
343346
message_type = proto_field_obj.type_name.split(".").pop().lower()
344347
map_entry = f"{proto_field_obj.name.replace('_', '').lower()}entry"

tests/inputs/entry/entry.proto

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
syntax = "proto3";
2+
3+
package entry;
4+
5+
// This is a minimal example of a repeated message field that caused issues when
6+
// checking whether a message is a map.
7+
//
8+
// During the check wheter a field is a "map", the string "entry" is added to
9+
// the field name, checked against the type name and then further checks are
10+
// made against the nested type of a parent message. In this edge-case, the
11+
// first check would pass even though it shouldn't and that would cause an
12+
// error because the parent type does not have a "nested_type" attribute.
13+
14+
message Test {
15+
repeated ExportEntry export = 1;
16+
}
17+
18+
message ExportEntry {
19+
string name = 1;
20+
}

0 commit comments

Comments
 (0)