From 824f19a3fde7712157300fdd107b8d3c871576f8 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst Date: Mon, 10 Feb 2020 12:38:22 +0000 Subject: [PATCH] protoc-gen-swagger: add default error response Adds a "default" error entry to all responses in the swagger definitions. Fixes #1122 --- Makefile | 2 +- examples/clients/abe/BUILD.bazel | 2 + examples/clients/abe/api/swagger.yaml | 160 ++++++++++++++ .../abe/api_a_bit_of_everything_service.go | 198 ++++++++++++++++++ .../abe/api_camel_case_service_name.go | 11 + examples/clients/abe/api_echo_rpc.go | 33 +++ examples/clients/abe/model_protobuf_any.go | 19 ++ examples/clients/abe/model_runtime_error.go | 18 ++ examples/clients/echo/BUILD.bazel | 2 + examples/clients/echo/api/swagger.yaml | 100 +++++++++ examples/clients/echo/api_echo_service.go | 77 +++++++ examples/clients/echo/model_protobuf_any.go | 18 ++ examples/clients/echo/model_runtime_error.go | 17 ++ examples/clients/responsebody/BUILD.bazel | 2 + .../clients/responsebody/api/swagger.yaml | 84 ++++++++ .../responsebody/api_response_body_service.go | 33 +++ .../clients/responsebody/docs/ProtobufAny.md | 11 + .../clients/responsebody/docs/RuntimeError.md | 13 ++ .../responsebody/model_protobuf_any.go | 18 ++ .../responsebody/model_runtime_error.go | 17 ++ examples/clients/unannotatedecho/BUILD.bazel | 2 + .../clients/unannotatedecho/api/swagger.yaml | 88 ++++++++ .../api_unannotated_echo_service.go | 44 ++++ .../unannotatedecho/model_protobuf_any.go | 18 ++ .../unannotatedecho/model_runtime_error.go | 17 ++ .../a_bit_of_everything.swagger.json | 168 +++++++++++++++ .../proto/examplepb/echo_service.swagger.json | 78 +++++++ .../response_body_service.swagger.json | 54 +++++ examples/proto/examplepb/stream.swagger.json | 39 ++++ .../unannotated_echo_service.swagger.json | 60 ++++++ .../examplepb/use_go_template.swagger.json | 48 +++++ .../proto/examplepb/wrappers.swagger.json | 102 +++++++++ internal/BUILD.bazel | 2 +- internal/errors.pb.go | 189 +++++++++++++++++ internal/errors.proto | 26 +++ internal/stream_chunk.pb.go | 119 ----------- internal/stream_chunk.proto | 15 -- protoc-gen-swagger/genswagger/template.go | 31 ++- .../genswagger/template_test.go | 2 +- runtime/errors.go | 20 +- 40 files changed, 1796 insertions(+), 161 deletions(-) create mode 100644 examples/clients/abe/model_protobuf_any.go create mode 100644 examples/clients/abe/model_runtime_error.go create mode 100644 examples/clients/echo/model_protobuf_any.go create mode 100644 examples/clients/echo/model_runtime_error.go create mode 100644 examples/clients/responsebody/docs/ProtobufAny.md create mode 100644 examples/clients/responsebody/docs/RuntimeError.md create mode 100644 examples/clients/responsebody/model_protobuf_any.go create mode 100644 examples/clients/responsebody/model_runtime_error.go create mode 100644 examples/clients/unannotatedecho/model_protobuf_any.go create mode 100644 examples/clients/unannotatedecho/model_runtime_error.go create mode 100644 internal/errors.pb.go create mode 100644 internal/errors.proto delete mode 100644 internal/stream_chunk.pb.go delete mode 100644 internal/stream_chunk.proto diff --git a/Makefile b/Makefile index eb31aa6d714..bd641d17240 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/examples/clients/abe/BUILD.bazel b/examples/clients/abe/BUILD.bazel index d64c856782d..a54d462444e 100644 --- a/examples/clients/abe/BUILD.bazel +++ b/examples/clients/abe/BUILD.bazel @@ -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", ], diff --git a/examples/clients/abe/api/swagger.yaml b/examples/clients/abe/api/swagger.yaml index 91859be58e5..8c234914da2 100644 --- a/examples/clients/abe/api/swagger.yaml +++ b/examples/clients/abe/api/swagger.yaml @@ -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: @@ -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" @@ -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: @@ -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: @@ -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: @@ -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" @@ -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: @@ -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: @@ -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" @@ -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" @@ -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: @@ -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: @@ -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" @@ -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: @@ -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" @@ -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" @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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" @@ -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\": ,\n \"lastName\": \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: @@ -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: diff --git a/examples/clients/abe/api_a_bit_of_everything_service.go b/examples/clients/abe/api_a_bit_of_everything_service.go index 2bd1d972c20..f5d1759381e 100644 --- a/examples/clients/abe/api_a_bit_of_everything_service.go +++ b/examples/clients/abe/api_a_bit_of_everything_service.go @@ -317,6 +317,17 @@ func (a *ABitOfEverythingServiceApiService) CheckGetQueryParams(ctx context.Cont return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -613,6 +624,17 @@ func (a *ABitOfEverythingServiceApiService) CheckNestedEnumGetQueryParams(ctx co return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -750,6 +772,17 @@ func (a *ABitOfEverythingServiceApiService) CheckPostQueryParams(ctx context.Con return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -921,6 +954,17 @@ func (a *ABitOfEverythingServiceApiService) Create(ctx context.Context, floatVal return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -1056,6 +1100,17 @@ func (a *ABitOfEverythingServiceApiService) CreateBody(ctx context.Context, body return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -1193,6 +1248,17 @@ func (a *ABitOfEverythingServiceApiService) DeepPathEcho(ctx context.Context, si return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -1327,6 +1393,17 @@ func (a *ABitOfEverythingServiceApiService) Delete(ctx context.Context, uuid str return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -1459,6 +1536,17 @@ func (a *ABitOfEverythingServiceApiService) ErrorWithDetails(ctx context.Context return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -1596,6 +1684,17 @@ func (a *ABitOfEverythingServiceApiService) GetMessageWithBody(ctx context.Conte return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -1884,6 +1983,17 @@ func (a *ABitOfEverythingServiceApiService) GetQuery(ctx context.Context, uuid s return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -2096,6 +2206,17 @@ func (a *ABitOfEverythingServiceApiService) GetRepeatedQuery(ctx context.Context return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -2230,6 +2351,17 @@ func (a *ABitOfEverythingServiceApiService) Lookup(ctx context.Context, uuid str return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -2367,6 +2499,17 @@ func (a *ABitOfEverythingServiceApiService) PostWithEmptyBody(ctx context.Contex return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -2499,6 +2642,17 @@ func (a *ABitOfEverythingServiceApiService) Timeout(ctx context.Context) (interf return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -2636,6 +2790,17 @@ func (a *ABitOfEverythingServiceApiService) Update(ctx context.Context, uuid str return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -2773,6 +2938,17 @@ func (a *ABitOfEverythingServiceApiService) UpdateV2(ctx context.Context, abeUui return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -2910,6 +3086,17 @@ func (a *ABitOfEverythingServiceApiService) UpdateV22(ctx context.Context, abeUu return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -3047,6 +3234,17 @@ func (a *ABitOfEverythingServiceApiService) UpdateV23(ctx context.Context, abeUu return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } diff --git a/examples/clients/abe/api_camel_case_service_name.go b/examples/clients/abe/api_camel_case_service_name.go index 80d867e660c..8a12b878ef7 100644 --- a/examples/clients/abe/api_camel_case_service_name.go +++ b/examples/clients/abe/api_camel_case_service_name.go @@ -151,6 +151,17 @@ func (a *CamelCaseServiceNameApiService) Empty(ctx context.Context) (interface{} return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } diff --git a/examples/clients/abe/api_echo_rpc.go b/examples/clients/abe/api_echo_rpc.go index 1dab0dc55f0..e8979f37579 100644 --- a/examples/clients/abe/api_echo_rpc.go +++ b/examples/clients/abe/api_echo_rpc.go @@ -167,6 +167,17 @@ func (a *EchoRpcApiService) Echo(ctx context.Context, value string) (SubStringMe return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -314,6 +325,17 @@ func (a *EchoRpcApiService) Echo2(ctx context.Context, body string) (SubStringMe return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -468,6 +490,17 @@ func (a *EchoRpcApiService) Echo3(ctx context.Context, localVarOptionals *Echo3O return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } diff --git a/examples/clients/abe/model_protobuf_any.go b/examples/clients/abe/model_protobuf_any.go new file mode 100644 index 00000000000..f3e536a4ab5 --- /dev/null +++ b/examples/clients/abe/model_protobuf_any.go @@ -0,0 +1,19 @@ +/* + * A Bit of Everything + * + * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) + * + * API version: 1.0 + * Contact: none@example.com + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package abe + +// `Any` contains an arbitrary serialized protocol buffer message along with a URL that describes the type of the serialized message. Protobuf library provides support to pack/unpack Any values in the form of utility functions or additional generated methods of the Any type. Example 1: Pack and unpack a message in C++. Foo foo = ...; Any any; any.PackFrom(foo); ... if (any.UnpackTo(&foo)) { ... } Example 2: Pack and unpack a message in Java. Foo foo = ...; Any any = Any.pack(foo); ... if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } Example 3: Pack and unpack a message in Python. foo = Foo(...) any = Any() any.Pack(foo) ... if any.Is(Foo.DESCRIPTOR): any.Unpack(foo) ... Example 4: Pack and unpack a message in Go foo := &pb.Foo{...} any, err := ptypes.MarshalAny(foo) ... foo := &pb.Foo{} if err := ptypes.UnmarshalAny(any, foo); err != nil { ... } The pack methods provided by protobuf library will by default use 'type.googleapis.com/full.type.name' as the type URL and the unpack methods only use the fully qualified type name after the last '/' in the type URL, for example \"foo.bar.com/x/y.z\" will yield type name \"y.z\". JSON ==== The JSON representation of an `Any` value uses the regular representation of the deserialized, embedded message, with an additional field `@type` which contains the type URL. Example: package google.profile; message Person { string first_name = 1; string last_name = 2; } { \"@type\": \"type.googleapis.com/google.profile.Person\", \"firstName\": , \"lastName\": } If the embedded message type is well-known and has a custom JSON representation, that representation will be embedded adding a field `value` which holds the custom JSON in addition to the `@type` field. Example (for message [google.protobuf.Duration][]): { \"@type\": \"type.googleapis.com/google.protobuf.Duration\", \"value\": \"1.212s\" } +type ProtobufAny struct { + // A URL/resource name that uniquely identifies the type of the serialized protocol buffer message. This string must contain at least one \"/\" character. The last segment of the URL's path must represent the fully qualified name of the type (as in `path/google.protobuf.Duration`). The name should be in a canonical form (e.g., leading \".\" is not accepted). In practice, teams usually precompile into the binary all types that they expect it to use in the context of Any. However, for URLs which use the scheme `http`, `https`, or no scheme, one can optionally set up a type server that maps type URLs to message definitions as follows: * If no scheme is provided, `https` is assumed. * An HTTP GET on the URL must yield a [google.protobuf.Type][] value in binary format, or produce an error. * Applications are allowed to cache lookup results based on the URL, or have them precompiled into a binary to avoid any lookup. Therefore, binary compatibility needs to be preserved on changes to types. (Use versioned type names to manage breaking changes.) Note: this functionality is not currently available in the official protobuf release, and it is not used for type URLs beginning with type.googleapis.com. Schemes other than `http`, `https` (or the empty scheme) might be used with implementation specific semantics. + TypeUrl string `json:"type_url,omitempty"` + // Must be a valid serialized protocol buffer of the above specified type. + Value string `json:"value,omitempty"` +} diff --git a/examples/clients/abe/model_runtime_error.go b/examples/clients/abe/model_runtime_error.go new file mode 100644 index 00000000000..9674e976dc7 --- /dev/null +++ b/examples/clients/abe/model_runtime_error.go @@ -0,0 +1,18 @@ +/* + * A Bit of Everything + * + * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) + * + * API version: 1.0 + * Contact: none@example.com + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package abe + +type RuntimeError struct { + Error_ string `json:"error,omitempty"` + Code int32 `json:"code,omitempty"` + Message string `json:"message,omitempty"` + Details []ProtobufAny `json:"details,omitempty"` +} diff --git a/examples/clients/echo/BUILD.bazel b/examples/clients/echo/BUILD.bazel index 31ac6d56986..5700dd477c1 100644 --- a/examples/clients/echo/BUILD.bazel +++ b/examples/clients/echo/BUILD.bazel @@ -10,6 +10,8 @@ go_library( "configuration.go", "model_examplepb_embedded.go", "model_examplepb_simple_message.go", + "model_protobuf_any.go", + "model_runtime_error.go", "response.go", ], importpath = "github.com/grpc-ecosystem/grpc-gateway/examples/clients/echo", diff --git a/examples/clients/echo/api/swagger.yaml b/examples/clients/echo/api/swagger.yaml index 9d28a1ab99b..7df1b8731c5 100644 --- a/examples/clients/echo/api/swagger.yaml +++ b/examples/clients/echo/api/swagger.yaml @@ -28,6 +28,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /v1/example/echo/{id}/{num}: get: tags: @@ -99,6 +103,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /v1/example/echo/{id}/{num}/{lang}: get: tags: @@ -169,6 +177,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /v1/example/echo1/{id}/{line_num}/{status.note}: get: tags: @@ -233,6 +245,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /v1/example/echo2/{no.note}: get: tags: @@ -299,6 +315,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /v1/example/echo_body: post: tags: @@ -317,6 +337,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /v1/example/echo_delete: delete: tags: @@ -389,6 +413,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" definitions: examplepbEmbedded: type: "object" @@ -436,3 +464,75 @@ definitions: status: note: "note" progress: "progress" + 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\": ,\n \"lastName\": \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 }" + runtimeError: + type: "object" + properties: + error: + type: "string" + code: + type: "integer" + format: "int32" + message: + type: "string" + details: + type: "array" + items: + $ref: "#/definitions/protobufAny" diff --git a/examples/clients/echo/api_echo_service.go b/examples/clients/echo/api_echo_service.go index 5e5b44b450d..40524345aa6 100644 --- a/examples/clients/echo/api_echo_service.go +++ b/examples/clients/echo/api_echo_service.go @@ -109,6 +109,17 @@ func (a *EchoServiceApiService) Echo(ctx context.Context, id string) (ExamplepbS return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -240,6 +251,17 @@ func (a *EchoServiceApiService) Echo2(ctx context.Context, id string, num string return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -368,6 +390,17 @@ func (a *EchoServiceApiService) Echo3(ctx context.Context, id string, num string return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -491,6 +524,17 @@ func (a *EchoServiceApiService) Echo4(ctx context.Context, id string, lineNum st return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -620,6 +664,17 @@ func (a *EchoServiceApiService) Echo5(ctx context.Context, noNote string, localV return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -709,6 +764,17 @@ func (a *EchoServiceApiService) EchoBody(ctx context.Context, body ExamplepbSimp return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -845,6 +911,17 @@ func (a *EchoServiceApiService) EchoDelete(ctx context.Context, localVarOptional return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } diff --git a/examples/clients/echo/model_protobuf_any.go b/examples/clients/echo/model_protobuf_any.go new file mode 100644 index 00000000000..198cda747f2 --- /dev/null +++ b/examples/clients/echo/model_protobuf_any.go @@ -0,0 +1,18 @@ +/* + * Echo Service + * + * Echo Service API consists of a single service which returns a message. + * + * API version: version not set + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package echo + +// `Any` contains an arbitrary serialized protocol buffer message along with a URL that describes the type of the serialized message. Protobuf library provides support to pack/unpack Any values in the form of utility functions or additional generated methods of the Any type. Example 1: Pack and unpack a message in C++. Foo foo = ...; Any any; any.PackFrom(foo); ... if (any.UnpackTo(&foo)) { ... } Example 2: Pack and unpack a message in Java. Foo foo = ...; Any any = Any.pack(foo); ... if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } Example 3: Pack and unpack a message in Python. foo = Foo(...) any = Any() any.Pack(foo) ... if any.Is(Foo.DESCRIPTOR): any.Unpack(foo) ... Example 4: Pack and unpack a message in Go foo := &pb.Foo{...} any, err := ptypes.MarshalAny(foo) ... foo := &pb.Foo{} if err := ptypes.UnmarshalAny(any, foo); err != nil { ... } The pack methods provided by protobuf library will by default use 'type.googleapis.com/full.type.name' as the type URL and the unpack methods only use the fully qualified type name after the last '/' in the type URL, for example \"foo.bar.com/x/y.z\" will yield type name \"y.z\". JSON ==== The JSON representation of an `Any` value uses the regular representation of the deserialized, embedded message, with an additional field `@type` which contains the type URL. Example: package google.profile; message Person { string first_name = 1; string last_name = 2; } { \"@type\": \"type.googleapis.com/google.profile.Person\", \"firstName\": , \"lastName\": } If the embedded message type is well-known and has a custom JSON representation, that representation will be embedded adding a field `value` which holds the custom JSON in addition to the `@type` field. Example (for message [google.protobuf.Duration][]): { \"@type\": \"type.googleapis.com/google.protobuf.Duration\", \"value\": \"1.212s\" } +type ProtobufAny struct { + // A URL/resource name that uniquely identifies the type of the serialized protocol buffer message. This string must contain at least one \"/\" character. The last segment of the URL's path must represent the fully qualified name of the type (as in `path/google.protobuf.Duration`). The name should be in a canonical form (e.g., leading \".\" is not accepted). In practice, teams usually precompile into the binary all types that they expect it to use in the context of Any. However, for URLs which use the scheme `http`, `https`, or no scheme, one can optionally set up a type server that maps type URLs to message definitions as follows: * If no scheme is provided, `https` is assumed. * An HTTP GET on the URL must yield a [google.protobuf.Type][] value in binary format, or produce an error. * Applications are allowed to cache lookup results based on the URL, or have them precompiled into a binary to avoid any lookup. Therefore, binary compatibility needs to be preserved on changes to types. (Use versioned type names to manage breaking changes.) Note: this functionality is not currently available in the official protobuf release, and it is not used for type URLs beginning with type.googleapis.com. Schemes other than `http`, `https` (or the empty scheme) might be used with implementation specific semantics. + TypeUrl string `json:"type_url,omitempty"` + // Must be a valid serialized protocol buffer of the above specified type. + Value string `json:"value,omitempty"` +} diff --git a/examples/clients/echo/model_runtime_error.go b/examples/clients/echo/model_runtime_error.go new file mode 100644 index 00000000000..836afd7ac6b --- /dev/null +++ b/examples/clients/echo/model_runtime_error.go @@ -0,0 +1,17 @@ +/* + * Echo Service + * + * Echo Service API consists of a single service which returns a message. + * + * API version: version not set + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package echo + +type RuntimeError struct { + Error_ string `json:"error,omitempty"` + Code int32 `json:"code,omitempty"` + Message string `json:"message,omitempty"` + Details []ProtobufAny `json:"details,omitempty"` +} diff --git a/examples/clients/responsebody/BUILD.bazel b/examples/clients/responsebody/BUILD.bazel index f5fb6a134ad..cf94bfa564a 100644 --- a/examples/clients/responsebody/BUILD.bazel +++ b/examples/clients/responsebody/BUILD.bazel @@ -11,7 +11,9 @@ go_library( "model_examplepb_repeated_response_strings.go", "model_examplepb_response_body_out.go", "model_examplepb_response_body_out_response.go", + "model_protobuf_any.go", "model_response_response_type.go", + "model_runtime_error.go", "response.go", ], importpath = "github.com/grpc-ecosystem/grpc-gateway/examples/clients/responsebody", diff --git a/examples/clients/responsebody/api/swagger.yaml b/examples/clients/responsebody/api/swagger.yaml index b94a7273d45..30d7604c7ff 100644 --- a/examples/clients/responsebody/api/swagger.yaml +++ b/examples/clients/responsebody/api/swagger.yaml @@ -26,6 +26,10 @@ paths: type: "array" items: $ref: "#/definitions/examplepbRepeatedResponseBodyOutResponse" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /responsebody/{data}: get: tags: @@ -42,6 +46,10 @@ paths: description: "" schema: $ref: "#/definitions/examplepbResponseBodyOutResponse" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /responsestrings/{data}: get: tags: @@ -60,6 +68,10 @@ paths: type: "array" items: type: "string" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" definitions: ResponseResponseType: type: "string" @@ -105,3 +117,75 @@ definitions: type: "string" example: data: "data" + 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\": ,\n \"lastName\": \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 }" + runtimeError: + type: "object" + properties: + error: + type: "string" + code: + type: "integer" + format: "int32" + message: + type: "string" + details: + type: "array" + items: + $ref: "#/definitions/protobufAny" diff --git a/examples/clients/responsebody/api_response_body_service.go b/examples/clients/responsebody/api_response_body_service.go index 4d534a3867e..575d8b40eb4 100644 --- a/examples/clients/responsebody/api_response_body_service.go +++ b/examples/clients/responsebody/api_response_body_service.go @@ -107,6 +107,17 @@ func (a *ResponseBodyServiceApiService) GetResponseBody(ctx context.Context, dat return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -195,6 +206,17 @@ func (a *ResponseBodyServiceApiService) ListResponseBodies(ctx context.Context, return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -283,6 +305,17 @@ func (a *ResponseBodyServiceApiService) ListResponseStrings(ctx context.Context, return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } diff --git a/examples/clients/responsebody/docs/ProtobufAny.md b/examples/clients/responsebody/docs/ProtobufAny.md new file mode 100644 index 00000000000..50aaf9e772b --- /dev/null +++ b/examples/clients/responsebody/docs/ProtobufAny.md @@ -0,0 +1,11 @@ +# ProtobufAny + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**TypeUrl** | **string** | A URL/resource name that uniquely identifies the type of the serialized protocol buffer message. This string must contain at least one \"/\" character. The last segment of the URL's path must represent the fully qualified name of the type (as in `path/google.protobuf.Duration`). The name should be in a canonical form (e.g., leading \".\" is not accepted). In practice, teams usually precompile into the binary all types that they expect it to use in the context of Any. However, for URLs which use the scheme `http`, `https`, or no scheme, one can optionally set up a type server that maps type URLs to message definitions as follows: * If no scheme is provided, `https` is assumed. * An HTTP GET on the URL must yield a [google.protobuf.Type][] value in binary format, or produce an error. * Applications are allowed to cache lookup results based on the URL, or have them precompiled into a binary to avoid any lookup. Therefore, binary compatibility needs to be preserved on changes to types. (Use versioned type names to manage breaking changes.) Note: this functionality is not currently available in the official protobuf release, and it is not used for type URLs beginning with type.googleapis.com. Schemes other than `http`, `https` (or the empty scheme) might be used with implementation specific semantics. | [optional] [default to null] +**Value** | **string** | Must be a valid serialized protocol buffer of the above specified type. | [optional] [default to null] + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/examples/clients/responsebody/docs/RuntimeError.md b/examples/clients/responsebody/docs/RuntimeError.md new file mode 100644 index 00000000000..664dd90b514 --- /dev/null +++ b/examples/clients/responsebody/docs/RuntimeError.md @@ -0,0 +1,13 @@ +# RuntimeError + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**Error_** | **string** | | [optional] [default to null] +**Code** | **int32** | | [optional] [default to null] +**Message** | **string** | | [optional] [default to null] +**Details** | [**[]ProtobufAny**](protobufAny.md) | | [optional] [default to null] + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/examples/clients/responsebody/model_protobuf_any.go b/examples/clients/responsebody/model_protobuf_any.go new file mode 100644 index 00000000000..902926206f8 --- /dev/null +++ b/examples/clients/responsebody/model_protobuf_any.go @@ -0,0 +1,18 @@ +/* + * examples/proto/examplepb/response_body_service.proto + * + * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) + * + * API version: version not set + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package responsebody + +// `Any` contains an arbitrary serialized protocol buffer message along with a URL that describes the type of the serialized message. Protobuf library provides support to pack/unpack Any values in the form of utility functions or additional generated methods of the Any type. Example 1: Pack and unpack a message in C++. Foo foo = ...; Any any; any.PackFrom(foo); ... if (any.UnpackTo(&foo)) { ... } Example 2: Pack and unpack a message in Java. Foo foo = ...; Any any = Any.pack(foo); ... if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } Example 3: Pack and unpack a message in Python. foo = Foo(...) any = Any() any.Pack(foo) ... if any.Is(Foo.DESCRIPTOR): any.Unpack(foo) ... Example 4: Pack and unpack a message in Go foo := &pb.Foo{...} any, err := ptypes.MarshalAny(foo) ... foo := &pb.Foo{} if err := ptypes.UnmarshalAny(any, foo); err != nil { ... } The pack methods provided by protobuf library will by default use 'type.googleapis.com/full.type.name' as the type URL and the unpack methods only use the fully qualified type name after the last '/' in the type URL, for example \"foo.bar.com/x/y.z\" will yield type name \"y.z\". JSON ==== The JSON representation of an `Any` value uses the regular representation of the deserialized, embedded message, with an additional field `@type` which contains the type URL. Example: package google.profile; message Person { string first_name = 1; string last_name = 2; } { \"@type\": \"type.googleapis.com/google.profile.Person\", \"firstName\": , \"lastName\": } If the embedded message type is well-known and has a custom JSON representation, that representation will be embedded adding a field `value` which holds the custom JSON in addition to the `@type` field. Example (for message [google.protobuf.Duration][]): { \"@type\": \"type.googleapis.com/google.protobuf.Duration\", \"value\": \"1.212s\" } +type ProtobufAny struct { + // A URL/resource name that uniquely identifies the type of the serialized protocol buffer message. This string must contain at least one \"/\" character. The last segment of the URL's path must represent the fully qualified name of the type (as in `path/google.protobuf.Duration`). The name should be in a canonical form (e.g., leading \".\" is not accepted). In practice, teams usually precompile into the binary all types that they expect it to use in the context of Any. However, for URLs which use the scheme `http`, `https`, or no scheme, one can optionally set up a type server that maps type URLs to message definitions as follows: * If no scheme is provided, `https` is assumed. * An HTTP GET on the URL must yield a [google.protobuf.Type][] value in binary format, or produce an error. * Applications are allowed to cache lookup results based on the URL, or have them precompiled into a binary to avoid any lookup. Therefore, binary compatibility needs to be preserved on changes to types. (Use versioned type names to manage breaking changes.) Note: this functionality is not currently available in the official protobuf release, and it is not used for type URLs beginning with type.googleapis.com. Schemes other than `http`, `https` (or the empty scheme) might be used with implementation specific semantics. + TypeUrl string `json:"type_url,omitempty"` + // Must be a valid serialized protocol buffer of the above specified type. + Value string `json:"value,omitempty"` +} diff --git a/examples/clients/responsebody/model_runtime_error.go b/examples/clients/responsebody/model_runtime_error.go new file mode 100644 index 00000000000..a7f7a1d0569 --- /dev/null +++ b/examples/clients/responsebody/model_runtime_error.go @@ -0,0 +1,17 @@ +/* + * examples/proto/examplepb/response_body_service.proto + * + * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) + * + * API version: version not set + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package responsebody + +type RuntimeError struct { + Error_ string `json:"error,omitempty"` + Code int32 `json:"code,omitempty"` + Message string `json:"message,omitempty"` + Details []ProtobufAny `json:"details,omitempty"` +} diff --git a/examples/clients/unannotatedecho/BUILD.bazel b/examples/clients/unannotatedecho/BUILD.bazel index 06348263dc7..f34a3fac4e0 100644 --- a/examples/clients/unannotatedecho/BUILD.bazel +++ b/examples/clients/unannotatedecho/BUILD.bazel @@ -9,6 +9,8 @@ go_library( "client.go", "configuration.go", "model_examplepb_unannotated_simple_message.go", + "model_protobuf_any.go", + "model_runtime_error.go", "response.go", ], importpath = "github.com/grpc-ecosystem/grpc-gateway/examples/clients/unannotatedecho", diff --git a/examples/clients/unannotatedecho/api/swagger.yaml b/examples/clients/unannotatedecho/api/swagger.yaml index 3a439d45a10..da2fb50e598 100644 --- a/examples/clients/unannotatedecho/api/swagger.yaml +++ b/examples/clients/unannotatedecho/api/swagger.yaml @@ -31,6 +31,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbUnannotatedSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /v1/example/echo/{id}/{num}: get: tags: @@ -62,6 +66,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbUnannotatedSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /v1/example/echo_body: post: tags: @@ -80,6 +88,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbUnannotatedSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" /v1/example/echo_delete: delete: tags: @@ -112,6 +124,10 @@ paths: description: "A successful response." schema: $ref: "#/definitions/examplepbUnannotatedSimpleMessage" + default: + description: "An unexpected error response" + schema: + $ref: "#/definitions/runtimeError" definitions: examplepbUnannotatedSimpleMessage: type: "object" @@ -130,3 +146,75 @@ definitions: duration: "duration" num: "num" id: "id" + 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\": ,\n \"lastName\": \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 }" + runtimeError: + type: "object" + properties: + error: + type: "string" + code: + type: "integer" + format: "int32" + message: + type: "string" + details: + type: "array" + items: + $ref: "#/definitions/protobufAny" diff --git a/examples/clients/unannotatedecho/api_unannotated_echo_service.go b/examples/clients/unannotatedecho/api_unannotated_echo_service.go index 7bbd4f9e92f..d864a7411e7 100644 --- a/examples/clients/unannotatedecho/api_unannotated_echo_service.go +++ b/examples/clients/unannotatedecho/api_unannotated_echo_service.go @@ -109,6 +109,17 @@ func (a *UnannotatedEchoServiceApiService) Echo(ctx context.Context, id string) return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -210,6 +221,17 @@ func (a *UnannotatedEchoServiceApiService) Echo2(ctx context.Context, id string, return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -299,6 +321,17 @@ func (a *UnannotatedEchoServiceApiService) EchoBody(ctx context.Context, body Ex return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } @@ -405,6 +438,17 @@ func (a *UnannotatedEchoServiceApiService) EchoDelete(ctx context.Context, local return localVarReturnValue, localVarHttpResponse, newErr } + if localVarHttpResponse.StatusCode == 0 { + var v RuntimeError + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return localVarReturnValue, localVarHttpResponse, newErr + } + newErr.model = v + return localVarReturnValue, localVarHttpResponse, newErr + } + return localVarReturnValue, localVarHttpResponse, newErr } diff --git a/examples/clients/unannotatedecho/model_protobuf_any.go b/examples/clients/unannotatedecho/model_protobuf_any.go new file mode 100644 index 00000000000..6959023a2ac --- /dev/null +++ b/examples/clients/unannotatedecho/model_protobuf_any.go @@ -0,0 +1,18 @@ +/* + * examples/proto/examplepb/unannotated_echo_service.proto + * + * Unannotated Echo Service Similar to echo_service.proto but without annotations. See unannotated_echo_service.yaml for the equivalent of the annotations in gRPC API configuration format. Echo Service API consists of a single service which returns a message. + * + * API version: version not set + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package unannotatedecho + +// `Any` contains an arbitrary serialized protocol buffer message along with a URL that describes the type of the serialized message. Protobuf library provides support to pack/unpack Any values in the form of utility functions or additional generated methods of the Any type. Example 1: Pack and unpack a message in C++. Foo foo = ...; Any any; any.PackFrom(foo); ... if (any.UnpackTo(&foo)) { ... } Example 2: Pack and unpack a message in Java. Foo foo = ...; Any any = Any.pack(foo); ... if (any.is(Foo.class)) { foo = any.unpack(Foo.class); } Example 3: Pack and unpack a message in Python. foo = Foo(...) any = Any() any.Pack(foo) ... if any.Is(Foo.DESCRIPTOR): any.Unpack(foo) ... Example 4: Pack and unpack a message in Go foo := &pb.Foo{...} any, err := ptypes.MarshalAny(foo) ... foo := &pb.Foo{} if err := ptypes.UnmarshalAny(any, foo); err != nil { ... } The pack methods provided by protobuf library will by default use 'type.googleapis.com/full.type.name' as the type URL and the unpack methods only use the fully qualified type name after the last '/' in the type URL, for example \"foo.bar.com/x/y.z\" will yield type name \"y.z\". JSON ==== The JSON representation of an `Any` value uses the regular representation of the deserialized, embedded message, with an additional field `@type` which contains the type URL. Example: package google.profile; message Person { string first_name = 1; string last_name = 2; } { \"@type\": \"type.googleapis.com/google.profile.Person\", \"firstName\": , \"lastName\": } If the embedded message type is well-known and has a custom JSON representation, that representation will be embedded adding a field `value` which holds the custom JSON in addition to the `@type` field. Example (for message [google.protobuf.Duration][]): { \"@type\": \"type.googleapis.com/google.protobuf.Duration\", \"value\": \"1.212s\" } +type ProtobufAny struct { + // A URL/resource name that uniquely identifies the type of the serialized protocol buffer message. This string must contain at least one \"/\" character. The last segment of the URL's path must represent the fully qualified name of the type (as in `path/google.protobuf.Duration`). The name should be in a canonical form (e.g., leading \".\" is not accepted). In practice, teams usually precompile into the binary all types that they expect it to use in the context of Any. However, for URLs which use the scheme `http`, `https`, or no scheme, one can optionally set up a type server that maps type URLs to message definitions as follows: * If no scheme is provided, `https` is assumed. * An HTTP GET on the URL must yield a [google.protobuf.Type][] value in binary format, or produce an error. * Applications are allowed to cache lookup results based on the URL, or have them precompiled into a binary to avoid any lookup. Therefore, binary compatibility needs to be preserved on changes to types. (Use versioned type names to manage breaking changes.) Note: this functionality is not currently available in the official protobuf release, and it is not used for type URLs beginning with type.googleapis.com. Schemes other than `http`, `https` (or the empty scheme) might be used with implementation specific semantics. + TypeUrl string `json:"type_url,omitempty"` + // Must be a valid serialized protocol buffer of the above specified type. + Value string `json:"value,omitempty"` +} diff --git a/examples/clients/unannotatedecho/model_runtime_error.go b/examples/clients/unannotatedecho/model_runtime_error.go new file mode 100644 index 00000000000..30f7572025d --- /dev/null +++ b/examples/clients/unannotatedecho/model_runtime_error.go @@ -0,0 +1,17 @@ +/* + * examples/proto/examplepb/unannotated_echo_service.proto + * + * Unannotated Echo Service Similar to echo_service.proto but without annotations. See unannotated_echo_service.yaml for the equivalent of the annotations in gRPC API configuration format. Echo Service API consists of a single service which returns a message. + * + * API version: version not set + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package unannotatedecho + +type RuntimeError struct { + Error_ string `json:"error,omitempty"` + Code int32 `json:"code,omitempty"` + Message string `json:"message,omitempty"` + Details []ProtobufAny `json:"details,omitempty"` +} diff --git a/examples/proto/examplepb/a_bit_of_everything.swagger.json b/examples/proto/examplepb/a_bit_of_everything.swagger.json index 033eb3e2f2c..61f298f170a 100644 --- a/examples/proto/examplepb/a_bit_of_everything.swagger.json +++ b/examples/proto/examplepb/a_bit_of_everything.swagger.json @@ -54,6 +54,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -109,6 +115,12 @@ "description": "Returned when the resource is temporarily unavailable.", "schema": {}, "x-number": 100 + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -154,6 +166,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -463,6 +481,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -778,6 +802,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -827,6 +857,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1157,6 +1193,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1338,6 +1380,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1388,6 +1436,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1427,6 +1481,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1476,6 +1536,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1525,6 +1591,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1755,6 +1827,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1802,6 +1880,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1863,6 +1947,12 @@ "description": "Returned when the resource is temporarily unavailable.", "schema": {}, "x-number": 100 + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1918,6 +2008,12 @@ "description": "Returned when the resource is temporarily unavailable.", "schema": {}, "x-number": 100 + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -1965,6 +2061,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "tags": [ @@ -1998,6 +2100,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "tags": [ @@ -2031,6 +2139,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -2080,6 +2194,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "tags": [ @@ -2113,6 +2233,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -2162,6 +2288,12 @@ "schema": { "$ref": "#/definitions/examplepbNumericEnum" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -2580,6 +2712,21 @@ ], "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\none \"/\" character. The last segment of the URL's path must represent\nthe 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\nexpect it to use in the context of Any. However, for URLs which use the\nscheme `http`, `https`, or no scheme, one can optionally set up a type\nserver 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." + } + }, + "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(\u0026foo)) {\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 := \u0026pb.Foo{...}\n any, err := ptypes.MarshalAny(foo)\n ...\n foo := \u0026pb.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\nname \"y.z\".\n\n\nJSON\n====\nThe JSON representation of an `Any` value uses the regular\nrepresentation of the deserialized, embedded message, with an\nadditional 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\": \u003cstring\u003e,\n \"lastName\": \u003cstring\u003e\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": { @@ -2594,6 +2741,27 @@ "description": "paths: \"f.a\"\n paths: \"f.b.d\"\n\nHere `f` represents a field in some root message, `a` and `b`\nfields in the message found in `f`, and `d` a field found in the\nmessage in `f.b`.\n\nField masks are used to specify a subset of fields that should be\nreturned by a get operation or modified by an update operation.\nField masks also have a custom JSON encoding (see below).\n\n# Field Masks in Projections\n\nWhen used in the context of a projection, a response message or\nsub-message is filtered by the API to only contain those fields as\nspecified in the mask. For example, if the mask in the previous\nexample is applied to a response message as follows:\n\n f {\n a : 22\n b {\n d : 1\n x : 2\n }\n y : 13\n }\n z: 8\n\nThe result will not contain specific values for fields x,y and z\n(their value will be set to the default, and omitted in proto text\noutput):\n\n\n f {\n a : 22\n b {\n d : 1\n }\n }\n\nA repeated field is not allowed except at the last position of a\npaths string.\n\nIf a FieldMask object is not present in a get operation, the\noperation applies to all fields (as if a FieldMask of all fields\nhad been specified).\n\nNote that a field mask does not necessarily apply to the\ntop-level response message. In case of a REST get operation, the\nfield mask applies directly to the response, but in case of a REST\nlist operation, the mask instead applies to each individual message\nin the returned resource list. In case of a REST custom method,\nother definitions may be used. Where the mask applies will be\nclearly documented together with its declaration in the API. In\nany case, the effect on the returned resource/resources is required\nbehavior for APIs.\n\n# Field Masks in Update Operations\n\nA field mask in update operations specifies which fields of the\ntargeted resource are going to be updated. The API is required\nto only change the values of the fields as specified in the mask\nand leave the others untouched. If a resource is passed in to\ndescribe the updated values, the API ignores the values of all\nfields not covered by the mask.\n\nIf a repeated field is specified for an update operation, new values will\nbe appended to the existing repeated field in the target resource. Note that\na repeated field is only allowed in the last position of a `paths` string.\n\nIf a sub-message is specified in the last position of the field mask for an\nupdate operation, then new value will be merged into the existing sub-message\nin the target resource.\n\nFor example, given the target message:\n\n f {\n b {\n d: 1\n x: 2\n }\n c: [1]\n }\n\nAnd an update message:\n\n f {\n b {\n d: 10\n }\n c: [2]\n }\n\nthen if the field mask is:\n\n paths: [\"f.b\", \"f.c\"]\n\nthen the result will be:\n\n f {\n b {\n d: 10\n x: 2\n }\n c: [1, 2]\n }\n\nAn implementation may provide options to override this default behavior for\nrepeated and message fields.\n\nIn order to reset a field's value to the default, the field must\nbe in the mask and set to the default value in the provided resource.\nHence, in order to reset all fields of a resource, provide a default\ninstance of the resource and set all fields in the mask, or do\nnot provide a mask as described below.\n\nIf a field mask is not present on update, the operation applies to\nall fields (as if a field mask of all fields has been specified).\nNote that in the presence of schema evolution, this may mean that\nfields the client does not know and has therefore not filled into\nthe request will be reset to their default. If this is unwanted\nbehavior, a specific service may require a client to always specify\na field mask, producing an error if not.\n\nAs with get operations, the location of the resource which\ndescribes the updated values in the request message depends on the\noperation kind. In any case, the effect of the field mask is\nrequired to be honored by the API.\n\n## Considerations for HTTP REST\n\nThe HTTP kind of an update operation which uses a field mask must\nbe set to PATCH instead of PUT in order to satisfy HTTP semantics\n(PUT must only be used for full updates).\n\n# JSON Encoding of Field Masks\n\nIn JSON, a field mask is encoded as a single string where paths are\nseparated by a comma. Fields name in each path are converted\nto/from lower-camel naming conventions.\n\nAs an example, consider the following message declarations:\n\n message Profile {\n User user = 1;\n Photo photo = 2;\n }\n message User {\n string display_name = 1;\n string address = 2;\n }\n\nIn proto a field mask for `Profile` may look as such:\n\n mask {\n paths: \"user.display_name\"\n paths: \"photo\"\n }\n\nIn JSON, the same mask is represented as below:\n\n {\n mask: \"user.displayName,photo\"\n }\n\n# Field Masks and Oneof Fields\n\nField masks treat fields in oneofs just as regular fields. Consider the\nfollowing message:\n\n message SampleMessage {\n oneof test_oneof {\n string name = 4;\n SubMessage sub_message = 9;\n }\n }\n\nThe field mask can be:\n\n mask {\n paths: \"name\"\n }\n\nOr:\n\n mask {\n paths: \"sub_message\"\n }\n\nNote that oneof type names (\"test_oneof\" in this case) cannot be used in\npaths.\n\n## Field Mask Verification\n\nThe implementation of any 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.", "title": "`FieldMask` represents a set of symbolic field paths, for example:" }, + "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": { diff --git a/examples/proto/examplepb/echo_service.swagger.json b/examples/proto/examplepb/echo_service.swagger.json index 45187c3e463..f7b35c2ddee 100644 --- a/examples/proto/examplepb/echo_service.swagger.json +++ b/examples/proto/examplepb/echo_service.swagger.json @@ -23,6 +23,12 @@ "schema": { "$ref": "#/definitions/examplepbSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -50,6 +56,12 @@ "schema": { "$ref": "#/definitions/examplepbSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -130,6 +142,12 @@ "schema": { "$ref": "#/definitions/examplepbSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -210,6 +228,12 @@ "schema": { "$ref": "#/definitions/examplepbSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -284,6 +308,12 @@ "schema": { "$ref": "#/definitions/examplepbSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -357,6 +387,12 @@ "schema": { "$ref": "#/definitions/examplepbSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -384,6 +420,12 @@ "schema": { "$ref": "#/definitions/examplepbSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -498,6 +540,42 @@ } }, "description": "SimpleMessage represents a simple message sent to the Echo service." + }, + "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\none \"/\" character. The last segment of the URL's path must represent\nthe 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\nexpect it to use in the context of Any. However, for URLs which use the\nscheme `http`, `https`, or no scheme, one can optionally set up a type\nserver 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." + } + }, + "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(\u0026foo)) {\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 := \u0026pb.Foo{...}\n any, err := ptypes.MarshalAny(foo)\n ...\n foo := \u0026pb.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\nname \"y.z\".\n\n\nJSON\n====\nThe JSON representation of an `Any` value uses the regular\nrepresentation of the deserialized, embedded message, with an\nadditional 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\": \u003cstring\u003e,\n \"lastName\": \u003cstring\u003e\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 }" + }, + "runtimeError": { + "type": "object", + "properties": { + "error": { + "type": "string" + }, + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/protobufAny" + } + } + } } } } diff --git a/examples/proto/examplepb/response_body_service.swagger.json b/examples/proto/examplepb/response_body_service.swagger.json index ea0ced6dcdb..50fa2d025c4 100644 --- a/examples/proto/examplepb/response_body_service.swagger.json +++ b/examples/proto/examplepb/response_body_service.swagger.json @@ -23,6 +23,12 @@ "$ref": "#/definitions/examplepbRepeatedResponseBodyOutResponse" } } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -47,6 +53,12 @@ "schema": { "$ref": "#/definitions/examplepbResponseBodyOutResponse" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -74,6 +86,12 @@ "type": "string" } } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -149,6 +167,42 @@ "type": "string" } } + }, + "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\none \"/\" character. The last segment of the URL's path must represent\nthe 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\nexpect it to use in the context of Any. However, for URLs which use the\nscheme `http`, `https`, or no scheme, one can optionally set up a type\nserver 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." + } + }, + "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(\u0026foo)) {\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 := \u0026pb.Foo{...}\n any, err := ptypes.MarshalAny(foo)\n ...\n foo := \u0026pb.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\nname \"y.z\".\n\n\nJSON\n====\nThe JSON representation of an `Any` value uses the regular\nrepresentation of the deserialized, embedded message, with an\nadditional 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\": \u003cstring\u003e,\n \"lastName\": \u003cstring\u003e\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 }" + }, + "runtimeError": { + "type": "object", + "properties": { + "error": { + "type": "string" + }, + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/protobufAny" + } + } + } } } } diff --git a/examples/proto/examplepb/stream.swagger.json b/examples/proto/examplepb/stream.swagger.json index 84215a088c1..c4c696989a6 100644 --- a/examples/proto/examplepb/stream.swagger.json +++ b/examples/proto/examplepb/stream.swagger.json @@ -29,6 +29,12 @@ }, "title": "Stream result of examplepbABitOfEverything" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "tags": [ @@ -45,6 +51,12 @@ "schema": { "properties": {} } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -81,6 +93,12 @@ }, "title": "Stream result of subStringMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -359,6 +377,27 @@ }, "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(\u0026foo)) {\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 := \u0026pb.Foo{...}\n any, err := ptypes.MarshalAny(foo)\n ...\n foo := \u0026pb.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\nname \"y.z\".\n\n\nJSON\n====\nThe JSON representation of an `Any` value uses the regular\nrepresentation of the deserialized, embedded message, with an\nadditional 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\": \u003cstring\u003e,\n \"lastName\": \u003cstring\u003e\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 }" }, + "runtimeError": { + "type": "object", + "properties": { + "error": { + "type": "string" + }, + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/protobufAny" + } + } + } + }, "runtimeStreamError": { "type": "object", "properties": { diff --git a/examples/proto/examplepb/unannotated_echo_service.swagger.json b/examples/proto/examplepb/unannotated_echo_service.swagger.json index 26dc292cc17..c33a67d98f6 100644 --- a/examples/proto/examplepb/unannotated_echo_service.swagger.json +++ b/examples/proto/examplepb/unannotated_echo_service.swagger.json @@ -23,6 +23,12 @@ "schema": { "$ref": "#/definitions/examplepbUnannotatedSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -50,6 +56,12 @@ "schema": { "$ref": "#/definitions/examplepbUnannotatedSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -89,6 +101,12 @@ "schema": { "$ref": "#/definitions/examplepbUnannotatedSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -116,6 +134,12 @@ "schema": { "$ref": "#/definitions/examplepbUnannotatedSimpleMessage" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -163,6 +187,42 @@ } }, "description": "UnannotatedSimpleMessage represents a simple message sent to the unannotated Echo service." + }, + "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\none \"/\" character. The last segment of the URL's path must represent\nthe 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\nexpect it to use in the context of Any. However, for URLs which use the\nscheme `http`, `https`, or no scheme, one can optionally set up a type\nserver 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." + } + }, + "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(\u0026foo)) {\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 := \u0026pb.Foo{...}\n any, err := ptypes.MarshalAny(foo)\n ...\n foo := \u0026pb.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\nname \"y.z\".\n\n\nJSON\n====\nThe JSON representation of an `Any` value uses the regular\nrepresentation of the deserialized, embedded message, with an\nadditional 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\": \u003cstring\u003e,\n \"lastName\": \u003cstring\u003e\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 }" + }, + "runtimeError": { + "type": "object", + "properties": { + "error": { + "type": "string" + }, + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/protobufAny" + } + } + } } } } diff --git a/examples/proto/examplepb/use_go_template.swagger.json b/examples/proto/examplepb/use_go_template.swagger.json index 011b33aeaf7..c85ad10e119 100644 --- a/examples/proto/examplepb/use_go_template.swagger.json +++ b/examples/proto/examplepb/use_go_template.swagger.json @@ -22,6 +22,12 @@ "schema": { "$ref": "#/definitions/examplepbLoginReply" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -50,6 +56,12 @@ "schema": { "$ref": "#/definitions/examplepbLogoutReply" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -126,6 +138,42 @@ "title": "This is an array" } } + }, + "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\none \"/\" character. The last segment of the URL's path must represent\nthe 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\nexpect it to use in the context of Any. However, for URLs which use the\nscheme `http`, `https`, or no scheme, one can optionally set up a type\nserver 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." + } + }, + "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(\u0026foo)) {\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 := \u0026pb.Foo{...}\n any, err := ptypes.MarshalAny(foo)\n ...\n foo := \u0026pb.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\nname \"y.z\".\n\n\nJSON\n====\nThe JSON representation of an `Any` value uses the regular\nrepresentation of the deserialized, embedded message, with an\nadditional 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\": \u003cstring\u003e,\n \"lastName\": \u003cstring\u003e\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 }" + }, + "runtimeError": { + "type": "object", + "properties": { + "error": { + "type": "string" + }, + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/protobufAny" + } + } + } } } } diff --git a/examples/proto/examplepb/wrappers.swagger.json b/examples/proto/examplepb/wrappers.swagger.json index 7c7c8566bd6..b6a93601d36 100644 --- a/examples/proto/examplepb/wrappers.swagger.json +++ b/examples/proto/examplepb/wrappers.swagger.json @@ -20,6 +20,12 @@ "schema": { "$ref": "#/definitions/examplepbWrappers" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -47,6 +53,12 @@ "type": "boolean", "format": "boolean" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -75,6 +87,12 @@ "type": "string", "format": "byte" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -103,6 +121,12 @@ "type": "number", "format": "double" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -130,6 +154,12 @@ "schema": { "properties": {} } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -157,6 +187,12 @@ "type": "number", "format": "float" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -185,6 +221,12 @@ "type": "integer", "format": "int32" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -213,6 +255,12 @@ "type": "string", "format": "int64" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -240,6 +288,12 @@ "schema": { "type": "string" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -267,6 +321,12 @@ "type": "integer", "format": "int64" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -295,6 +355,12 @@ "type": "string", "format": "uint64" } + }, + "default": { + "description": "An unexpected error response", + "schema": { + "$ref": "#/definitions/runtimeError" + } } }, "parameters": [ @@ -354,6 +420,42 @@ "format": "byte" } } + }, + "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\none \"/\" character. The last segment of the URL's path must represent\nthe 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\nexpect it to use in the context of Any. However, for URLs which use the\nscheme `http`, `https`, or no scheme, one can optionally set up a type\nserver 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." + } + }, + "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(\u0026foo)) {\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 := \u0026pb.Foo{...}\n any, err := ptypes.MarshalAny(foo)\n ...\n foo := \u0026pb.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\nname \"y.z\".\n\n\nJSON\n====\nThe JSON representation of an `Any` value uses the regular\nrepresentation of the deserialized, embedded message, with an\nadditional 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\": \u003cstring\u003e,\n \"lastName\": \u003cstring\u003e\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 }" + }, + "runtimeError": { + "type": "object", + "properties": { + "error": { + "type": "string" + }, + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/protobufAny" + } + } + } } } } diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index 5f73042d6f6..5242751fb2d 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel @@ -6,7 +6,7 @@ package(default_visibility = ["//visibility:public"]) proto_library( name = "internal_proto", - srcs = ["stream_chunk.proto"], + srcs = ["errors.proto"], deps = ["@com_google_protobuf//:any_proto"], ) diff --git a/internal/errors.pb.go b/internal/errors.pb.go new file mode 100644 index 00000000000..61101d7177f --- /dev/null +++ b/internal/errors.pb.go @@ -0,0 +1,189 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: internal/errors.proto + +package internal + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + any "github.com/golang/protobuf/ptypes/any" + math "math" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// Error is the generic error returned from unary RPCs. +type Error struct { + Error string `protobuf:"bytes,1,opt,name=error,proto3" json:"error,omitempty"` + // This is to make the error more compatible with users that expect errors to be Status objects: + // https://github.com/grpc/grpc/blob/master/src/proto/grpc/status/status.proto + // It should be the exact same message as the Error field. + Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"` + Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"` + Details []*any.Any `protobuf:"bytes,4,rep,name=details,proto3" json:"details,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Error) Reset() { *m = Error{} } +func (m *Error) String() string { return proto.CompactTextString(m) } +func (*Error) ProtoMessage() {} +func (*Error) Descriptor() ([]byte, []int) { + return fileDescriptor_9b093362ca6d1e03, []int{0} +} + +func (m *Error) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Error.Unmarshal(m, b) +} +func (m *Error) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Error.Marshal(b, m, deterministic) +} +func (m *Error) XXX_Merge(src proto.Message) { + xxx_messageInfo_Error.Merge(m, src) +} +func (m *Error) XXX_Size() int { + return xxx_messageInfo_Error.Size(m) +} +func (m *Error) XXX_DiscardUnknown() { + xxx_messageInfo_Error.DiscardUnknown(m) +} + +var xxx_messageInfo_Error proto.InternalMessageInfo + +func (m *Error) GetError() string { + if m != nil { + return m.Error + } + return "" +} + +func (m *Error) GetCode() int32 { + if m != nil { + return m.Code + } + return 0 +} + +func (m *Error) GetMessage() string { + if m != nil { + return m.Message + } + return "" +} + +func (m *Error) GetDetails() []*any.Any { + if m != nil { + return m.Details + } + return nil +} + +// StreamError is a response type which is returned when +// streaming rpc returns an error. +type StreamError struct { + GrpcCode int32 `protobuf:"varint,1,opt,name=grpc_code,json=grpcCode,proto3" json:"grpc_code,omitempty"` + HttpCode int32 `protobuf:"varint,2,opt,name=http_code,json=httpCode,proto3" json:"http_code,omitempty"` + Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"` + HttpStatus string `protobuf:"bytes,4,opt,name=http_status,json=httpStatus,proto3" json:"http_status,omitempty"` + Details []*any.Any `protobuf:"bytes,5,rep,name=details,proto3" json:"details,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *StreamError) Reset() { *m = StreamError{} } +func (m *StreamError) String() string { return proto.CompactTextString(m) } +func (*StreamError) ProtoMessage() {} +func (*StreamError) Descriptor() ([]byte, []int) { + return fileDescriptor_9b093362ca6d1e03, []int{1} +} + +func (m *StreamError) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_StreamError.Unmarshal(m, b) +} +func (m *StreamError) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_StreamError.Marshal(b, m, deterministic) +} +func (m *StreamError) XXX_Merge(src proto.Message) { + xxx_messageInfo_StreamError.Merge(m, src) +} +func (m *StreamError) XXX_Size() int { + return xxx_messageInfo_StreamError.Size(m) +} +func (m *StreamError) XXX_DiscardUnknown() { + xxx_messageInfo_StreamError.DiscardUnknown(m) +} + +var xxx_messageInfo_StreamError proto.InternalMessageInfo + +func (m *StreamError) GetGrpcCode() int32 { + if m != nil { + return m.GrpcCode + } + return 0 +} + +func (m *StreamError) GetHttpCode() int32 { + if m != nil { + return m.HttpCode + } + return 0 +} + +func (m *StreamError) GetMessage() string { + if m != nil { + return m.Message + } + return "" +} + +func (m *StreamError) GetHttpStatus() string { + if m != nil { + return m.HttpStatus + } + return "" +} + +func (m *StreamError) GetDetails() []*any.Any { + if m != nil { + return m.Details + } + return nil +} + +func init() { + proto.RegisterType((*Error)(nil), "grpc.gateway.runtime.Error") + proto.RegisterType((*StreamError)(nil), "grpc.gateway.runtime.StreamError") +} + +func init() { proto.RegisterFile("internal/errors.proto", fileDescriptor_9b093362ca6d1e03) } + +var fileDescriptor_9b093362ca6d1e03 = []byte{ + // 252 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x90, 0xc1, 0x4a, 0xc4, 0x30, + 0x10, 0x86, 0x89, 0xbb, 0x75, 0xdb, 0xe9, 0x2d, 0x54, 0x88, 0xee, 0xc1, 0xb2, 0xa7, 0x9e, 0x52, + 0xd0, 0x27, 0xd0, 0xc5, 0x17, 0xe8, 0xde, 0xbc, 0x2c, 0xd9, 0xdd, 0x31, 0x16, 0xda, 0xa4, 0x24, + 0x53, 0xa4, 0xf8, 0x56, 0x3e, 0xa1, 0x24, 0xa5, 0xb0, 0x27, 0xf1, 0xd6, 0xf9, 0xfb, 0xcf, 0x7c, + 0x1f, 0x81, 0xbb, 0xd6, 0x10, 0x3a, 0xa3, 0xba, 0x1a, 0x9d, 0xb3, 0xce, 0xcb, 0xc1, 0x59, 0xb2, + 0xbc, 0xd0, 0x6e, 0x38, 0x4b, 0xad, 0x08, 0xbf, 0xd4, 0x24, 0xdd, 0x68, 0xa8, 0xed, 0xf1, 0xe1, + 0x5e, 0x5b, 0xab, 0x3b, 0xac, 0x63, 0xe7, 0x34, 0x7e, 0xd4, 0xca, 0x4c, 0xf3, 0xc2, 0xee, 0x1b, + 0x92, 0xb7, 0x70, 0x80, 0x17, 0x90, 0xc4, 0x4b, 0x82, 0x95, 0xac, 0xca, 0x9a, 0x79, 0xe0, 0x1c, + 0xd6, 0x67, 0x7b, 0x41, 0x71, 0x53, 0xb2, 0x2a, 0x69, 0xe2, 0x37, 0x17, 0xb0, 0xe9, 0xd1, 0x7b, + 0xa5, 0x51, 0xac, 0x62, 0x77, 0x19, 0xb9, 0x84, 0xcd, 0x05, 0x49, 0xb5, 0x9d, 0x17, 0xeb, 0x72, + 0x55, 0xe5, 0x4f, 0x85, 0x9c, 0xc9, 0x72, 0x21, 0xcb, 0x17, 0x33, 0x35, 0x4b, 0x69, 0xf7, 0xc3, + 0x20, 0x3f, 0x90, 0x43, 0xd5, 0xcf, 0x0e, 0x5b, 0xc8, 0x82, 0xff, 0x31, 0x22, 0x59, 0x44, 0xa6, + 0x21, 0xd8, 0x07, 0xec, 0x16, 0xb2, 0x4f, 0xa2, 0xe1, 0x78, 0xe5, 0x93, 0x86, 0x60, 0xff, 0xb7, + 0xd3, 0x23, 0xe4, 0x71, 0xcd, 0x93, 0xa2, 0x31, 0x78, 0x85, 0xbf, 0x10, 0xa2, 0x43, 0x4c, 0xae, + 0xa5, 0x93, 0x7f, 0x48, 0xbf, 0xc2, 0x7b, 0xba, 0xbc, 0xfd, 0xe9, 0x36, 0x56, 0x9e, 0x7f, 0x03, + 0x00, 0x00, 0xff, 0xff, 0xde, 0x72, 0x6b, 0x83, 0x8e, 0x01, 0x00, 0x00, +} diff --git a/internal/errors.proto b/internal/errors.proto new file mode 100644 index 00000000000..4fb212c6b69 --- /dev/null +++ b/internal/errors.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; +package grpc.gateway.runtime; +option go_package = "internal"; + +import "google/protobuf/any.proto"; + +// Error is the generic error returned from unary RPCs. +message Error { + string error = 1; + // This is to make the error more compatible with users that expect errors to be Status objects: + // https://github.com/grpc/grpc/blob/master/src/proto/grpc/status/status.proto + // It should be the exact same message as the Error field. + int32 code = 2; + string message = 3; + repeated google.protobuf.Any details = 4; +} + +// StreamError is a response type which is returned when +// streaming rpc returns an error. +message StreamError { + int32 grpc_code = 1; + int32 http_code = 2; + string message = 3; + string http_status = 4; + repeated google.protobuf.Any details = 5; +} diff --git a/internal/stream_chunk.pb.go b/internal/stream_chunk.pb.go deleted file mode 100644 index 1eca68e3350..00000000000 --- a/internal/stream_chunk.pb.go +++ /dev/null @@ -1,119 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// source: internal/stream_chunk.proto - -package internal - -import ( - fmt "fmt" - proto "github.com/golang/protobuf/proto" - any "github.com/golang/protobuf/ptypes/any" - math "math" -) - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package - -// StreamError is a response type which is returned when -// streaming rpc returns an error. -type StreamError struct { - GrpcCode int32 `protobuf:"varint,1,opt,name=grpc_code,json=grpcCode,proto3" json:"grpc_code,omitempty"` - HttpCode int32 `protobuf:"varint,2,opt,name=http_code,json=httpCode,proto3" json:"http_code,omitempty"` - Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"` - HttpStatus string `protobuf:"bytes,4,opt,name=http_status,json=httpStatus,proto3" json:"http_status,omitempty"` - Details []*any.Any `protobuf:"bytes,5,rep,name=details,proto3" json:"details,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *StreamError) Reset() { *m = StreamError{} } -func (m *StreamError) String() string { return proto.CompactTextString(m) } -func (*StreamError) ProtoMessage() {} -func (*StreamError) Descriptor() ([]byte, []int) { - return fileDescriptor_9d15b670e96bbb5a, []int{0} -} - -func (m *StreamError) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_StreamError.Unmarshal(m, b) -} -func (m *StreamError) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_StreamError.Marshal(b, m, deterministic) -} -func (m *StreamError) XXX_Merge(src proto.Message) { - xxx_messageInfo_StreamError.Merge(m, src) -} -func (m *StreamError) XXX_Size() int { - return xxx_messageInfo_StreamError.Size(m) -} -func (m *StreamError) XXX_DiscardUnknown() { - xxx_messageInfo_StreamError.DiscardUnknown(m) -} - -var xxx_messageInfo_StreamError proto.InternalMessageInfo - -func (m *StreamError) GetGrpcCode() int32 { - if m != nil { - return m.GrpcCode - } - return 0 -} - -func (m *StreamError) GetHttpCode() int32 { - if m != nil { - return m.HttpCode - } - return 0 -} - -func (m *StreamError) GetMessage() string { - if m != nil { - return m.Message - } - return "" -} - -func (m *StreamError) GetHttpStatus() string { - if m != nil { - return m.HttpStatus - } - return "" -} - -func (m *StreamError) GetDetails() []*any.Any { - if m != nil { - return m.Details - } - return nil -} - -func init() { - proto.RegisterType((*StreamError)(nil), "grpc.gateway.runtime.StreamError") -} - -func init() { proto.RegisterFile("internal/stream_chunk.proto", fileDescriptor_9d15b670e96bbb5a) } - -var fileDescriptor_9d15b670e96bbb5a = []byte{ - // 223 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x34, 0x90, 0x41, 0x4e, 0xc3, 0x30, - 0x10, 0x45, 0x15, 0x4a, 0x69, 0x3b, 0xd9, 0x45, 0x5d, 0x18, 0xba, 0x20, 0x62, 0x95, 0x95, 0x23, - 0xc1, 0x09, 0x00, 0x71, 0x81, 0x74, 0xc7, 0xa6, 0x9a, 0x26, 0x83, 0x13, 0x91, 0xd8, 0xd1, 0x78, - 0x22, 0x94, 0x6b, 0x71, 0xc2, 0xca, 0x8e, 0xb2, 0xf4, 0x7b, 0x7f, 0xbe, 0xbe, 0x0c, 0xa7, 0xce, - 0x0a, 0xb1, 0xc5, 0xbe, 0xf4, 0xc2, 0x84, 0xc3, 0xa5, 0x6e, 0x27, 0xfb, 0xab, 0x47, 0x76, 0xe2, - 0xb2, 0xa3, 0xe1, 0xb1, 0xd6, 0x06, 0x85, 0xfe, 0x70, 0xd6, 0x3c, 0x59, 0xe9, 0x06, 0x7a, 0x7a, - 0x34, 0xce, 0x99, 0x9e, 0xca, 0x98, 0xb9, 0x4e, 0x3f, 0x25, 0xda, 0x79, 0x39, 0x78, 0xf9, 0x4f, - 0x20, 0x3d, 0xc7, 0x9e, 0x2f, 0x66, 0xc7, 0xd9, 0x09, 0x0e, 0xa1, 0xe2, 0x52, 0xbb, 0x86, 0x54, - 0x92, 0x27, 0xc5, 0xb6, 0xda, 0x07, 0xf0, 0xe9, 0x1a, 0x0a, 0xb2, 0x15, 0x19, 0x17, 0x79, 0xb7, - 0xc8, 0x00, 0xa2, 0x54, 0xb0, 0x1b, 0xc8, 0x7b, 0x34, 0xa4, 0x36, 0x79, 0x52, 0x1c, 0xaa, 0xf5, - 0x99, 0x3d, 0x43, 0x1a, 0xcf, 0xbc, 0xa0, 0x4c, 0x5e, 0xdd, 0x47, 0x0b, 0x01, 0x9d, 0x23, 0xc9, - 0x34, 0xec, 0x1a, 0x12, 0xec, 0x7a, 0xaf, 0xb6, 0xf9, 0xa6, 0x48, 0x5f, 0x8f, 0x7a, 0x59, 0xac, - 0xd7, 0xc5, 0xfa, 0xdd, 0xce, 0xd5, 0x1a, 0xfa, 0x80, 0xef, 0xfd, 0xfa, 0x09, 0xd7, 0x87, 0x18, - 0x79, 0xbb, 0x05, 0x00, 0x00, 0xff, 0xff, 0x0d, 0x7d, 0xa5, 0x18, 0x17, 0x01, 0x00, 0x00, -} diff --git a/internal/stream_chunk.proto b/internal/stream_chunk.proto deleted file mode 100644 index 55f42ce63ec..00000000000 --- a/internal/stream_chunk.proto +++ /dev/null @@ -1,15 +0,0 @@ -syntax = "proto3"; -package grpc.gateway.runtime; -option go_package = "internal"; - -import "google/protobuf/any.proto"; - -// StreamError is a response type which is returned when -// streaming rpc returns an error. -message StreamError { - int32 grpc_code = 1; - int32 http_code = 2; - string message = 3; - string http_status = 4; - repeated google.protobuf.Any details = 5; -} diff --git a/protoc-gen-swagger/genswagger/template.go b/protoc-gen-swagger/genswagger/template.go index 6b2b62eb38f..f7715c8a3ff 100644 --- a/protoc-gen-swagger/genswagger/template.go +++ b/protoc-gen-swagger/genswagger/template.go @@ -882,6 +882,15 @@ func renderServices(services []*descriptor.Service, paths swaggerPathsObject, re Description: desc, Schema: responseSchema, }, + // https://github.com/OAI/OpenAPI-Specification/blob/3.0.0/versions/2.0.md#responses-object + "default": swaggerResponseObject{ + Description: "An unexpected error response", + Schema: swaggerSchemaObject{ + schemaCore: schemaCore{ + Ref: fmt.Sprintf("#/definitions/%s", fullyQualifiedNameToSwaggerName(".grpc.gateway.runtime.Error", reg)), + }, + }, + }, }, } if bIdx == 0 { @@ -1034,14 +1043,24 @@ func applyTemplate(p param) (*swaggerObject, error) { panic(err) } + messages := messageMap{} + streamingMessages := messageMap{} + enums := enumMap{} + + // Add the error type to the message map + runtimeError, err := p.reg.LookupMsg(".grpc.gateway.runtime", "Error") + if err == nil { + messages[fullyQualifiedNameToSwaggerName(".grpc.gateway.runtime.Error", p.reg)] = runtimeError + } else { + // just in case there is an error looking up runtimeError + glog.Error(err) + } + // Find all the service's messages and enumerations that are defined (recursively) // and write request, response and other custom (but referenced) types out as definition objects. - m := messageMap{} - ms := messageMap{} - e := enumMap{} - findServicesMessagesAndEnumerations(p.Services, p.reg, m, ms, e, requestResponseRefs) - renderMessagesAsDefinition(m, s.Definitions, p.reg, customRefs) - renderEnumerationsAsDefinition(e, s.Definitions, p.reg) + findServicesMessagesAndEnumerations(p.Services, p.reg, messages, streamingMessages, enums, requestResponseRefs) + renderMessagesAsDefinition(messages, s.Definitions, p.reg, customRefs) + renderEnumerationsAsDefinition(enums, s.Definitions, p.reg) // File itself might have some comments and metadata. packageProtoPath := protoPathIndex(reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil)), "Package") diff --git a/protoc-gen-swagger/genswagger/template_test.go b/protoc-gen-swagger/genswagger/template_test.go index 9c1f0b74c70..cda39688835 100644 --- a/protoc-gen-swagger/genswagger/template_test.go +++ b/protoc-gen-swagger/genswagger/template_test.go @@ -925,7 +925,7 @@ func TestApplyTemplateRequestWithClientStreaming(t *testing.T) { } // Only ExampleMessage must be present, not NestedMessage - if want, got, name := 3, len(result.Definitions), "len(Definitions)"; !reflect.DeepEqual(got, want) { + if want, got, name := 4, len(result.Definitions), "len(Definitions)"; !reflect.DeepEqual(got, want) { t.Errorf("applyTemplate(%#v).%s = %d want to be %d", file, name, got, want) } if _, ok := result.Paths["/v1/echo"].Post.Responses["200"]; !ok { diff --git a/runtime/errors.go b/runtime/errors.go index a36080713ce..0118ca0479d 100644 --- a/runtime/errors.go +++ b/runtime/errors.go @@ -5,11 +5,10 @@ import ( "io" "net/http" - "github.com/golang/protobuf/proto" - "github.com/golang/protobuf/ptypes/any" "google.golang.org/grpc/codes" "google.golang.org/grpc/grpclog" "google.golang.org/grpc/status" + "github.com/grpc-ecosystem/grpc-gateway/internal" ) // HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status. @@ -65,21 +64,6 @@ var ( OtherErrorHandler = DefaultOtherErrorHandler ) -type errorBody struct { - Error string `protobuf:"bytes,100,name=error" json:"error"` - // This is to make the error more compatible with users that expect errors to be Status objects: - // https://github.com/grpc/grpc/blob/master/src/proto/grpc/status/status.proto - // It should be the exact same message as the Error field. - Code int32 `protobuf:"varint,1,name=code" json:"code"` - Message string `protobuf:"bytes,2,name=message" json:"message"` - Details []*any.Any `protobuf:"bytes,3,rep,name=details" json:"details,omitempty"` -} - -// Make this also conform to proto.Message for builtin JSONPb Marshaler -func (e *errorBody) Reset() { *e = errorBody{} } -func (e *errorBody) String() string { return proto.CompactTextString(e) } -func (*errorBody) ProtoMessage() {} - // DefaultHTTPError is the default implementation of HTTPError. // If "err" is an error from gRPC system, the function replies with the status code mapped by HTTPStatusFromCode. // If otherwise, it replies with http.StatusInternalServerError. @@ -106,7 +90,7 @@ func DefaultHTTPError(ctx context.Context, mux *ServeMux, marshaler Marshaler, w } w.Header().Set("Content-Type", contentType) - body := &errorBody{ + body := &internal.Error{ Error: s.Message(), Message: s.Message(), Code: int32(s.Code()),