Skip to content

Commit

Permalink
fix: support for imported oneof fields in request bodies (#2867)
Browse files Browse the repository at this point in the history
* fix: support for oneof fields in request bodies

* chore: run bazelisk

* chore: update buf.yaml

* fix: bazelisk
  • Loading branch information
isbang authored Aug 31, 2022
1 parent 3a111d3 commit 7bc2511
Show file tree
Hide file tree
Showing 20 changed files with 1,784 additions and 1,011 deletions.
2 changes: 2 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ lint:
- examples/internal/proto/examplepb/visibility_rule_echo_service.proto
- examples/internal/proto/examplepb/use_go_template.proto
- examples/internal/proto/examplepb/wrappers.proto
- examples/internal/proto/oneofenum/oneof_enum.proto
- examples/internal/proto/pathenum/path_enum.proto
- examples/internal/proto/sub/message.proto
- examples/internal/proto/sub2/message.proto
Expand Down Expand Up @@ -104,6 +105,7 @@ lint:
- examples/internal/proto/examplepb/visibility_rule_echo_service.proto
- examples/internal/proto/examplepb/use_go_template.proto
- examples/internal/proto/examplepb/wrappers.proto
- examples/internal/proto/oneofenum/oneof_enum.proto
- examples/internal/proto/pathenum/path_enum.proto
- examples/internal/proto/sub/message.proto
- examples/internal/proto/sub2/message.proto
Expand Down
1 change: 1 addition & 0 deletions examples/internal/clients/abe/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ go_library(
"model_examplepbsnake_case_enum.go",
"model_message_path_enum_nested_path_enum.go",
"model_nested_deep_enum.go",
"model_oneofenum_example_enum.go",
"model_pathenum_path_enum.go",
"model_pathenumsnake_case_for_import.go",
"model_protobuf_any.go",
Expand Down
44 changes: 44 additions & 0 deletions examples/internal/clients/abe/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,44 @@ paths:
description: "An unexpected error response."
schema:
$ref: "#/definitions/rpcStatus"
/v1/example/oneofenum:
post:
tags:
- "ABitOfEverythingService"
operationId: "ABitOfEverythingService_PostOneofEnum"
parameters:
- in: "body"
name: "exampleEnum"
required: true
schema:
$ref: "#/definitions/oneofenumExampleEnum"
x-exportParamName: "ExampleEnum"
responses:
200:
description: "A successful response."
schema:
type: "object"
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"
500:
description: "Server error"
schema:
$ref: "#/definitions/examplepbErrorResponse"
default:
description: "An unexpected error response."
schema:
$ref: "#/definitions/rpcStatus"
/v1/example/snake/{who}/{what}/{where}:
get:
tags:
Expand Down Expand Up @@ -4744,6 +4782,12 @@ definitions:
- "value_c"
- "value_d"
default: "value_c"
oneofenumExampleEnum:
type: "string"
enum:
- "EXAMPLE_ENUM_UNSPECIFIED"
- "EXAMPLE_ENUM_FIRST"
default: "EXAMPLE_ENUM_UNSPECIFIED"
pathenumPathEnum:
type: "string"
enum:
Expand Down
157 changes: 157 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 @@ -4899,6 +4899,163 @@ func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceOverwriteResp
return localVarReturnValue, localVarHttpResponse, nil
}

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

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

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
}
// body params
localVarPostBody = &exampleEnum
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 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 == 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 == 500 {
var v ExamplepbErrorResponse
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
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
Expand Down
19 changes: 19 additions & 0 deletions examples/internal/clients/abe/model_oneofenum_example_enum.go
Original file line number Diff line number Diff line change
@@ -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

type OneofenumExampleEnum string

// List of oneofenumExampleEnum
const (
UNSPECIFIED_OneofenumExampleEnum OneofenumExampleEnum = "EXAMPLE_ENUM_UNSPECIFIED"
FIRST_OneofenumExampleEnum OneofenumExampleEnum = "EXAMPLE_ENUM_FIRST"
)
2 changes: 2 additions & 0 deletions examples/internal/proto/examplepb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ proto_library(
"wrappers.proto",
],
deps = [
"//examples/internal/proto/oneofenum:oneofenum_proto",
"//examples/internal/proto/pathenum:pathenum_proto",
"//examples/internal/proto/sub:sub_proto",
"//examples/internal/proto/sub2:sub2_proto",
Expand Down Expand Up @@ -98,6 +99,7 @@ go_proto_library(
importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb",
proto = ":examplepb_proto",
deps = [
"//examples/internal/proto/oneofenum",
"//examples/internal/proto/pathenum",
"//examples/internal/proto/sub",
"//examples/internal/proto/sub2",
Expand Down
Loading

0 comments on commit 7bc2511

Please sign in to comment.