Registry / serialization / protoc-gen-openapiv2

protoc-gen-openapiv2

JSON →
library0.0.1pypypiunverified

This Python package provides a convenient wrapper to install the `protoc-gen-openapiv2` binary. The binary generates OpenAPI v2 (Swagger) definitions directly from Protocol Buffer service definitions, making it a critical component for gRPC Gateway projects to expose gRPC services as RESTful APIs. It is currently at version 0.0.1, with development closely tied to the upstream Go project.

pip install protoc-gen-openapiv2
INSTALL
IMPORT
SIG · PROTOC-GEN-OPENAPI
P
protoc-gen-openapiv2
serializationpythonv0.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

This quickstart demonstrates how to use the `protoc-gen-openapiv2` plugin via a Python script. It creates a dummy `.proto` file, then executes the `protoc` command to generate an OpenAPI v2 specification. This highlights that the package primarily provides a command-line tool, not Python symbols for direct import. Ensure `protoc` is installed and `protoc-gen-openapiv2` is in your system's PATH.

import subprocess import os import sys # --- Prerequisites Check --- # This library's core function is a 'protoc' plugin, so 'protoc' and the plugin # binary must be available in your system's PATH. def check_command(cmd_name): return subprocess.run(["which", cmd_name], capture_output=True).returncode == 0 if not check_command("protoc"): print("Error: 'protoc' (Protocol Buffer compiler) not found in PATH.", file=sys.stderr) print("Please install protoc. E.g., on Debian/Ubuntu: 'sudo apt install protobuf-compiler'", file=sys.stderr) sys.exit(1) if not check_command("protoc-gen-openapiv2"): print("Error: 'protoc-gen-openapiv2' not found in PATH.", file=sys.stderr) print("This Python package installs the binary, but you may need to add its install location (e.g., ~/.local/bin) to your PATH.", file=sys.stderr) sys.exit(1) # --- 1. Create a dummy .proto file for demonstration --- proto_file = "example.proto" output_dir = "./openapi_output" swagger_output_file = os.path.join(output_dir, "example.swagger.json") proto_content = """ syntax = "proto3"; package example; import "google/api/annotations.proto"; // Essential for gRPC Gateway HTTP annotations service MyService { rpc MyMethod (MyRequest) returns (MyResponse) { option (google.api.http) = { get: "/v1/example/{query}" }; } } message MyRequest { string query = 1; } message MyResponse { string result = 1; } """ with open(proto_file, "w") as f: f.write(proto_content) os.makedirs(output_dir, exist_ok=True) # --- 2. Run protoc with the openapiv2 plugin --- # The 'google/api/annotations.proto' needs to be discoverable by protoc. # It's typically found in your protoc installation or the grpc-gateway repo. # If not, you might need to add: -I/path/to/grpc-gateway/third_party/googleapis protoc_command = [ "protoc", "-I.", # Look for proto files in the current directory f"--openapiv2_out={output_dir}", # Output directory for OpenAPI spec "--openapiv2_opt=logtostderr=true", # Optional: log to stderr for debugging proto_file ] print(f"Executing command: {' '.join(protoc_command)}\n") try: result = subprocess.run(protoc_command, check=True, capture_output=True, text=True) print("STDOUT:\n", result.stdout) if result.stderr: print("STDERR:\n", result.stderr) # Often contains warnings or logs from the plugin print(f"\nSuccessfully generated OpenAPI v2 spec at: {swagger_output_file}") except subprocess.CalledProcessError as e: print(f"Error generating OpenAPI spec: {e}", file=sys.stderr) print(f"Command: {' '.join(e.cmd)}", file=sys.stderr) print(f"STDOUT:\n{e.stdout}", file=sys.stderr) print(f"STDERR:\n{e.stderr}", file=sys.stderr) sys.exit(1) except FileNotFoundError as e: print(f"Error: Command not found. {e}", file=sys.stderr) sys.exit(1) finally: # --- Clean up --- if os.path.exists(proto_file): os.remove(proto_file) print(f"\nCleaned up temporary '{proto_file}'. The output spec remains in '{output_dir}'.")
protoc-gen-openapiv2 --version
Debug
Known issues
gotchaThe `protoc-gen-openapiv2` Python package primarily installs a command-line binary. Its core functionality is invoked via the `protoc` command, not by importing Python modules and calling functions directly.
fix
Always interact with `protoc-gen-openapiv2` through the `protoc` command-line tool, typically within a build script or Makefile. Do not expect to `import protoc_gen_openapiv2` in your Python application code.
affects: 0.0.1
gotchaThe `protoc` (Protocol Buffer compiler) binary must be installed on your system and accessible in your system's PATH for this plugin to function. This Python package only installs the OpenAPI v2 generator plugin, not `protoc` itself.
fix
Install `protoc` separately. For example, on Debian/Ubuntu: `sudo apt install protobuf-compiler`. For other systems, refer to the official Protocol Buffers documentation.
affects: 0.0.1
gotchaAfter installing `protoc-gen-openapiv2` via `pip`, the `protoc-gen-openapiv2` binary might be installed into a user-specific directory (e.g., `~/.local/bin` in Linux or `AppData\Roaming\Python\Scripts` in Windows for Python >= 3.3). This directory might not be in your system's PATH by default.
fix
Ensure the directory where `pip` installs binaries is included in your system's PATH environment variable. Alternatively, call the binary using its full path.
affects: 0.0.1
gotchaWhen using `protoc`, correctly specifying include paths (`-I` flags) for all `.proto` files, especially imported ones like `google/api/annotations.proto` (common for gRPC Gateway), is critical. Incorrect paths will lead to `File not found` errors during generation.
fix
Verify that all directories containing `.proto` files are correctly added to the `protoc` command with `-I` flags. For `google/api/annotations.proto`, you might need to clone the `grpc-ecosystem/grpc-gateway` repository and include its `third_party/googleapis` directory.
affects: 0.0.1
breakingGiven its early version `0.0.1`, future updates (even minor releases) are likely to introduce breaking changes to command-line options, generated output, or dependencies without strictly adhering to semantic versioning guidelines.
fix
Pin your `protoc-gen-openapiv2` version in your `requirements.txt` or build environment to avoid unexpected breakage. Thoroughly test when upgrading to new versions.
affects: <1.0.0
Errors
Common errors & fixes
protoc-gen-openapiv2: program not found or is not executable
The `protoc-gen-openapiv2` binary is either not installed, not in your system's PATH, or lacks execution permissions.
fix
Ensure the `protoc-gen-openapiv2` Python package is installed via `pip install protoc-gen-openapiv2`, which downloads the binary. Then, verify that the directory containing the installed binary (e.g., `~/.local/bin` or a Python virtual environment's `bin` directory) is included in your system's PATH environment variable. You might also need to explicitly `chmod +x` the binary if it has permission issues. If installing manually (without the Python wrapper), use `go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest` and ensure `$GOPATH/bin` is in your PATH.
Import "protoc-gen-openapiv2/options/annotations.proto" was not found or had errors.
The `protoc` compiler cannot locate the required `.proto` files that define the OpenAPI annotations, typically because the include path (`-I` or `--proto_path`) is not correctly configured.
fix
You need to provide the `protoc` compiler with the correct path to the `annotations.proto` and `openapiv2.proto` files. These files are usually located within the `protoc-gen-openapiv2/options` directory of the `grpc-gateway` repository. The Python package `protoc-gen-openapiv2` makes these available. You should include the directory containing the `protoc-gen-openapiv2/options` folder in your `protoc` command using the `-I` flag. For example, if you've copied these files to a `third_party/googleapis` directory in your project, your command might look like `protoc -I. -I./third_party/googleapis --openapiv2_out=. your_service.proto`.
go: go.mod file not found in current directory or any parent directory
This error occurs when attempting to use Go module-aware commands like `go get` or `go install` to fetch the `protoc-gen-openapiv2` binary directly, but you are not inside a Go module or have a malformed module path.
fix
If you intend to install the `protoc-gen-openapiv2` binary as a global tool (and not as a dependency of a specific Go module), use `go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest`. This command should work outside of a Go module. If you are developing a Go project and want to manage it as a module dependency, ensure you are in a directory with a `go.mod` file (created with `go mod init <your_module_path>`). For this specific Python wrapper library, consider using `pip install protoc-gen-openapiv2` as the recommended way to get the binary, which abstracts away Go-specific installation details.
protoc-gen-openapiv2: command not found
The protoc-gen-openapiv2 executable is not in your shell's PATH environment variable, or the installation via the Python wrapper failed to place it in an accessible location.
fix
Add the directory containing the binary (e.g., `~/.local/bin` after `pip install`) to your system's `PATH`, or ensure your Python environment's `bin` directory is sourced. For convenience, running `python -m protoc_gen_openapiv2.install` often prints the binary's full path.
google/api/annotations.proto: File not found.
The `protoc` compiler cannot find the standard `google/api` proto files, which are essential for `protoc-gen-openapiv2` to process HTTP rule annotations.
fix
Download or clone the `googleapis` repository (e.g., `github.com/googleapis/googleapis`) and provide its root directory to `protoc` using the `-I` flag; for example: `protoc -I. -I/path/to/googleapis ...`.
Upgrade
Version history
0.0.1latest on PyPI · released Dec 2, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
protoc-gen-openapiv2 — pip install protoc-gen-openapiv2 · libregistry