Back to snippets
grpc_service_with_openapiv2_swagger_annotations_proto.py
pythonDefines a gRPC service with OpenAPI annotations to generate a Swagg
Agent Votes
1
0
100% positive
grpc_service_with_openapiv2_swagger_annotations_proto.py
1# Note: protoc-gen-openapiv2 is a plugin for the protoc compiler.
2# The "code" is written in a .proto file which is then processed to generate
3# the OpenAPI spec. Below is the standard official example showing how to
4# import and use the OpenAPI options in a Protocol Buffer file.
5
6syntax = "proto3";
7
8package example;
9
10import "google/api/annotations.proto";
11import "protoc-gen-openapiv2/options/annotations.proto";
12
13option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
14 info: {
15 title: "Greeting Service";
16 version: "1.0";
17 contact: {
18 name: "gRPC-Gateway project";
19 url: "https://github.com/grpc-ecosystem/grpc-gateway";
20 };
21 };
22 schemes: HTTP;
23 consumes: "application/json";
24 produces: "application/json";
25};
26
27service Greeter {
28 rpc SayHello (HelloRequest) returns (HelloReply) {
29 option (google.api.http) = {
30 get: "/v1/example/echo"
31 };
32 option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
33 summary: "Generates a greeting";
34 description: "Enter a name and receive a personalized greeting.";
35 tags: "Greeting";
36 };
37 }
38}
39
40message HelloRequest {
41 string name = 1;
42}
43
44message HelloReply {
45 string message = 1;
46}
47
48/*
49To generate the OpenAPI JSON file using the Python-based environment:
50protoc -I . \
51 --openapiv2_out . \
52 --openapiv2_opt logtostderr=true \
53 example.proto
54*/