Skip to content

Commit

Permalink
fix import alias issue with protoc-gen-grpc-gateway (#1757)
Browse files Browse the repository at this point in the history
* return registry from gengateway.New

retain parsing order

comment

* update test after merge

* add example to abe

* bad merge

* cut and paste err

* simple change

* simplify

* outdated

* goimports
  • Loading branch information
danielhochman committed Oct 16, 2020
1 parent f2e74d6 commit 80a7a7b
Show file tree
Hide file tree
Showing 27 changed files with 1,546 additions and 879 deletions.
1 change: 1 addition & 0 deletions examples/internal/clients/abe/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"model_examplepb_a_bit_of_everything_repeated.go",
"model_examplepb_body.go",
"model_examplepb_book.go",
"model_examplepb_check_status_response.go",
"model_examplepb_numeric_enum.go",
"model_examplepb_update_v2_request.go",
"model_message_path_enum_nested_path_enum.go",
Expand Down
92 changes: 92 additions & 0 deletions examples/internal/clients/abe/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,34 @@ paths:
description: "An unexpected error response."
schema:
$ref: "#/definitions/rpcStatus"
/v1/example/checkStatus:
get:
tags:
- "ABitOfEverythingService"
operationId: "ABitOfEverythingService_CheckStatus"
parameters: []
responses:
200:
description: "A successful response."
schema:
$ref: "#/definitions/examplepbCheckStatusResponse"
403:
description: "Returned when the user does not have permission to access\
\ the resource."
schema: {}
404:
description: "Returned when the resource does not exist."
schema:
type: "string"
format: "string"
418:
description: "I'm a teapot."
schema:
$ref: "#/definitions/examplepbNumericEnum"
default:
description: "An unexpected error response."
schema:
$ref: "#/definitions/rpcStatus"
/v1/example/deep_path/{singleNested.name}:
post:
tags:
Expand Down Expand Up @@ -2564,6 +2592,20 @@ definitions:
createTime: "2000-01-23T04:56:07.000+00:00"
name: "name"
id: "id"
examplepbCheckStatusResponse:
type: "object"
properties:
status:
$ref: "#/definitions/rpcStatus"
example:
status:
code: 0
details:
- typeUrl: "typeUrl"
value: "value"
- typeUrl: "typeUrl"
value: "value"
message: "message"
examplepbNumericEnum:
type: "string"
description: "NumericEnum is one or zero.\n\n - ZERO: ZERO means 0\n - ONE: ONE\
Expand Down Expand Up @@ -2648,18 +2690,68 @@ definitions:
\ 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 }"
example:
typeUrl: "typeUrl"
value: "value"
rpcStatus:
type: "object"
properties:
code:
type: "integer"
format: "int32"
description: "The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]."
message:
type: "string"
description: "A developer-facing error message, which should be in English.\
\ Any\nuser-facing error message should be localized and sent in the\n[google.rpc.Status.details][google.rpc.Status.details]\
\ field, or localized by the client."
details:
type: "array"
description: "A list of messages that carry the error details. There is a\
\ common set of\nmessage types for APIs to use."
items:
$ref: "#/definitions/protobufAny"
title: "The `Status` type defines a logical error model that is suitable for different\n\
programming environments, including REST APIs and RPC APIs. It is used by\n\
[gRPC](https://github.com/grpc). The error model is designed to be:"
description: "- Simple to use and understand for most users\n- Flexible enough\
\ to meet unexpected needs\n\n# Overview\n\nThe `Status` message contains three\
\ pieces of data: error code, error message,\nand error details. The error code\
\ should be an enum value of\n[google.rpc.Code][google.rpc.Code], but it may\
\ accept additional error codes if needed. The\nerror message should be a developer-facing\
\ English message that helps\ndevelopers *understand* and *resolve* the error.\
\ If a localized user-facing\nerror message is needed, put the localized message\
\ in the error details or\nlocalize it in the client. The optional error details\
\ may contain arbitrary\ninformation about the error. There is a predefined\
\ set of error detail types\nin the package `google.rpc` that can be used for\
\ common error conditions.\n\n# Language mapping\n\nThe `Status` message is\
\ the logical representation of the error model, but it\nis not necessarily\
\ the actual wire format. When the `Status` message is\nexposed in different\
\ client libraries and different wire protocols, it can be\nmapped differently.\
\ For example, it will likely be mapped to some exceptions\nin Java, but more\
\ likely mapped to some error codes in C.\n\n# Other uses\n\nThe error model\
\ and the `Status` message can be used in a variety of\nenvironments, either\
\ with or without APIs, to provide a\nconsistent developer experience across\
\ different environments.\n\nExample uses of this error model include:\n\n-\
\ Partial errors. If a service needs to return partial errors to the client,\n\
\ it may embed the `Status` in the normal response to indicate the partial\n\
\ errors.\n\n- Workflow errors. A typical workflow has multiple steps. Each\
\ step may\n have a `Status` message for error reporting.\n\n- Batch operations.\
\ If a client uses batch request and batch response, the\n `Status` message\
\ should be used directly inside batch response, one for\n each error sub-response.\n\
\n- Asynchronous operations. If an API call embeds asynchronous operation\n\
\ results in its response, the status of those operations should be\n \
\ represented directly using the `Status` message.\n\n- Logging. If some API\
\ errors are stored in logs, the message `Status` could\n be used directly\
\ after any stripping needed for security/privacy reasons."
example:
code: 0
details:
- typeUrl: "typeUrl"
value: "value"
- typeUrl: "typeUrl"
value: "value"
message: "message"
subStringMessage:
type: "object"
properties:
Expand Down
143 changes: 143 additions & 0 deletions examples/internal/clients/abe/api_a_bit_of_everything_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,149 @@ func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckPostQuer
return localVarReturnValue, localVarHttpResponse, nil
}

