The protoschema-plugins repository contains a collection of Protobuf plugins that generate different types of schema from protobuf files. This includes:
Generates a schema for a given protobuf file that can be used as a PubSub schema in the form of a single self-contained messaged normalized to proto2.
Install the protoc-gen-pubsub plugin directly:
go install github.com/bufbuild/protoschema-plugins/cmd/protoc-gen-pubsub@latestOr reference it as a Remote Plugin in buf.gen.yaml:
version: v1
plugins:
- plugin: buf.build/bufbuild/protoschema-pubsub
out: ./genFor examples see testdata which contains the generated schema for test case definitions found in proto.
Generates a JSON Schema for a given protobuf file. This implementation uses the latest JSON Schema Draft 2020-12.
Install the protoc-gen-jsonschema directly:
go install github.com/bufbuild/protoschema-plugins/cmd/protoc-gen-jsonschema@latestOr reference it as a Remote Plugin in buf.gen.yaml:
version: v1
plugins:
- plugin: buf.build/bufbuild/protoschema-jsonschema
out: ./genFor examples see testdata which contains the generated schema for test case definitions found in proto.
Here is a simple generated schema from the following protobuf:
message Product {
message Location {
float lat = 1;
float long = 2;
}
int32 product_id = 1;
string product_name = 2;
float price = 3;
repeated string tags = 4;
Location location = 5;
}Results in the following JSON Schema files:
Product.schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"product_id": {
"type": "integer"
},
"product_name": {
"type": "string"
},
"price": {
"type": "number"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"location": {
"type": "object",
"properties": {
"lat": {
"type": "number"
},
"long": {
"type": "number"
}
},
"required": ["lat", "long"]
}
},
"required": ["product_id", "product_name", "price", "tags", "location"]
}Product.Location.schema.json
{
"$id": "Product.Location.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"additionalProperties": false,
"properties": {
"lat": {
"anyOf": [
{
"type": "number"
},
{
"type": "string"
},
{
"enum": ["NaN", "Infinity", "-Infinity"],
"type": "string"
}
]
},
"long": {
"anyOf": [
{
"type": "number"
},
{
"type": "string"
},
{
"enum": ["NaN", "Infinity", "-Infinity"],
"type": "string"
}
]
}
},
"type": "object"
}The sourceinfo plugin provides a protoc-gen-sourceinfo command that generates an .sourceinfo.binpb file, for every .proto file.
Each generated file contains the associated serialized google.protobuf.SourceCodeInfo value.
These files can then be loaded directly or embedded in a binary.
The following helpers are provided for go via import "github.com/bufbuild/protoschema-plugins/sourceinfo":
sourceinfo.GlobalFiles(): A drop-in replacement for protoregistry.GlobalFilessourceinfo.GlobalTypes(): A drop-in replacement for protogregistry.GlobalTypessourceinfo.RegisterAll(root string): Registers all*.sourceinfo.binpbfiles found under the root dir.sourceinfo.RegisterAllFS(fsys fs.FS, root string): Same as above, except uses the given file system.
For help and discussion around Protobuf, best practices, and more, join us on Slack.
This project is currently in alpha. The API should be considered unstable and likely to change.
Offered under the Apache 2 license.