Skip to content

Commit 6801210

Browse files
committed
fix: Map field edge-case
During the check wheter a field is a "map", the string "entry" is added to the field name, checked against the type name and then further checks are made against the nested type of a parent message. In this edge-case, the first check would pass even though it shouldn't and that would cause an error because the parent type does not have a "nested_type" attribute.
1 parent ad8b917 commit 6801210

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/betterproto/plugin/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ def is_map(
324324
) -> bool:
325325
"""True if proto_field_obj is a map, otherwise False."""
326326
if proto_field_obj.type == FieldDescriptorProtoType.TYPE_MESSAGE:
327+
if not hasattr(parent_message, "nested_type"):
328+
return False
329+
327330
# This might be a map...
328331
message_type = proto_field_obj.type_name.split(".").pop().lower()
329332
map_entry = f"{proto_field_obj.name.replace('_', '').lower()}entry"

tests/inputs/entry/entry.proto

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

0 commit comments

Comments
 (0)