Skip to content

Commit 750d51d

Browse files
authored
feat: generate OpenAPI v3 spec (#34)
2 parents 02e6c9a + 6ea67d5 commit 750d51d

File tree

27 files changed

+6140
-2148
lines changed

27 files changed

+6140
-2148
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
*.sw*
44
node_modules
55
!gen
6+
/buf-plugin-openapi/bin
7+
/openapi-debug.log

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ deps/go:
2020
${GO} mod tidy
2121
npm install
2222

23+
pre-build:
24+
cd protocol-apis-annotations && make proto
25+
cd buf-plugin-openapi && make install
2326

2427
.PHONY: proto
25-
proto:
28+
proto: pre-build
2629
rm -rf gen/python || true
2730
rm -rf gen/pre-python || true
2831
buf generate protos

buf-plugin-openapi/Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.PHONY: build clean test install generate
2+
3+
# Binary name
4+
BINARY_NAME=protoc-gen-buf-plugin-openapi
5+
6+
# Build directory
7+
BUILD_DIR=./bin
8+
9+
# Go build flags
10+
GO_BUILD_FLAGS=-v
11+
12+
# Installation directory (this should be in your PATH)
13+
INSTALL_DIR=$(HOME)/.gvm/pkgsets/go1.23.6/global/bin
14+
15+
# Ensure build directory exists
16+
$(BUILD_DIR):
17+
mkdir -p $(BUILD_DIR)
18+
19+
# Build the binary
20+
build: $(BUILD_DIR)
21+
go build $(GO_BUILD_FLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)
22+
23+
# Install the binary to GOPATH/bin for buf to find it
24+
install: build-all
25+
mkdir -p $(INSTALL_DIR)
26+
cp $(BUILD_DIR)/$(BINARY_NAME) $(INSTALL_DIR)/protoc-gen-buf-plugin-openapi
27+
28+
# Clean build artifacts
29+
clean:
30+
rm -rf $(BUILD_DIR)
31+
32+
# Test the plugin with buf
33+
test: install
34+
cd .. && buf generate
35+
36+
PLUGIN_NAME := buf-plugin-openapi
37+
38+
generate:
39+
protoc --go_out=. --go_opt=paths=source_relative proto/annotations.proto
40+
41+
.PHONY: build-all
42+
build-all: build

buf-plugin-openapi/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# buf-plugin-openapi
2+
3+
A [buf](https://buf.build) plugin that generates OpenAPI v3 specifications from Protocol Buffer definitions. This plugin is designed to work with gRPC-Gateway annotations to create accurate OpenAPI/Swagger documentation.
4+
5+
## Features
6+
7+
- Generates OpenAPI v3.0.0 specifications from Protocol Buffer definitions
8+
- Supports gRPC-Gateway HTTP annotations
9+
- Converts Protocol Buffer types to OpenAPI types
10+
- Handles path parameters and query parameters
11+
- Supports JSON field name customization
12+
- Generates proper request/response schemas
13+
14+
## Installation
15+
16+
```bash
17+
go install github.com/seanmcgary/buf-plugin-openapi@latest
18+
```
19+
20+
## Usage
21+
22+
Add the plugin to your `buf.yaml`:
23+
24+
```yaml
25+
version: v1
26+
deps:
27+
- buf.build/googleapis/googleapis
28+
- buf.build/grpc-ecosystem/grpc-gateway
29+
plugins:
30+
- plugin: buf-plugin-openapi
31+
out: gen/openapi
32+
opt:
33+
- emit_defaults=true
34+
- json_names_for_fields=true
35+
```
36+
37+
Available options:
38+
39+
- `emit_defaults`: Include default values in the OpenAPI output (default: true)
40+
- `omit_enum_default_value`: Omit default values for enum types (default: false)
41+
- `enums_as_ints`: Render enum values as integers instead of strings (default: false)
42+
- `simple_operation_ids`: Remove the service prefix from operation IDs (default: false)
43+
- `json_names_for_fields`: Use JSON names for fields instead of proto names (default: true)
44+
45+
## Example
46+
47+
Given a Protocol Buffer definition with gRPC-Gateway annotations:
48+
49+
```protobuf
50+
syntax = "proto3";
51+
52+
package example.v1;
53+
54+
import "google/api/annotations.proto";
55+
56+
service ExampleService {
57+
rpc GetExample(GetExampleRequest) returns (GetExampleResponse) {
58+
option (google.api.http) = {
59+
get: "/v1/examples/{id}"
60+
};
61+
}
62+
}
63+
64+
message GetExampleRequest {
65+
string id = 1;
66+
}
67+
68+
message GetExampleResponse {
69+
string id = 1;
70+
string name = 2;
71+
int32 count = 3;
72+
}
73+
```
74+
75+
The plugin will generate an OpenAPI v3 specification with proper paths, parameters, and schemas.
76+
77+
## License
78+
79+
MIT

buf-plugin-openapi/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/Layr-Labs/buf-plugin-openapi
2+
3+
go 1.23.6
4+
5+
require (
6+
github.com/Layr-Labs/protocol-apis-annotations v0.0.0-00010101000000-000000000000
7+
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80
8+
google.golang.org/protobuf v1.36.5
9+
)
10+
11+
require google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect
12+
13+
replace github.com/Layr-Labs/protocol-apis-annotations => ../protocol-apis-annotations

buf-plugin-openapi/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
2+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
4+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5+
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 h1:KAeGQVN3M9nD0/bQXnr/ClcEMJ968gUXJQ9pwfSynuQ=
6+
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80/go.mod h1:cc8bqMqtv9gMOr0zHg2Vzff5ULhhL2IXP4sbcn32Dro=
7+
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 h1:Lj5rbfG876hIAYFjqiJnPHfhXbv+nzTWfm04Fg/XSVU=
8+
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80/go.mod h1:4jWUdICTdgc3Ibxmr8nAJiiLHwQBY0UI0XZcEMaFKaA=
9+
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
10+
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=

0 commit comments

Comments
 (0)