davsclaus commented on code in PR #30550: URL: https://github.com/apache/camel/pull/30550#discussion_r4652255233
########## components/camel-protobuf-schema/src/main/java/org/apache/camel/dataformat/protobuf/schema/WireProtobufSchemaParser.java: ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dataformat.protobuf.schema; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.google.protobuf.Any; +import com.google.protobuf.DescriptorProtos; +import com.google.protobuf.DescriptorProtos.FileDescriptorProto; +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; +import com.squareup.wire.schema.CoreLoader; +import com.squareup.wire.schema.ErrorCollector; +import com.squareup.wire.schema.Linker; +import com.squareup.wire.schema.Loader; +import com.squareup.wire.schema.Location; +import com.squareup.wire.schema.ProtoFile; +import com.squareup.wire.schema.Schema; +import com.squareup.wire.schema.internal.SchemaEncoder; +import com.squareup.wire.schema.internal.parser.ProtoParser; Review Comment: Done in 37cb29464eae: the class Javadoc says that SchemaEncoder and ProtoParser come from Wire's internal package and that a Wire upgrade must be checked with this module's tests. _Claude Code on behalf of Claus Ibsen_ ########## components/camel-protobuf-schema/src/main/java/org/apache/camel/dataformat/protobuf/schema/WireProtobufSchemaParser.java: ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dataformat.protobuf.schema; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.google.protobuf.Any; +import com.google.protobuf.DescriptorProtos; +import com.google.protobuf.DescriptorProtos.FileDescriptorProto; +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; +import com.squareup.wire.schema.CoreLoader; +import com.squareup.wire.schema.ErrorCollector; +import com.squareup.wire.schema.Linker; +import com.squareup.wire.schema.Loader; +import com.squareup.wire.schema.Location; +import com.squareup.wire.schema.ProtoFile; +import com.squareup.wire.schema.Schema; +import com.squareup.wire.schema.internal.SchemaEncoder; +import com.squareup.wire.schema.internal.parser.ProtoParser; +import org.apache.camel.dataformat.protobuf.ProtobufSchemaParser; + +/** + * The {@link ProtobufSchemaParser} of the protobuf data format, built on the .proto parser of Square Wire: it parses + * and links the .proto text, encodes it as a FileDescriptorProto and builds the descriptor with protobuf-java, so that + * a message can be read and written as a DynamicMessage without a class generated by protoc. The file may import the + * well-known types of google/protobuf, such as timestamp.proto; other imports are not supported. + */ +public class WireProtobufSchemaParser implements ProtobufSchemaParser { + + private static final String FILE_NAME = "camel-schema.proto"; + + /** + * The descriptor of the message type of the schema. + * + * @param schema the text of a .proto file + * @param messageName the name of the message type, simple or with its package; null for the first message type of + * the file + */ + @Override + public Descriptor parse(String schema, String messageName) { + FileDescriptor file = parseFile(schema); + if (messageName == null || messageName.isBlank()) { + if (file.getMessageTypes().isEmpty()) { + throw new IllegalArgumentException("The protobuf schema has no message type"); + } + return file.getMessageTypes().get(0); + } + String name = messageName.trim(); + String pkg = file.getPackage(); + if (!pkg.isEmpty() && name.startsWith(pkg + ".")) { + name = name.substring(pkg.length() + 1); + } + Descriptor answer = findMessage(file, name); + if (answer == null) { + throw new IllegalArgumentException("The protobuf schema has no message type " + messageName); + } + return answer; + } + + private static Descriptor findMessage(FileDescriptor file, String name) { + // a nested type is Outer.Inner + String[] parts = name.split("\\."); + Descriptor d = file.findMessageTypeByName(parts[0]); + for (int i = 1; d != null && i < parts.length; i++) { + d = d.findNestedTypeByName(parts[i]); + } + return d; + } + + static FileDescriptor parseFile(String schema) { + ProtoFile file; + try { + file = ProtoFile.Companion.get(ProtoParser.Companion.parse(Location.get(FILE_NAME), schema)); + } catch (RuntimeException e) { + // wire reports a syntax error as an IllegalStateException + throw new IllegalArgumentException("Cannot read the protobuf schema: " + e.getMessage(), e); + } + Loader loader = new Loader() { + @Override + public ProtoFile load(String path) { + return FILE_NAME.equals(path) ? file : CoreLoader.INSTANCE.load(path); + } + + @Override + public Loader withErrors(ErrorCollector errors) { + return this; Review Comment: Fixed in 37cb29464eae: `withErrors` now returns a loader that wraps `CoreLoader.INSTANCE.withErrors(errors)`, so the linker's collector reaches the core loader. _Claude Code on behalf of Claus Ibsen_ ########## components/camel-protobuf-schema/src/main/java/org/apache/camel/dataformat/protobuf/schema/WireProtobufSchemaParser.java: ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dataformat.protobuf.schema; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.google.protobuf.Any; +import com.google.protobuf.DescriptorProtos; +import com.google.protobuf.DescriptorProtos.FileDescriptorProto; +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; +import com.squareup.wire.schema.CoreLoader; +import com.squareup.wire.schema.ErrorCollector; +import com.squareup.wire.schema.Linker; +import com.squareup.wire.schema.Loader; +import com.squareup.wire.schema.Location; +import com.squareup.wire.schema.ProtoFile; +import com.squareup.wire.schema.Schema; +import com.squareup.wire.schema.internal.SchemaEncoder; +import com.squareup.wire.schema.internal.parser.ProtoParser; +import org.apache.camel.dataformat.protobuf.ProtobufSchemaParser; + +/** + * The {@link ProtobufSchemaParser} of the protobuf data format, built on the .proto parser of Square Wire: it parses + * and links the .proto text, encodes it as a FileDescriptorProto and builds the descriptor with protobuf-java, so that + * a message can be read and written as a DynamicMessage without a class generated by protoc. The file may import the + * well-known types of google/protobuf, such as timestamp.proto; other imports are not supported. + */ +public class WireProtobufSchemaParser implements ProtobufSchemaParser { + + private static final String FILE_NAME = "camel-schema.proto"; + + /** + * The descriptor of the message type of the schema. + * + * @param schema the text of a .proto file + * @param messageName the name of the message type, simple or with its package; null for the first message type of + * the file + */ + @Override + public Descriptor parse(String schema, String messageName) { + FileDescriptor file = parseFile(schema); + if (messageName == null || messageName.isBlank()) { + if (file.getMessageTypes().isEmpty()) { + throw new IllegalArgumentException("The protobuf schema has no message type"); + } + return file.getMessageTypes().get(0); + } + String name = messageName.trim(); + String pkg = file.getPackage(); + if (!pkg.isEmpty() && name.startsWith(pkg + ".")) { + name = name.substring(pkg.length() + 1); + } + Descriptor answer = findMessage(file, name); + if (answer == null) { + throw new IllegalArgumentException("The protobuf schema has no message type " + messageName); + } + return answer; + } + + private static Descriptor findMessage(FileDescriptor file, String name) { + // a nested type is Outer.Inner + String[] parts = name.split("\\."); + Descriptor d = file.findMessageTypeByName(parts[0]); + for (int i = 1; d != null && i < parts.length; i++) { + d = d.findNestedTypeByName(parts[i]); + } + return d; + } + + static FileDescriptor parseFile(String schema) { + ProtoFile file; + try { + file = ProtoFile.Companion.get(ProtoParser.Companion.parse(Location.get(FILE_NAME), schema)); + } catch (RuntimeException e) { + // wire reports a syntax error as an IllegalStateException + throw new IllegalArgumentException("Cannot read the protobuf schema: " + e.getMessage(), e); + } + Loader loader = new Loader() { + @Override + public ProtoFile load(String path) { + return FILE_NAME.equals(path) ? file : CoreLoader.INSTANCE.load(path); + } + + @Override + public Loader withErrors(ErrorCollector errors) { + return this; + } + }; + try { + Schema linked = new Linker(loader, new ErrorCollector(), false, true).link(List.of(file)); + return build(linked, FILE_NAME, new HashMap<>()); + } catch (IllegalArgumentException e) { + throw e; + } catch (Exception e) { + // such as an IllegalStateException of wire for an unknown type or import + throw new IllegalArgumentException("Cannot read the protobuf schema: " + e.getMessage(), e); + } + } + + private static FileDescriptor build(Schema schema, String path, Map<String, FileDescriptor> built) throws Exception { + FileDescriptor answer = built.get(path); + if (answer != null) { + return answer; + } + answer = wellKnownType(path); + 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"); + } + FileDescriptorProto proto = FileDescriptorProto.parseFrom(new SchemaEncoder(schema).encode(file).toByteArray()); + List<FileDescriptor> dependencies = new ArrayList<>(); + for (String dependency : proto.getDependencyList()) { + dependencies.add(build(schema, dependency, built)); + } + answer = FileDescriptor.buildFrom(proto, dependencies.toArray(new FileDescriptor[0])); + } + built.put(path, answer); + return answer; + } + + /** + * 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. + */ + 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(); + case "google/protobuf/descriptor.proto" -> DescriptorProtos.getDescriptor(); + default -> null; Review Comment: `api.proto`, `source_context.proto` and `type.proto` are not in the core loader of Wire 6.4.7 (it ships any, descriptor, duration, empty, struct, timestamp and wrappers), so they cannot be linked without bundling their .proto text. In 37cb29464eae the docs and the class Javadoc list the supported imports and name these three as not supported. _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]
