Search in sources :

Example 1 with ProtobufSchema

use of io.apicurio.registry.utils.protobuf.schema.ProtobufSchema in project apicurio-registry by Apicurio.

the class ProtobufSchemaParser method parseSchema.

/**
 * @see io.apicurio.registry.serde.SchemaParser#parseSchema(byte[])
 */
@Override
public ProtobufSchema parseSchema(byte[] rawSchema) {
    try {
        // textual .proto file
        ProtoFileElement fileElem = ProtoParser.Companion.parse(FileDescriptorUtils.DEFAULT_LOCATION, IoUtil.toString(rawSchema));
        FileDescriptor fileDescriptor = FileDescriptorUtils.protoFileToFileDescriptor(fileElem);
        return new ProtobufSchema(fileDescriptor, fileElem);
    } catch (DescriptorValidationException pe) {
        throw new SerializationException("Error parsing protobuf schema ", pe);
    }
}
Also used : SerializationException(org.apache.kafka.common.errors.SerializationException) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) ProtobufSchema(io.apicurio.registry.utils.protobuf.schema.ProtobufSchema) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor)

Example 2 with ProtobufSchema

use of io.apicurio.registry.utils.protobuf.schema.ProtobufSchema in project apicurio-registry by Apicurio.

the class ProtobufSchemaParser method getSchemaFromData.

/**
 * @see io.apicurio.registry.resolver.SchemaParser#getSchemaFromData(java.lang.Object)
 */
@Override
public ParsedSchema<ProtobufSchema> getSchemaFromData(Record<U> data) {
    FileDescriptor schemaFileDescriptor = data.payload().getDescriptorForType().getFile();
    ProtoFileElement protoFileElement = toProtoFileElement(schemaFileDescriptor);
    ProtobufSchema protobufSchema = new ProtobufSchema(schemaFileDescriptor, protoFileElement);
    byte[] rawSchema = IoUtil.toBytes(protoFileElement.toSchema());
    return new ParsedSchemaImpl<ProtobufSchema>().setParsedSchema(protobufSchema).setReferenceName(protobufSchema.getFileDescriptor().getName()).setSchemaReferences(handleDependencies(schemaFileDescriptor)).setRawSchema(rawSchema);
}
Also used : ParsedSchemaImpl(io.apicurio.registry.resolver.ParsedSchemaImpl) ProtobufSchema(io.apicurio.registry.utils.protobuf.schema.ProtobufSchema) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor)

Example 3 with ProtobufSchema

use of io.apicurio.registry.utils.protobuf.schema.ProtobufSchema in project apicurio-registry by Apicurio.

the class ProtobufSchemaParser method parseSchema.

/**
 * @see io.apicurio.registry.serde.SchemaParser#parseSchema(byte[])
 */
@Override
public ProtobufSchema parseSchema(byte[] rawSchema, Map<String, ParsedSchema<ProtobufSchema>> resolvedReferences) {
    try {
        // textual .proto file
        ProtoFileElement fileElem = ProtoParser.Companion.parse(FileDescriptorUtils.DEFAULT_LOCATION, IoUtil.toString(rawSchema));
        Map<String, ProtoFileElement> dependencies = new HashMap<>();
        resolvedReferences.forEach((key, value) -> {
            dependencies.put(key, value.getParsedSchema().getProtoFileElement());
            if (value.hasReferences()) {
                addReferencesToDependencies(value.getSchemaReferences(), dependencies);
            }
        });
        MessageElement firstMessage = FileDescriptorUtils.firstMessage(fileElem);
        if (firstMessage != null) {
            try {
                final Descriptors.Descriptor fileDescriptor = FileDescriptorUtils.toDescriptor(firstMessage.getName(), fileElem, dependencies);
                return new ProtobufSchema(fileDescriptor.getFile(), fileElem);
            } catch (IllegalStateException ise) {
                // If we fail to init the dynamic schema, try to get the descriptor from the proto element
                return getFileDescriptorFromElement(fileElem);
            }
        } else {
            return getFileDescriptorFromElement(fileElem);
        }
    } catch (DescriptorValidationException pe) {
        throw new SerializationException("Error parsing protobuf schema ", pe);
    }
}
Also used : SerializationException(org.apache.kafka.common.errors.SerializationException) HashMap(java.util.HashMap) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) ProtobufSchema(io.apicurio.registry.utils.protobuf.schema.ProtobufSchema) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) Descriptors(com.google.protobuf.Descriptors) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement)

Example 4 with ProtobufSchema

use of io.apicurio.registry.utils.protobuf.schema.ProtobufSchema in project apicurio-registry by Apicurio.

the class ProtobufSchemaParser method handleDependencies.

private List<ParsedSchema<ProtobufSchema>> handleDependencies(FileDescriptor fileDescriptor) {
    List<ParsedSchema<ProtobufSchema>> schemaReferences = new ArrayList<>();
    fileDescriptor.getDependencies().forEach(referenceFileDescriptor -> {
        ProtoFileElement referenceProtoFileElement = toProtoFileElement(referenceFileDescriptor);
        ProtobufSchema referenceProtobufSchema = new ProtobufSchema(referenceFileDescriptor, referenceProtoFileElement);
        byte[] rawSchema = IoUtil.toBytes(referenceProtoFileElement.toSchema());
        ParsedSchema<ProtobufSchema> referencedSchema = new ParsedSchemaImpl<ProtobufSchema>().setParsedSchema(referenceProtobufSchema).setReferenceName(referenceProtobufSchema.getFileDescriptor().getName()).setSchemaReferences(handleDependencies(referenceFileDescriptor)).setRawSchema(rawSchema);
        schemaReferences.add(referencedSchema);
    });
    return schemaReferences;
}
Also used : ParsedSchemaImpl(io.apicurio.registry.resolver.ParsedSchemaImpl) ArrayList(java.util.ArrayList) ProtobufSchema(io.apicurio.registry.utils.protobuf.schema.ProtobufSchema) ParsedSchema(io.apicurio.registry.resolver.ParsedSchema) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement)

Aggregations

ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)4 ProtobufSchema (io.apicurio.registry.utils.protobuf.schema.ProtobufSchema)4 DescriptorValidationException (com.google.protobuf.Descriptors.DescriptorValidationException)2 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)2 ParsedSchemaImpl (io.apicurio.registry.resolver.ParsedSchemaImpl)2 SerializationException (org.apache.kafka.common.errors.SerializationException)2 Descriptors (com.google.protobuf.Descriptors)1 MessageElement (com.squareup.wire.schema.internal.parser.MessageElement)1 ParsedSchema (io.apicurio.registry.resolver.ParsedSchema)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1