/*
ABitOfEverythingServiceApiService
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@return ExamplepbCheckStatusResponse
*/
func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckStatus(ctx context.Context) (ExamplepbCheckStatusResponse, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Get")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
localVarReturnValue ExamplepbCheckStatusResponse
)

// create path and map variables
localVarPath := a.client.cfg.BasePath + "/v1/example/checkStatus"

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}

// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json", "application/x-foo-mime"}

// set Content-Type header
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
if localVarHttpContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHttpContentType
}

// to determine the Accept header
localVarHttpHeaderAccepts := []string{"application/json", "application/x-foo-mime"}

// set Accept header
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
if ctx != nil {
// API Key Authentication
if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
var key string
if auth.Prefix != "" {
key = auth.Prefix + " " + auth.Key
} else {
key = auth.Key
}
localVarHeaderParams["X-API-Key"] = key

}
}
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return localVarReturnValue, nil, err
}

localVarHttpResponse, err := a.client.callAPI(r)
if err != nil || localVarHttpResponse == nil {
return localVarReturnValue, localVarHttpResponse, err
}

localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
localVarHttpResponse.Body.Close()
if err != nil {
return localVarReturnValue, localVarHttpResponse, err
}

if localVarHttpResponse.StatusCode < 300 {
// If we succeed, return the data, otherwise pass on to decode error.
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
if err == nil {
return localVarReturnValue, localVarHttpResponse, err
}
}

if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericSwaggerError{
body: localVarBody,
error: localVarHttpResponse.Status,
}

if localVarHttpResponse.StatusCode == 200 {
var v ExamplepbCheckStatusResponse
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
}

if localVarHttpResponse.StatusCode == 403 {
var v interface{}
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
}

if localVarHttpResponse.StatusCode == 404 {
var v string
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
}

if localVarHttpResponse.StatusCode == 418 {
var v ExamplepbNumericEnum
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
}

if localVarHttpResponse.StatusCode == 0 {
var v RpcStatus
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
}

return localVarReturnValue, localVarHttpResponse, nil
}

