This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 2e08bc294d8a CAMEL-27377: camel-protobuf-schema - a clear error for an
import that is not a Google well-known type (#30590)
2e08bc294d8a is described below
commit 2e08bc294d8a9dcc5fb78a19fc045b5740759c4d
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 22 11:46:01 2026 +0100
CAMEL-27377: camel-protobuf-schema - a clear error for an import that is
not a Google well-known type (#30590)
A .proto given with the schema option that imports another file, such as
shop/common.proto, failed with the message of the core loader of Wire:
"Cannot read the protobuf schema: unexpected load: shop/common.proto".
The loader of the schema now turns a failed load into "Cannot read the
protobuf schema: it imports shop/common.proto, but only the well-known types
of google/protobuf can be imported (any.proto, descriptor.proto, ...): put
the types of shop/common.proto in the schema itself". The same message is
used for a google/protobuf file that Wire does not ship (type.proto).
- camel-protobuf-schema - catch only the IllegalStateException of the Wire
loader, and field_mask.proto is not a supported import
The core loader of Wire 6.4.7 throws IllegalStateException("unexpected load:
...") for a file it does not ship; other runtime exceptions are no longer
reported as an unsupported import.
Wire does not ship google/protobuf/field_mask.proto (CoreLoaderKt
isWireRuntimeProto has any, descriptor, duration, empty, struct, timestamp
and
wrappers), so its import fails in the loader and the FieldMask case of
wellKnownType could not be reached: it is removed, the docs list
field_mask.proto among the imports that are not supported, and a test pins
the message, so a Wire upgrade that ships the file shows up.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---
.../apache/camel/catalog/docs/protobuf-schema.adoc | 2 +-
.../src/main/docs/protobuf-schema.adoc | 2 +-
.../protobuf/schema/WireProtobufSchemaParser.java | 31 ++++++++++---
.../schema/ProtobufSchemaDynamicMessageTest.java | 53 ++++++++++++++++++++++
4 files changed, 79 insertions(+), 9 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/protobuf-schema.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/protobuf-schema.adoc
index ccbae72dc84d..5a296ba90533 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/protobuf-schema.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/protobuf-schema.adoc
@@ -52,7 +52,7 @@ A YAML route that turns JSON text into a Protobuf message of
the type `Order`, a
== Supported schemas
-`proto2` and `proto3` files with nested messages, enums, maps, `oneof` and
repeated fields. A schema may import these well-known types of
`google/protobuf`, which are read in their JSON form (a `Timestamp` as RFC 3339
text): `any.proto`, `descriptor.proto`, `duration.proto`, `empty.proto`,
`struct.proto`, `timestamp.proto` and `wrappers.proto`. Other imports are not
supported, including `api.proto`, `source_context.proto` and `type.proto`.
+`proto2` and `proto3` files with nested messages, enums, maps, `oneof` and
repeated fields. A schema may import these well-known types of
`google/protobuf`, which are read in their JSON form (a `Timestamp` as RFC 3339
text): `any.proto`, `descriptor.proto`, `duration.proto`, `empty.proto`,
`struct.proto`, `timestamp.proto` and `wrappers.proto`. Other imports are not
supported, including `api.proto`, `field_mask.proto`, `source_context.proto`
and `type.proto`: the schema is one file, and [...]
== Your own parser
diff --git
a/components/camel-protobuf-schema/src/main/docs/protobuf-schema.adoc
b/components/camel-protobuf-schema/src/main/docs/protobuf-schema.adoc
index ccbae72dc84d..5a296ba90533 100644
--- a/components/camel-protobuf-schema/src/main/docs/protobuf-schema.adoc
+++ b/components/camel-protobuf-schema/src/main/docs/protobuf-schema.adoc
@@ -52,7 +52,7 @@ A YAML route that turns JSON text into a Protobuf message of
the type `Order`, a
== Supported schemas
-`proto2` and `proto3` files with nested messages, enums, maps, `oneof` and
repeated fields. A schema may import these well-known types of
`google/protobuf`, which are read in their JSON form (a `Timestamp` as RFC 3339
text): `any.proto`, `descriptor.proto`, `duration.proto`, `empty.proto`,
`struct.proto`, `timestamp.proto` and `wrappers.proto`. Other imports are not
supported, including `api.proto`, `source_context.proto` and `type.proto`.
+`proto2` and `proto3` files with nested messages, enums, maps, `oneof` and
repeated fields. A schema may import these well-known types of
`google/protobuf`, which are read in their JSON form (a `Timestamp` as RFC 3339
text): `any.proto`, `descriptor.proto`, `duration.proto`, `empty.proto`,
`struct.proto`, `timestamp.proto` and `wrappers.proto`. Other imports are not
supported, including `api.proto`, `field_mask.proto`, `source_context.proto`
and `type.proto`: the schema is one file, and [...]
== Your own parser
diff --git
a/components/camel-protobuf-schema/src/main/java/org/apache/camel/dataformat/protobuf/schema/WireProtobufSchemaParser.java
b/components/camel-protobuf-schema/src/main/java/org/apache/camel/dataformat/protobuf/schema/WireProtobufSchemaParser.java
index a979bd3e9e58..e4c16488dcfc 100644
---
a/components/camel-protobuf-schema/src/main/java/org/apache/camel/dataformat/protobuf/schema/WireProtobufSchemaParser.java
+++
b/components/camel-protobuf-schema/src/main/java/org/apache/camel/dataformat/protobuf/schema/WireProtobufSchemaParser.java
@@ -28,7 +28,6 @@ import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Duration;
import com.google.protobuf.Empty;
-import com.google.protobuf.FieldMask;
import com.google.protobuf.StringValue;
import com.google.protobuf.Struct;
import com.google.protobuf.Timestamp;
@@ -56,6 +55,8 @@ import
org.apache.camel.dataformat.protobuf.ProtobufSchemaParser;
public class WireProtobufSchemaParser implements ProtobufSchemaParser {
private static final String FILE_NAME = "camel-schema.proto";
+ private static final String WELL_KNOWN_TYPES = "any.proto,
descriptor.proto, duration.proto, empty.proto,"
+ + " struct.proto,
timestamp.proto and wrappers.proto";
/**
* The descriptor of the message type of the schema.
@@ -123,7 +124,15 @@ public class WireProtobufSchemaParser implements
ProtobufSchemaParser {
return new Loader() {
@Override
public ProtoFile load(String path) {
- return FILE_NAME.equals(path) ? file : core.load(path);
+ if (FILE_NAME.equals(path)) {
+ return file;
+ }
+ try {
+ return core.load(path);
+ } catch (IllegalStateException e) {
+ // the core loader of Wire only has its own files, and
says "unexpected load: shop/common.proto"
+ throw new
IllegalArgumentException(unsupportedImport(path), e);
+ }
}
@Override
@@ -142,9 +151,7 @@ public class WireProtobufSchemaParser implements
ProtobufSchemaParser {
if (answer == null) {
ProtoFile file = schema.protoFile(path);
if (file == null) {
- throw new IllegalArgumentException(
- "The protobuf schema imports " + path
- + ", but only the
well-known types of google/protobuf can be imported");
+ throw new IllegalArgumentException(unsupportedImport(path));
}
FileDescriptorProto proto = FileDescriptorProto.parseFrom(new
SchemaEncoder(schema).encode(file).toByteArray());
List<FileDescriptor> dependencies = new ArrayList<>();
@@ -157,16 +164,26 @@ public class WireProtobufSchemaParser implements
ProtobufSchemaParser {
return answer;
}
+ /**
+ * The message for an import that is not a well-known type that Wire
ships: the schema is one file, and Camel has no
+ * path to load other .proto files from.
+ */
+ private static String unsupportedImport(String path) {
+ return "Cannot read the protobuf schema: it imports " + path + ", but
only the well-known types of"
+ + " google/protobuf can be imported (" + WELL_KNOWN_TYPES + "):
put the types of " + path
+ + " in the schema itself";
+ }
+
/**
* The well-known types compiled into protobuf-java, so that JsonFormat
prints and parses them in their JSON form,
- * such as a Timestamp as RFC 3339 text.
+ * such as a Timestamp as RFC 3339 text. These are the google/protobuf
files that the core loader of Wire ships:
+ * field_mask.proto is not one of them, so its import fails in the loader
already.
*/
private static FileDescriptor wellKnownType(String path) {
return switch (path) {
case "google/protobuf/any.proto" -> Any.getDescriptor().getFile();
case "google/protobuf/duration.proto" ->
Duration.getDescriptor().getFile();
case "google/protobuf/empty.proto" ->
Empty.getDescriptor().getFile();
- case "google/protobuf/field_mask.proto" ->
FieldMask.getDescriptor().getFile();
case "google/protobuf/struct.proto" ->
Struct.getDescriptor().getFile();
case "google/protobuf/timestamp.proto" ->
Timestamp.getDescriptor().getFile();
case "google/protobuf/wrappers.proto" ->
StringValue.getDescriptor().getFile();
diff --git
a/components/camel-protobuf-schema/src/test/java/org/apache/camel/dataformat/protobuf/schema/ProtobufSchemaDynamicMessageTest.java
b/components/camel-protobuf-schema/src/test/java/org/apache/camel/dataformat/protobuf/schema/ProtobufSchemaDynamicMessageTest.java
index 399f1f06be32..05c4bbcd1647 100644
---
a/components/camel-protobuf-schema/src/test/java/org/apache/camel/dataformat/protobuf/schema/ProtobufSchemaDynamicMessageTest.java
+++
b/components/camel-protobuf-schema/src/test/java/org/apache/camel/dataformat/protobuf/schema/ProtobufSchemaDynamicMessageTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.dataformat.protobuf.schema;
import java.util.Map;
+import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.util.JsonFormat;
import org.apache.camel.builder.RouteBuilder;
@@ -85,6 +86,58 @@ public class ProtobufSchemaDynamicMessageTest extends
CamelTestSupport {
assertTrue(e.getMessage().contains("other.proto"), e.getMessage());
}
+ @Test
+ public void importOfAnotherFileSaysOnlyTheWellKnownTypesCanBeImported() {
+ IllegalArgumentException e =
assertThrows(IllegalArgumentException.class,
+ () -> new WireProtobufSchemaParser().parse(
+ "syntax = \"proto3\"; package shop; import
\"shop/common.proto\";"
+ + " message Order {
shop.Money total = 1; }",
+ null));
+ assertEquals("Cannot read the protobuf schema: it imports
shop/common.proto, but only the well-known types of"
+ + " google/protobuf can be imported (any.proto,
descriptor.proto, duration.proto, empty.proto,"
+ + " struct.proto, timestamp.proto and wrappers.proto):
put the types of"
+ + " shop/common.proto in the schema itself",
+ e.getMessage());
+ }
+
+ @Test
+ public void
importOfAnUnsupportedGoogleFileSaysOnlyTheWellKnownTypesCanBeImported() {
+ IllegalArgumentException e =
assertThrows(IllegalArgumentException.class,
+ () -> new WireProtobufSchemaParser().parse(
+ "syntax = \"proto3\"; import
\"google/protobuf/type.proto\"; message A { string x = 1; }",
+ null));
+ assertTrue(e.getMessage().startsWith("Cannot read the protobuf schema:
it imports google/protobuf/type.proto, but"
+ + " only the well-known types of
google/protobuf can be imported"),
+ e.getMessage());
+ }
+
+ @Test
+ public void importOfFieldMaskSaysOnlyTheWellKnownTypesCanBeImported() {
+ // Wire does not ship google/protobuf/field_mask.proto: if a Wire
upgrade does, it can be added to the list
+ IllegalArgumentException e =
assertThrows(IllegalArgumentException.class,
+ () -> new WireProtobufSchemaParser().parse(
+ "syntax = \"proto3\"; import
\"google/protobuf/field_mask.proto\"; message A { string x = 1; }",
+ null));
+ assertTrue(e.getMessage().startsWith("Cannot read the protobuf schema:
it imports google/protobuf/field_mask.proto,"
+ + " but only the well-known types
of google/protobuf can be imported"),
+ e.getMessage());
+ }
+
+ @Test
+ public void wellKnownTypesCanBeImported() {
+ Descriptor d = new WireProtobufSchemaParser().parse(
+ "syntax = \"proto3\"; import
\"google/protobuf/timestamp.proto\";"
+ + " import
\"google/protobuf/duration.proto\";"
+ + " import
\"google/protobuf/wrappers.proto\";"
+ + " message A {
google.protobuf.Timestamp at = 1;"
+ + "
google.protobuf.Duration took = 2;"
+ + "
google.protobuf.StringValue note = 3; }",
+ null);
+ assertEquals("google.protobuf.Timestamp",
d.findFieldByName("at").getMessageType().getFullName());
+ assertEquals("google.protobuf.Duration",
d.findFieldByName("took").getMessageType().getFullName());
+ assertEquals("google.protobuf.StringValue",
d.findFieldByName("note").getMessageType().getFullName());
+ }
+
@Test
public void syntaxErrorFails() {
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class,