|
| 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 |
0 commit comments