/*
ABitOfEverythingServiceApiService Create a new ABitOfEverything
This API creates a new ABitOfEverything
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 ExamplepbCheckStatusResponse struct {
Status *RpcStatus `json:"status,omitempty"`
}
4 changes: 4 additions & 0 deletions examples/internal/clients/abe/model_rpc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

package abe

// - Simple to use and understand for most users - Flexible enough to meet unexpected needs # Overview The `Status` message contains three pieces of data: error code, error message, and error details. The error code should be an enum value of [google.rpc.Code][google.rpc.Code], but it may accept additional error codes if needed. The error message should be a developer-facing English message that helps developers *understand* and *resolve* the error. If a localized user-facing error message is needed, put the localized message in the error details or localize it in the client. The optional error details may contain arbitrary information about the error. There is a predefined set of error detail types in the package `google.rpc` that can be used for common error conditions. # Language mapping The `Status` message is the logical representation of the error model, but it is not necessarily the actual wire format. When the `Status` message is exposed in different client libraries and different wire protocols, it can be mapped differently. For example, it will likely be mapped to some exceptions in Java, but more likely mapped to some error codes in C. # Other uses The error model and the `Status` message can be used in a variety of environments, either with or without APIs, to provide a consistent developer experience across different environments. Example uses of this error model include: - Partial errors. If a service needs to return partial errors to the client, it may embed the `Status` in the normal response to indicate the partial errors. - Workflow errors. A typical workflow has multiple steps. Each step may have a `Status` message for error reporting. - Batch operations. If a client uses batch request and batch response, the `Status` message should be used directly inside batch response, one for each error sub-response. - Asynchronous operations. If an API call embeds asynchronous operation results in its response, the status of those operations should be represented directly using the `Status` message. - Logging. If some API errors are stored in logs, the message `Status` could be used directly after any stripping needed for security/privacy reasons.
type RpcStatus struct {
// The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code].
Code int32 `json:"code,omitempty"`
// A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client.
Message string `json:"message,omitempty"`
// A list of messages that carry the error details. There is a common set of message types for APIs to use.
Details []ProtobufAny `json:"details,omitempty"`
}
39 changes: 39 additions & 0 deletions examples/internal/clients/echo/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,48 @@ definitions:
code:
type: "integer"
format: "int32"
description: "The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]."
message:
type: "string"
description: "A developer-facing error message, which should be in English.\
\ Any\nuser-facing error message should be localized and sent in the\n[google.rpc.Status.details][google.rpc.Status.details]\
\ field, or localized by the client."
details:
type: "array"
description: "A list of messages that carry the error details. There is a\
\ common set of\nmessage types for APIs to use."
items:
$ref: "#/definitions/protobufAny"
title: "The `Status` type defines a logical error model that is suitable for different\n\
programming environments, including REST APIs and RPC APIs. It is used by\n\
[gRPC](https://github.com/grpc). The error model is designed to be:"
description: "- Simple to use and understand for most users\n- Flexible enough\
\ to meet unexpected needs\n\n# Overview\n\nThe `Status` message contains three\
\ pieces of data: error code, error message,\nand error details. The error code\
\ should be an enum value of\n[google.rpc.Code][google.rpc.Code], but it may\
\ accept additional error codes if needed. The\nerror message should be a developer-facing\
\ English message that helps\ndevelopers *understand* and *resolve* the error.\
\ If a localized user-facing\nerror message is needed, put the localized message\
\ in the error details or\nlocalize it in the client. The optional error details\
\ may contain arbitrary\ninformation about the error. There is a predefined\
\ set of error detail types\nin the package `google.rpc` that can be used for\
\ common error conditions.\n\n# Language mapping\n\nThe `Status` message is\
\ the logical representation of the error model, but it\nis not necessarily\
\ the actual wire format. When the `Status` message is\nexposed in different\
\ client libraries and different wire protocols, it can be\nmapped differently.\
\ For example, it will likely be mapped to some exceptions\nin Java, but more\
\ likely mapped to some error codes in C.\n\n# Other uses\n\nThe error model\
\ and the `Status` message can be used in a variety of\nenvironments, either\
\ with or without APIs, to provide a\nconsistent developer experience across\
\ different environments.\n\nExample uses of this error model include:\n\n-\
\ Partial errors. If a service needs to return partial errors to the client,\n\
\ it may embed the `Status` in the normal response to indicate the partial\n\
\ errors.\n\n- Workflow errors. A typical workflow has multiple steps. Each\
\ step may\n have a `Status` message for error reporting.\n\n- Batch operations.\
\ If a client uses batch request and batch response, the\n `Status` message\
\ should be used directly inside batch response, one for\n each error sub-response.\n\
\n- Asynchronous operations. If an API call embeds asynchronous operation\n\
\ results in its response, the status of those operations should be\n \
\ represented directly using the `Status` message.\n\n- Logging. If some API\
\ errors are stored in logs, the message `Status` could\n be used directly\
\ after any stripping needed for security/privacy reasons."
Loading

0 comments on commit 80a7a7b

Please sign in to comment.