Skip to content

Commit 6b20d46

Browse files
authored
Fix #73: nested protoc type defs (#656)
1 parent 35a9925 commit 6b20d46

5 files changed

Lines changed: 37 additions & 15 deletions

File tree

protobuf/src/main/java/tools/jackson/dataformat/protobuf/schema/TypeResolver.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,31 @@ private ProtobufField _findAndResolve(FieldElement nativeField, String typeStr)
223223
if (nativeMt != null) {
224224
return new ProtobufField(nativeField, resolve(this, nativeMt));
225225
}
226-
return null;
226+
// [dataformats-binary#73] Handle dot-notation references to nested message types
227+
// (e.g. "OuterType.InnerType")
228+
return _findDottedType(nativeField, typeStr);
229+
}
230+
231+
/**
232+
* Try to resolve a dot-notation type reference (e.g. {@code "OuterType.InnerType"})
233+
* by navigating the message type hierarchy declared at this scope level.
234+
*/
235+
private ProtobufField _findDottedType(FieldElement nativeField, String typeStr)
236+
{
237+
int dotIx = typeStr.indexOf('.');
238+
if (dotIx <= 0) {
239+
return null;
240+
}
241+
String outerName = typeStr.substring(0, dotIx);
242+
String innerPath = typeStr.substring(dotIx + 1);
243+
MessageElement outerMsg = _declaredMessageTypes.get(outerName);
244+
if (outerMsg == null) {
245+
return null;
246+
}
247+
// Create a resolver in the context of the outer type and recursively
248+
// resolve the remaining path (handles arbitrary nesting depth)
249+
TypeResolver outerResolver = TypeResolver.construct(this, outerName, outerMsg.nestedElements());
250+
return outerResolver._findAndResolve(nativeField, innerPath);
227251
}
228252

229253
private StringBuilder _knownEnums(StringBuilder sb) {

protobuf/src/test/java/module-info.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@
2020
opens tools.jackson.dataformat.protobuf.schema;
2121
opens tools.jackson.dataformat.protobuf.testutil;
2222
opens tools.jackson.dataformat.protobuf.testutil.failure;
23-
opens tools.jackson.dataformat.protobuf.tofix;
2423
}

protobuf/src/test/java/tools/jackson/dataformat/protobuf/tofix/GenerateNestedType73Test.java renamed to protobuf/src/test/java/tools/jackson/dataformat/protobuf/schema/NestedTypeRef73Test.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1-
package tools.jackson.dataformat.protobuf.tofix;
1+
package tools.jackson.dataformat.protobuf.schema;
22

33
import java.io.StringReader;
44

55
import org.junit.jupiter.api.Test;
66

77
import tools.jackson.dataformat.protobuf.ProtobufMapper;
88
import tools.jackson.dataformat.protobuf.ProtobufTestBase;
9-
import tools.jackson.dataformat.protobuf.schema.ProtobufSchema;
10-
import tools.jackson.dataformat.protobuf.testutil.failure.JacksonTestFailureExpected;
119

1210
import static org.junit.jupiter.api.Assertions.assertNotNull;
1311

14-
public class GenerateNestedType73Test extends ProtobufTestBase
12+
// [dataformats-binary#73]
13+
public class NestedTypeRef73Test extends ProtobufTestBase
1514
{
16-
/*
17-
/**********************************************************
18-
/* Test methods
19-
/**********************************************************
20-
*/
21-
2215
final ProtobufMapper MAPPER = new ProtobufMapper();
2316

24-
// [dataformats-binary#68]
25-
@JacksonTestFailureExpected
17+
// [dataformats-binary#73]: dot-notation reference to a nested message type
2618
@Test
27-
public void testNestedTypes() throws Exception
19+
public void testNestedTypeRefViaRootType() throws Exception
2820
{
2921
final String SCHEMA_STR =
3022
" package mypackage;\n"

release-notes/CREDITS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Fawzi Essam (@iifawzi)
1818
* Contributed #591: Change `CBOR` Features defaults for 3.0
1919
(3.0.0)
2020

21+
Kenji Noguchi (@knoguchi)
22+
23+
* Reported #73: (protobuf) Cannot resolve inner types in protoc definitions
24+
(3.1.0)
25+
2126
Andy Wilkinson (@wilkinsona)
2227

2328
* Requested #619: (avro, cbor, ion, smile) Add `isEnabled()` methods for format-specific features

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ implementations)
1616

1717
3.1.0 (not yet released)
1818

19+
#73: (protobuf) Cannot resolve inner types in protoc definitions
20+
(reported by Kenji N)
1921
#649: (cbor, smile) `StreamReadConstraints.maxDocumentLength` not checked
2022
when creating parser with fixed buffer
2123

0 commit comments

Comments
 (0)