Leave a Comment
Complete list of swagger options to protobuf file
Here is an example with many options that help generate proper swagger out of protofile.
Original URL — https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/examples/internal/proto/examplepb/a_bit_of_everything.proto.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
syntax = "proto3"; option go_package = "examplepb"; package grpc.gateway.examples.internal.examplepb; import "google/api/annotations.proto"; import "google/protobuf/field_mask.proto"; import "google/protobuf/empty.proto"; import "google/protobuf/duration.proto"; import "google/protobuf/wrappers.proto"; import "examples/internal/proto/pathenum/path_enum.proto"; import "examples/internal/proto/sub/message.proto"; import "examples/internal/proto/sub2/message.proto"; import "google/protobuf/timestamp.proto"; import "protoc-gen-swagger/options/annotations.proto"; option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = { info: { title: "A Bit of Everything"; version: "1.0"; contact: { name: "gRPC-Gateway project"; url: "https://github.com/grpc-ecosystem/grpc-gateway"; email: "none@example.com"; }; license: { name: "BSD 3-Clause License"; url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/master/LICENSE.txt"; }; extensions: { key: "x-something-something"; value { string_value: "yadda"; } } }; // Overwriting host entry breaks tests, so this is not done here. external_docs: { url: "https://github.com/grpc-ecosystem/grpc-gateway"; description: "More about gRPC-Gateway"; } schemes: HTTP; schemes: HTTPS; schemes: WSS; consumes: "application/json"; consumes: "application/x-foo-mime"; produces: "application/json"; produces: "application/x-foo-mime"; security_definitions: { security: { key: "BasicAuth"; value: { type: TYPE_BASIC; } } security: { key: "ApiKeyAuth"; value: { type: TYPE_API_KEY; in: IN_HEADER; name: "X-API-Key"; extensions: { key: "x-amazon-apigateway-authtype"; value { string_value: "oauth2"; } } extensions: { key: "x-amazon-apigateway-authorizer"; value { struct_value { fields { key: "type"; value { string_value: "token"; } } fields { key: "authorizerResultTtlInSeconds"; value { number_value: 60; } } } } } } } security: { key: "OAuth2"; value: { type: TYPE_OAUTH2; flow: FLOW_ACCESS_CODE; authorization_url: "https://example.com/oauth/authorize"; token_url: "https://example.com/oauth/token"; scopes: { scope: { key: "read"; value: "Grants read access"; } scope: { key: "write"; value: "Grants write access"; } scope: { key: "admin"; value: "Grants read and write access to administrative information"; } } } } } security: { security_requirement: { key: "BasicAuth"; value: {}; } security_requirement: { key: "ApiKeyAuth"; value: {}; } } security: { security_requirement: { key: "OAuth2"; value: { scope: "read"; scope: "write"; } } security_requirement: { key: "ApiKeyAuth"; value: {}; } } responses: { key: "403"; value: { description: "Returned when the user does not have permission to access the resource."; } } responses: { key: "404"; value: { description: "Returned when the resource does not exist."; schema: { json_schema: { type: STRING; } } } } responses: { key: "418"; value: { description: "I'm a teapot."; schema: { json_schema: { ref: ".grpc.gateway.examples.internal.examplepb.NumericEnum"; } } } } extensions: { key: "x-grpc-gateway-foo"; value { string_value: "bar"; } } extensions: { key: "x-grpc-gateway-baz-list"; value { list_value: { values: { string_value: "one"; } values: { bool_value: true; } } } } }; // Intentionally complicated message type to cover many features of Protobuf. message ABitOfEverything { option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = { json_schema: { title: "A bit of everything" description: "Intentionaly complicated message type to cover many features of Protobuf." required: ["uuid", "int64_value", "double_value"] } external_docs: { url: "https://github.com/grpc-ecosystem/grpc-gateway"; description: "Find out more about ABitOfEverything"; } example: { value: '{ "uuid": "0cf361e1-4b44-483d-a159-54dabdf7e814" }' } }; // Nested is nested type. message Nested { option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = { example: { value: '{ "ok": "TRUE" }' } }; // name is nested field. string name = 1; uint32 amount = 2; // DeepEnum is one or zero. enum DeepEnum { // FALSE is false. FALSE = 0; // TRUE is true. TRUE = 1; } // DeepEnum comment. DeepEnum ok = 3 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {description: "DeepEnum description."}]; } Nested single_nested = 25; string uuid = 1 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {pattern: "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}", min_length: 1}]; repeated Nested nested = 2; float float_value = 3 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {description: "Float value field", default: "0.2", required: ['float_value']}]; double double_value = 4; int64 int64_value = 5; uint64 uint64_value = 6; int32 int32_value = 7; fixed64 fixed64_value = 8; fixed32 fixed32_value = 9; bool bool_value = 10; string string_value = 11; bytes bytes_value = 29; uint32 uint32_value = 13; NumericEnum enum_value = 14; pathenum.PathEnum path_enum_value = 30; pathenum.MessagePathEnum.NestedPathEnum nested_path_enum_value = 31; sfixed32 sfixed32_value = 15; sfixed64 sfixed64_value = 16; sint32 sint32_value = 17; sint64 sint64_value = 18; repeated string repeated_string_value = 19; oneof oneof_value { google.protobuf.Empty oneof_empty = 20; string oneof_string = 21; } map<string, NumericEnum> map_value = 22; map<string, string> mapped_string_value = 23; map<string, Nested> mapped_nested_value = 24; string nonConventionalNameValue = 26; google.protobuf.Timestamp timestamp_value = 27; // repeated enum value. it is comma-separated in query repeated NumericEnum repeated_enum_value = 28; // repeated numeric enum comment (This comment is overridden by the field annotation) repeated NumericEnum repeated_enum_annotation = 32 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {title: "Repeated numeric enum title", description: "Repeated numeric enum description."}]; // numeric enum comment (This comment is overridden by the field annotation) NumericEnum enum_value_annotation = 33 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {title: "Numeric enum title", description: "Numeric enum description."}]; // repeated string comment (This comment is overridden by the field annotation) repeated string repeated_string_annotation = 34 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {title: "Repeated string title", description: "Repeated string description."}]; // repeated nested object comment (This comment is overridden by the field annotation) repeated Nested repeated_nested_annotation = 35 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {title: "Repeated nested object title", description: "Repeated nested object description."}]; // nested object comments (This comment is overridden by the field annotation) Nested nested_annotation = 36 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {title: "Nested object title", description: "Nested object description."}]; int64 int64_override_type = 37 [(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {type: INTEGER}]; } // ABitOfEverythingRepeated is used to validate repeated path parameter functionality message ABitOfEverythingRepeated { option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = { example: { value: '{ "path_repeated_bool_value": [true, true, false, true], "path_repeated_int32_value": [1, 2, 3] }' } }; // repeated values. they are comma-separated in path repeated float path_repeated_float_value = 1; repeated double path_repeated_double_value = 2; repeated int64 path_repeated_int64_value = 3; repeated uint64 path_repeated_uint64_value = 4; repeated int32 path_repeated_int32_value = 5; repeated fixed64 path_repeated_fixed64_value = 6; repeated fixed32 path_repeated_fixed32_value = 7; repeated bool path_repeated_bool_value = 8; repeated string path_repeated_string_value = 9; repeated bytes path_repeated_bytes_value = 10; repeated uint32 path_repeated_uint32_value = 11; repeated NumericEnum path_repeated_enum_value = 12; repeated sfixed32 path_repeated_sfixed32_value = 13; repeated sfixed64 path_repeated_sfixed64_value = 14; repeated sint32 path_repeated_sint32_value = 15; repeated sint64 path_repeated_sint64_value = 16; } message Body { string name = 1; } message MessageWithBody { string id = 1; Body data = 2; } // NumericEnum is one or zero. enum NumericEnum { // ZERO means 0 ZERO = 0; // ONE means 1 ONE = 1; } // UpdateV2Request request for update includes the message and the update mask message UpdateV2Request { ABitOfEverything abe = 1; google.protobuf.FieldMask update_mask = 2; } // ABitOfEverything service is used to validate that APIs with complicated // proto messages and URL templates are still processed correctly. service ABitOfEverythingService { option (grpc.gateway.protoc_gen_swagger.options.openapiv2_tag) = { description: "ABitOfEverythingService description -- which should not be used in place of the documentation comment!" external_docs: { url: "https://github.com/grpc-ecosystem/grpc-gateway"; description: "Find out more about EchoService"; } }; // Create a new ABitOfEverything // // This API creates a new ABitOfEverything rpc Create(ABitOfEverything) returns (ABitOfEverything) { option (google.api.http) = { post: "/v1/example/a_bit_of_everything/{float_value}/{double_value}/{int64_value}/separator/{uint64_value}/{int32_value}/{fixed64_value}/{fixed32_value}/{bool_value}/{string_value=strprefix/*}/{uint32_value}/{sfixed32_value}/{sfixed64_value}/{sint32_value}/{sint64_value}/{nonConventionalNameValue}/{enum_value}/{path_enum_value}/{nested_path_enum_value}/{enum_value_annotation}" }; } rpc CreateBody(ABitOfEverything) returns (ABitOfEverything) { option (google.api.http) = { post: "/v1/example/a_bit_of_everything" body: "*" }; } rpc Lookup(sub2.IdMessage) returns (ABitOfEverything) { option (google.api.http) = { get: "/v1/example/a_bit_of_everything/{uuid}" }; } rpc Update(ABitOfEverything) returns (google.protobuf.Empty) { option (google.api.http) = { put: "/v1/example/a_bit_of_everything/{uuid}" body: "*" }; } rpc UpdateV2(UpdateV2Request) returns (google.protobuf.Empty) { option (google.api.http) = { put: "/v2/example/a_bit_of_everything/{abe.uuid}" body: "abe" additional_bindings: [ { patch: "/v2/example/a_bit_of_everything/{abe.uuid}" body: "abe" }, { patch: "/v2a/example/a_bit_of_everything/{abe.uuid}" body: "*" } ] }; } rpc Delete(sub2.IdMessage) returns (google.protobuf.Empty) { option (google.api.http) = { delete: "/v1/example/a_bit_of_everything/{uuid}" }; option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = { security: { security_requirement: { key: "ApiKeyAuth"; value: {} } security_requirement: { key: "OAuth2"; value: { scope: "read"; scope: "write"; } } } extensions: { key: "x-irreversible"; value { bool_value: true; } } }; } rpc GetQuery(ABitOfEverything) returns (google.protobuf.Empty) { option (google.api.http) = { get: "/v1/example/a_bit_of_everything/query/{uuid}" }; option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = { deprecated: true // For testing purposes. external_docs: { url: "https://github.com/grpc-ecosystem/grpc-gateway"; description: "Find out more about GetQuery"; } security: { } }; } rpc GetRepeatedQuery(ABitOfEverythingRepeated) returns (ABitOfEverythingRepeated) { option (google.api.http) = { get: "/v1/example/a_bit_of_everything_repeated/{path_repeated_float_value}/{path_repeated_double_value}/{path_repeated_int64_value}/{path_repeated_uint64_value}/{path_repeated_int32_value}/{path_repeated_fixed64_value}/{path_repeated_fixed32_value}/{path_repeated_bool_value}/{path_repeated_string_value}/{path_repeated_bytes_value}/{path_repeated_uint32_value}/{path_repeated_enum_value}/{path_repeated_sfixed32_value}/{path_repeated_sfixed64_value}/{path_repeated_sint32_value}/{path_repeated_sint64_value}" }; } // Echo allows posting a StringMessage value. // // It also exposes multiple bindings. // // This makes it useful when validating that the OpenAPI v2 API // description exposes documentation correctly on all paths // defined as additional_bindings in the proto. rpc Echo(grpc.gateway.examples.internal.sub.StringMessage) returns (grpc.gateway.examples.internal.sub.StringMessage) { option (google.api.http) = { get: "/v1/example/a_bit_of_everything/echo/{value}" additional_bindings { post: "/v2/example/echo" body: "value" } additional_bindings { get: "/v2/example/echo" } }; option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = { description: "Description Echo"; summary: "Summary: Echo rpc"; tags: "echo rpc"; external_docs: { url: "https://github.com/grpc-ecosystem/grpc-gateway"; description: "Find out more Echo"; } responses: { key: "200" value: { examples: { key: "application/json" value: '{"value": "the input value"}' } } } responses: { key: "503"; value: { description: "Returned when the resource is temporarily unavailable."; extensions: { key: "x-number"; value { number_value: 100; } } } } responses: { // Overwrites global definition. key: "404"; value: { description: "Returned when the resource does not exist."; schema: { json_schema: { type: INTEGER; } } } } }; } rpc DeepPathEcho(ABitOfEverything) returns (ABitOfEverything) { option (google.api.http) = { post: "/v1/example/a_bit_of_everything/{single_nested.name}" body: "*" }; } rpc NoBindings(google.protobuf.Duration) returns (google.protobuf.Empty) {} rpc Timeout(google.protobuf.Empty) returns (google.protobuf.Empty) { option (google.api.http) = { get: "/v2/example/timeout", }; } rpc ErrorWithDetails(google.protobuf.Empty) returns (google.protobuf.Empty) { option (google.api.http) = { get: "/v2/example/errorwithdetails", }; } rpc GetMessageWithBody(MessageWithBody) returns (google.protobuf.Empty) { option (google.api.http) = { post: "/v2/example/withbody/{id}", body: "data" }; } rpc PostWithEmptyBody(Body) returns (google.protobuf.Empty) { option (google.api.http) = { post: "/v2/example/postwithemptybody/{name}", body: "*" }; } rpc CheckGetQueryParams(ABitOfEverything) returns (ABitOfEverything) { option (google.api.http) = { get: "/v1/example/a_bit_of_everything/params/get/{single_nested.name}" }; } rpc CheckNestedEnumGetQueryParams(ABitOfEverything) returns (ABitOfEverything) { option (google.api.http) = { get: "/v1/example/a_bit_of_everything/params/get/nested_enum/{single_nested.ok}" }; } rpc CheckPostQueryParams(ABitOfEverything) returns (ABitOfEverything) { option (google.api.http) = { post: "/v1/example/a_bit_of_everything/params/post/{string_value}" body: "single_nested" }; } rpc OverwriteResponseContentType(google.protobuf.Empty) returns (google.protobuf.StringValue) { option (google.api.http) = { get: "/v2/example/overwriteresponsecontenttype" }; option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = { produces: "application/text" }; } rpc CheckExternalPathEnum(pathenum.MessageWithPathEnum) returns (google.protobuf.Empty) { option (google.api.http) = { get: "/v2/{value}:check" }; } rpc CheckExternalNestedPathEnum(pathenum.MessageWithNestedPathEnum) returns (google.protobuf.Empty) { option (google.api.http) = { get: "/v2/{value}:check" }; } } // camelCase and lowercase service names are valid but not recommended (use TitleCase instead) service camelCaseServiceName { rpc Empty(google.protobuf.Empty) returns (google.protobuf.Empty) { option (google.api.http) = { get: "/v2/example/empty", }; } } service AnotherServiceWithNoBindings { rpc NoBindings(google.protobuf.Empty) returns (google.protobuf.Empty) {} } |
More interesting articles on the topic:
https://grpc-ecosystem.github.io/grpc-gateway/docs/mapping/customizing_openapi_output/
https://github.com/grpc-ecosystem/grpc-gateway/blob/master/examples/internal/proto/examplepb/use_go_template.proto
Similar Posts
LEAVE A COMMENT
Для отправки комментария вам необходимо авторизоваться.