davsclaus opened a new pull request, #30550: URL: https://github.com/apache/camel/pull/30550
[CAMEL-27329](https://issues.apache.org/jira/browse/CAMEL-27329) ## What The protobuf data format (library `GoogleProtobuf`) needs `instanceClass`, a class generated by `protoc`. A low-code route, such as a YAML route or a Kamelet, has no generated classes, so it cannot turn JSON into a Protobuf message. For example, it cannot produce the `com.google.protobuf.Message` that the Confluent and Apicurio serializers need for `kafka` with `schemaFormat=protobuf`. This PR adds two options, used instead of `instanceClass`: - `schema`: the text of a `.proto` file, or a resource such as `classpath:order.proto`, `file:order.proto` or an http URL. - `messageName`: the message type, such as `Order`, `shop.Order` or the nested `Order.Line`. By default it is the first message type of the schema. With `schema` set, the default instance is `DynamicMessage.getDefaultInstance(descriptor)`, so unmarshal (binary data, or JSON with `contentTypeFormat=json`) and marshal (a `DynamicMessage` or a `Map`) work as they do today. ```yaml - unmarshal: protobuf: contentTypeFormat: json schema: classpath:order.proto messageName: shop.Order ``` ## Two modules, so camel-protobuf stays light protobuf-java cannot parse `.proto` text, and the only maintained Java parser is Square Wire's, which brings in Kotlin. - **camel-protobuf** gets the options and a small SPI, `ProtobufSchemaParser` (`Descriptor parse(String schema, String messageName)`). It gets no new dependency. The parser is a single bean of that type in the registry, or else the one named in `META-INF/services/org/apache/camel/protobuf-schema-parser`. If `schema` is set and no parser is found, startup fails with a message that says to add `camel-protobuf-schema` (`camel:protobuf-schema` as a Kamelet dependency). - **camel-protobuf-schema** (new, an "other" module) holds `WireProtobufSchemaParser`, built on `wire-schema-jvm` 6.4.7. That version is already managed in camel-parent and is the one the Confluent and Apicurio protobuf serdes use. Wire's code generators are excluded. Imports of the well-known types of `google/protobuf` map to the descriptors compiled into protobuf-java, so JsonFormat handles them in their JSON form. Other imports fail with a clear message. ## Also in this PR - The options in the model (`ProtobufDataFormat` in camel-core-model and its reifier), with the regenerated XML, YAML and Java DSL files, the XSDs and the catalog. - The docs of the data format, a new "Without generated classes: the schema option" section, the `@BodyType` descriptions, and a page for the new module. - Both Jackson protobuf data formats exclude the two options, as they already exclude `instanceClass`. - **A fix in camel-package-maven-plugin:** `PackageDataFormatMojo` matched `excludeProperties` as a substring of the raw comma list. Because protobuf excludes `schemaResolver`, the new `schema` option was dropped from the metadata. Names are now matched whole. I checked every data format with `excludeProperties`/`includeProperties`, and protobuf is the only one whose generated output changes. No upgrade-guide entry: the change only adds things. ## Tests - camel-protobuf (21 tests, including the new `ProtobufSchemaParserLookupTest`): startup fails without a parser, and a parser bean from the registry is used. - camel-protobuf-schema (7 tests): - proto3 with nested messages, an enum, a map, oneof, repeated fields and an imported `google.protobuf.Timestamp`; - JSON -> DynamicMessage, Map -> binary -> DynamicMessage, and an inline schema with a nested `messageName`; - the options set through the route model and reifier (`dataFormat().protobuf().schema(...).messageName(...)`); - the errors for an unknown message name, a syntax error and an unsupported import. - camel-jackson-protobuf, camel-jackson3-protobuf, camel-package-maven-plugin and camel-catalog (1143) tests pass locally. Note: the parser uses wire's `SchemaEncoder` and `ProtoParser`, which live in `com.squareup.wire.schema.internal`. Those are the same classes that the Confluent and Apicurio serdes use to read `.proto` text. 🤖 Generated with [Claude Code](https://claude.com/claude-code) _Claude Code on behalf of Claus Ibsen_ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
