Generate Protobuf schemas and models
Protobuf output is generated from native Protobuf schemas in the contract. The generator does not derive Protobuf from AsyncAPI Schema Objects. Two outputs are available:
.protofiles only, with theprotobuf-schemagenerator; and.protofiles plus Java message classes, and for Kotlin the official Kotlin DSL, with model typeprotobuf-message.
Put the Protobuf schema in the contract
Embed the schema, or reference a .proto file next to the contract:
components:
schemas:
UserCreated:
schemaFormat: application/vnd.google.protobuf;version=3
schema: |
syntax = "proto3";
package com.example.events;
option java_package = "com.example.events";
option java_multiple_files = true;
message UserCreated {
string user_id = 1;
string email = 2;
}
To reference a file instead, write schema: { $ref: user-created.proto }.
The accepted formats are application/vnd.google.protobuf;version=2 and
application/vnd.google.protobuf;version=3. Message generation requires a
top-level message with the payload's name, UserCreated in this example.
Generate .proto files
Maven:
<configuration>
<generatorName>protobuf-schema</generatorName>
<inputSpec>${project.basedir}/src/main/resources/asyncapi.yaml</inputSpec>
<schemaPackage>com.example.events.proto</schemaPackage>
</configuration>
Gradle:
asyncApiGenerator {
executions {
register("proto") {
generatorName.set("protobuf-schema")
inputSpec.set(file("src/main/resources/asyncapi.yaml"))
schemaPackage.set("com.example.events.proto")
}
}
}
CLI:
asyncapi-gen \
--input-spec src/main/resources/asyncapi.yaml \
--generator-name protobuf-schema \
--schema-package com.example.events.proto
The schema content is written unchanged to
com/example/events/proto/UserCreated.proto. schemaPackage selects the
directory only; it does not rewrite the package declaration.
Generate Java messages and the Kotlin DSL
Use a java or kotlin execution with model type protobuf-message:
<configuration>
<generatorName>kotlin</generatorName>
<inputSpec>${project.basedir}/src/main/resources/asyncapi.yaml</inputSpec>
<modelPackage>com.example.events</modelPackage>
<modelConfig>
<modelType>protobuf-message</modelType>
</modelConfig>
</configuration>
In Gradle set modelConfig { modelType.set("protobuf-message") }; in the CLI
pass --model-type protobuf-message. The schema must meet two requirements:
modelPackagemust match the schema's Java package, as described in Native Avro and Protobuf models; and- the schema must set
option java_multiple_files = true.
With java, the result is the Java message classes. With kotlin, it is the
Java message classes plus the Kotlin DSL; the Java classes remain the runtime
types. The application needs com.google.protobuf:protobuf-java 3.25.x, and
for the Kotlin DSL also com.google.protobuf:protobuf-kotlin 3.25.x.
Make protoc available
Message generation runs protoc version 3.25.5. Each frontend resolves it
as described in
Protobuf compiler.
Only executions with protobuf-message need it.
On build machines without repository access, install protoc yourself and
point the generator at it:
| Frontend | Setting |
|---|---|
| Maven | <protocExecutable>/opt/protoc/bin/protoc</protocExecutable> or -Dasyncapi.protocExecutable=... |
| Gradle | asyncApiGenerator { protocExecutable.set(file("/opt/protoc/bin/protoc")) } |
| CLI | --protoc /opt/protoc/bin/protoc |
Use a protoc 3.25.x release so the generated code matches the Protobuf
runtime.
A complete schema example is linked from Native Protobuf schema artifact.