Skip to content

Commit

Permalink
protoc-gen-swagger: add default error response
Browse files Browse the repository at this point in the history
Adds a "default" error entry to all responses
in the swagger definitions.

Fixes #1122
  • Loading branch information
johanbrandhorst committed Feb 10, 2020
1 parent e1a127c commit 824f19a
Show file tree
Hide file tree
Showing 40 changed files with 1,796 additions and 161 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ SWAGGER_PLUGIN_FLAGS?=
GOOGLEAPIS_DIR=third_party/googleapis
OUTPUT_DIR=_output

RUNTIME_PROTO=internal/stream_chunk.proto
RUNTIME_PROTO=internal/errors.proto
RUNTIME_GO=$(RUNTIME_PROTO:.proto=.pb.go)

OPENAPIV2_PROTO=protoc-gen-swagger/options/openapiv2.proto protoc-gen-swagger/options/annotations.proto
Expand Down
2 changes: 2 additions & 0 deletions examples/clients/abe/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ go_library(
"model_message_path_enum_nested_path_enum.go",
"model_nested_deep_enum.go",
"model_pathenum_path_enum.go",
"model_protobuf_any.go",
"model_protobuf_field_mask.go",
"model_runtime_error.go",
"model_sub_string_message.go",
"response.go",
],
Expand Down
160 changes: 160 additions & 0 deletions examples/clients/abe/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v1/example/a_bit_of_everything/echo/{value}:
get:
tags:
Expand Down Expand Up @@ -97,6 +101,10 @@ paths:
description: "Returned when the resource is temporarily unavailable."
schema: {}
x-number: 100
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
externalDocs:
description: "Find out more Echo"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
Expand Down Expand Up @@ -384,6 +392,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v1/example/a_bit_of_everything/params/get/{single_nested.name}:
get:
tags:
Expand Down Expand Up @@ -675,6 +687,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v1/example/a_bit_of_everything/params/post/{string_value}:
post:
tags:
Expand Down Expand Up @@ -710,6 +726,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v1/example/a_bit_of_everything/query/{uuid}:
get:
tags:
Expand Down Expand Up @@ -1007,6 +1027,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
security: []
externalDocs:
description: "Find out more about GetQuery"
Expand Down Expand Up @@ -1161,6 +1185,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v1/example/a_bit_of_everything/{single_nested.name}:
post:
tags:
Expand Down Expand Up @@ -1197,6 +1225,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v1/example/a_bit_of_everything/{uuid}:
get:
tags:
Expand Down Expand Up @@ -1226,6 +1258,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
put:
tags:
- "ABitOfEverythingService"
Expand Down Expand Up @@ -1259,6 +1295,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
delete:
tags:
- "ABitOfEverythingService"
Expand Down Expand Up @@ -1286,6 +1326,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
security:
- ApiKeyAuth: []
OAuth2:
Expand Down Expand Up @@ -1479,6 +1523,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v2/example/a_bit_of_everything/{abe.uuid}:
put:
tags:
Expand Down Expand Up @@ -1513,6 +1561,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
patch:
tags:
- "ABitOfEverythingService"
Expand Down Expand Up @@ -1546,6 +1598,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v2/example/echo:
get:
tags:
Expand Down Expand Up @@ -1585,6 +1641,10 @@ paths:
description: "Returned when the resource is temporarily unavailable."
schema: {}
x-number: 100
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
externalDocs:
description: "Find out more Echo"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
Expand Down Expand Up @@ -1626,6 +1686,10 @@ paths:
description: "Returned when the resource is temporarily unavailable."
schema: {}
x-number: 100
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
externalDocs:
description: "Find out more Echo"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
Expand All @@ -1652,6 +1716,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v2/example/errorwithdetails:
get:
tags:
Expand All @@ -1675,6 +1743,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v2/example/postwithemptybody/{name}:
post:
tags:
Expand Down Expand Up @@ -1709,6 +1781,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v2/example/timeout:
get:
tags:
Expand All @@ -1732,6 +1808,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v2/example/withbody/{id}:
post:
tags:
Expand Down Expand Up @@ -1766,6 +1846,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
/v2a/example/a_bit_of_everything/{abe.uuid}:
patch:
tags:
Expand Down Expand Up @@ -1800,6 +1884,10 @@ paths:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response"
schema:
$ref: "#/definitions/runtimeError"
securityDefinitions:
ApiKeyAuth:
type: "apiKey"
Expand Down Expand Up @@ -2110,6 +2198,64 @@ definitions:
- "ABC"
- "DEF"
default: "ABC"
protobufAny:
type: "object"
properties:
type_url:
type: "string"
description: "A URL/resource name that uniquely identifies the type of the\
\ serialized\nprotocol buffer message. This string must contain at least\n\
one \"/\" character. The last segment of the URL's path must represent\n\
the fully qualified name of the type (as in\n`path/google.protobuf.Duration`).\
\ The name should be in a canonical form\n(e.g., leading \".\" is not accepted).\n\
\nIn practice, teams usually precompile into the binary all types that they\n\
expect it to use in the context of Any. However, for URLs which use the\n\
scheme `http`, `https`, or no scheme, one can optionally set up a type\n\
server that maps type URLs to message definitions as follows:\n\n* If no\
\ scheme is provided, `https` is assumed.\n* An HTTP GET on the URL must\
\ yield a [google.protobuf.Type][]\n value in binary format, or produce\
\ an error.\n* Applications are allowed to cache lookup results based on\
\ the\n URL, or have them precompiled into a binary to avoid any\n lookup.\
\ Therefore, binary compatibility needs to be preserved\n on changes to\
\ types. (Use versioned type names to manage\n breaking changes.)\n\nNote:\
\ this functionality is not currently available in the official\nprotobuf\
\ release, and it is not used for type URLs beginning with\ntype.googleapis.com.\n\
\nSchemes other than `http`, `https` (or the empty scheme) might be\nused\
\ with implementation specific semantics."
value:
type: "string"
format: "byte"
description: "Must be a valid serialized protocol buffer of the above specified\
\ type."
pattern: "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
description: "`Any` contains an arbitrary serialized protocol buffer message along\
\ with a\nURL that describes the type of the serialized message.\n\nProtobuf\
\ library provides support to pack/unpack Any values in the form\nof utility\
\ functions or additional generated methods of the Any type.\n\nExample 1: Pack\
\ and unpack a message in C++.\n\n Foo foo = ...;\n Any any;\n any.PackFrom(foo);\n\
\ ...\n if (any.UnpackTo(&foo)) {\n ...\n }\n\nExample 2: Pack\
\ and unpack a message in Java.\n\n Foo foo = ...;\n Any any = Any.pack(foo);\n\
\ ...\n if (any.is(Foo.class)) {\n foo = any.unpack(Foo.class);\n\
\ }\n\n Example 3: Pack and unpack a message in Python.\n\n foo = Foo(...)\n\
\ any = Any()\n any.Pack(foo)\n ...\n if any.Is(Foo.DESCRIPTOR):\n\
\ any.Unpack(foo)\n ...\n\n Example 4: Pack and unpack a message in\
\ Go\n\n foo := &pb.Foo{...}\n any, err := ptypes.MarshalAny(foo)\n\
\ ...\n foo := &pb.Foo{}\n if err := ptypes.UnmarshalAny(any, foo);\
\ err != nil {\n ...\n }\n\nThe pack methods provided by protobuf\
\ library will by default use\n'type.googleapis.com/full.type.name' as the type\
\ URL and the unpack\nmethods only use the fully qualified type name after the\
\ last '/'\nin the type URL, for example \"foo.bar.com/x/y.z\" will yield type\n\
name \"y.z\".\n\n\nJSON\n====\nThe JSON representation of an `Any` value uses\
\ the regular\nrepresentation of the deserialized, embedded message, with an\n\
additional field `@type` which contains the type URL. Example:\n\n package\
\ google.profile;\n message Person {\n string first_name = 1;\n \
\ string last_name = 2;\n }\n\n {\n \"@type\": \"type.googleapis.com/google.profile.Person\"\
,\n \"firstName\": <string>,\n \"lastName\": <string>\n }\n\nIf\
\ the embedded message type is well-known and has a custom JSON\nrepresentation,\
\ that representation will be embedded adding a field\n`value` which holds the\
\ custom JSON in addition to the `@type`\nfield. Example (for message [google.protobuf.Duration][]):\n\
\n {\n \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n\
\ \"value\": \"1.212s\"\n }"
protobufFieldMask:
type: "object"
properties:
Expand Down Expand Up @@ -2193,6 +2339,20 @@ definitions:
\ API method which has a FieldMask type field in the\nrequest should verify\
\ the included field paths, and return an\n`INVALID_ARGUMENT` error if any path\
\ is duplicated or unmappable."
runtimeError:
type: "object"
properties:
error:
type: "string"
code:
type: "integer"
format: "int32"
message:
type: "string"
details:
type: "array"
items:
$ref: "#/definitions/protobufAny"
subStringMessage:
type: "object"
properties:
Expand Down
Loading

0 comments on commit 824f19a

Please sign in to comment.