Skip to content

Commit

Permalink
Fix to swagger generator to ensure all definitions are output for nes…
Browse files Browse the repository at this point in the history
…ted structs. (grpc-ecosystem#505)
  • Loading branch information
warmans authored and achew22 committed Dec 23, 2017
1 parent 6a715f6 commit cccb822
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 102 deletions.
64 changes: 64 additions & 0 deletions examples/clients/abe/a_bit_of_everything_service_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,70 @@ func (a ABitOfEverythingServiceApi) Echo3(value string) (*SubStringMessage, *API
return successPayload, localVarAPIResponse, err
}

/**
*
*
* @param id
* @param body
* @return *ProtobufEmpty
*/
func (a ABitOfEverythingServiceApi) GetMessageWithBody(id string, body ExamplepbBody) (*ProtobufEmpty, *APIResponse, error) {

var localVarHttpMethod = strings.ToUpper("Post")
// create path and map variables
localVarPath := a.Configuration.BasePath + "/v2/example/withbody/{id}"
localVarPath = strings.Replace(localVarPath, "{"+"id"+"}", fmt.Sprintf("%v", id), -1)

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := make(map[string]string)
var localVarPostBody interface{}
var localVarFileName string
var localVarFileBytes []byte
// add default headers if any
for key := range a.Configuration.DefaultHeader {
localVarHeaderParams[key] = a.Configuration.DefaultHeader[key]
}

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

// set Content-Type header
localVarHttpContentType := a.Configuration.APIClient.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 := a.Configuration.APIClient.SelectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
// body params
localVarPostBody = &body
var successPayload = new(ProtobufEmpty)
localVarHttpResponse, err := a.Configuration.APIClient.CallAPI(localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)

var localVarURL, _ = url.Parse(localVarPath)
localVarURL.RawQuery = localVarQueryParams.Encode()
var localVarAPIResponse = &APIResponse{Operation: "GetMessageWithBody", Method: localVarHttpMethod, RequestURL: localVarURL.String()}
if localVarHttpResponse != nil {
localVarAPIResponse.Response = localVarHttpResponse.RawResponse
localVarAPIResponse.Payload = localVarHttpResponse.Body()
}

if err != nil {
return successPayload, localVarAPIResponse, err
}
err = json.Unmarshal(localVarHttpResponse.Body(), &successPayload)
return successPayload, localVarAPIResponse, err
}

/**
*
*
Expand Down
16 changes: 16 additions & 0 deletions examples/clients/abe/examplepb_body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* A Bit of Everything
*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: 1.0
* Contact: [email protected]
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/

package abe

type ExamplepbBody struct {

Name string `json:"name,omitempty"`
}
282 changes: 181 additions & 101 deletions examples/examplepb/a_bit_of_everything.pb.go

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions examples/examplepb/a_bit_of_everything.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions examples/examplepb/a_bit_of_everything.proto
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ message ABitOfEverything {
repeated NumericEnum repeated_enum_value = 28;
}

message Body {
string name = 1;
}

message MessageWithBody {
string id = 1;
Body data = 2;
}


// NumericEnum is one or zero.
enum NumericEnum {
// ZERO means 0
Expand Down Expand Up @@ -194,6 +204,12 @@ service ABitOfEverythingService {
get: "/v2/example/timeout",
};
}
rpc GetMessageWithBody(MessageWithBody) returns (google.protobuf.Empty) {
option (google.api.http) = {
post: "/v2/example/withbody/{id}",
body: "data"
};
}
}

service AnotherServiceWithNoBindings {
Expand Down
40 changes: 40 additions & 0 deletions examples/examplepb/a_bit_of_everything.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,38 @@
"ABitOfEverythingService"
]
}
},
"/v2/example/withbody/{id}": {
"post": {
"operationId": "GetMessageWithBody",
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/protobufEmpty"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/examplepbBody"
}
}
],
"tags": [
"ABitOfEverythingService"
]
}
}
},
"definitions": {
Expand Down Expand Up @@ -744,6 +776,14 @@
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
}
},
"examplepbBody": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"examplepbNumericEnum": {
"type": "string",
"enum": [
Expand Down
2 changes: 2 additions & 0 deletions examples/examplepb/echo_service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions examples/server/a_bit_of_everything.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

)

// Implements of ABitOfEverythingServiceServer
Expand Down Expand Up @@ -246,3 +247,7 @@ func (s *_ABitOfEverythingServer) Timeout(ctx context.Context, msg *empty.Empty)
return nil, ctx.Err()
}
}

func (s *_ABitOfEverythingServer) GetMessageWithBody(ctx context.Context, msg *examples.MessageWithBody) (*empty.Empty, error) {
return &empty.Empty{}, nil
}
3 changes: 2 additions & 1 deletion protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ func findServicesMessagesAndEnumerations(s []*descriptor.Service, reg *descripto
// Request may be fully included in query
if _, ok := refs[fmt.Sprintf("#/definitions/%s", fullyQualifiedNameToSwaggerName(meth.RequestType.FQMN(), reg))]; ok {
m[fullyQualifiedNameToSwaggerName(meth.RequestType.FQMN(), reg)] = meth.RequestType
findNestedMessagesAndEnumerations(meth.RequestType, reg, m, e)
}
findNestedMessagesAndEnumerations(meth.RequestType, reg, m, e)

m[fullyQualifiedNameToSwaggerName(meth.ResponseType.FQMN(), reg)] = meth.ResponseType
findNestedMessagesAndEnumerations(meth.ResponseType, reg, m, e)
}
Expand Down

0 comments on commit cccb822

Please sign in to comment.