Skip to content

Commit 48e4bd9

Browse files
warmansachew22
authored andcommitted
Fix to swagger generator to ensure all definitions are output for nested structs. (#505)
1 parent 8879672 commit 48e4bd9

File tree

9 files changed

+390
-102
lines changed

9 files changed

+390
-102
lines changed

examples/clients/abe/a_bit_of_everything_service_api.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,70 @@ func (a ABitOfEverythingServiceApi) Echo3(value string) (*SubStringMessage, *API
501501
return successPayload, localVarAPIResponse, err
502502
}
503503

504+
/**
505+
*
506+
*
507+
* @param id
508+
* @param body
509+
* @return *ProtobufEmpty
510+
*/
511+
func (a ABitOfEverythingServiceApi) GetMessageWithBody(id string, body ExamplepbBody) (*ProtobufEmpty, *APIResponse, error) {
512+
513+
var localVarHttpMethod = strings.ToUpper("Post")
514+
// create path and map variables
515+
localVarPath := a.Configuration.BasePath + "/v2/example/withbody/{id}"
516+
localVarPath = strings.Replace(localVarPath, "{"+"id"+"}", fmt.Sprintf("%v", id), -1)
517+
518+
localVarHeaderParams := make(map[string]string)
519+
localVarQueryParams := url.Values{}
520+
localVarFormParams := make(map[string]string)
521+
var localVarPostBody interface{}
522+
var localVarFileName string
523+
var localVarFileBytes []byte
524+
// add default headers if any
525+
for key := range a.Configuration.DefaultHeader {
526+
localVarHeaderParams[key] = a.Configuration.DefaultHeader[key]
527+
}
528+
529+
// to determine the Content-Type header
530+
localVarHttpContentTypes := []string{ "application/json", "application/x-foo-mime", }
531+
532+
// set Content-Type header
533+
localVarHttpContentType := a.Configuration.APIClient.SelectHeaderContentType(localVarHttpContentTypes)
534+
if localVarHttpContentType != "" {
535+
localVarHeaderParams["Content-Type"] = localVarHttpContentType
536+
}
537+
// to determine the Accept header
538+
localVarHttpHeaderAccepts := []string{
539+
"application/json",
540+
"application/x-foo-mime",
541+
}
542+
543+
// set Accept header
544+
localVarHttpHeaderAccept := a.Configuration.APIClient.SelectHeaderAccept(localVarHttpHeaderAccepts)
545+
if localVarHttpHeaderAccept != "" {
546+
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
547+
}
548+
// body params
549+
localVarPostBody = &body
550+
var successPayload = new(ProtobufEmpty)
551+
localVarHttpResponse, err := a.Configuration.APIClient.CallAPI(localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
552+
553+
var localVarURL, _ = url.Parse(localVarPath)
554+
localVarURL.RawQuery = localVarQueryParams.Encode()
555+
var localVarAPIResponse = &APIResponse{Operation: "GetMessageWithBody", Method: localVarHttpMethod, RequestURL: localVarURL.String()}
556+
if localVarHttpResponse != nil {
557+
localVarAPIResponse.Response = localVarHttpResponse.RawResponse
558+
localVarAPIResponse.Payload = localVarHttpResponse.Body()
559+
}
560+
561+
if err != nil {
562+
return successPayload, localVarAPIResponse, err
563+
}
564+
err = json.Unmarshal(localVarHttpResponse.Body(), &successPayload)
565+
return successPayload, localVarAPIResponse, err
566+
}
567+
504568
/**
505569
*
506570
*
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* A Bit of Everything
3+
*
4+
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
5+
*
6+
* OpenAPI spec version: 1.0
7+
* Contact: [email protected]
8+
* Generated by: https://github.com/swagger-api/swagger-codegen.git
9+
*/
10+
11+
package abe
12+
13+
type ExamplepbBody struct {
14+
15+
Name string `json:"name,omitempty"`
16+
}

examples/examplepb/a_bit_of_everything.pb.go

Lines changed: 181 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/examplepb/a_bit_of_everything.pb.gw.go

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/examplepb/a_bit_of_everything.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ message ABitOfEverything {
9797
repeated NumericEnum repeated_enum_value = 28;
9898
}
9999

100+
message Body {
101+
string name = 1;
102+
}
103+
104+
message MessageWithBody {
105+
string id = 1;
106+
Body data = 2;
107+
}
108+
109+
100110
// NumericEnum is one or zero.
101111
enum NumericEnum {
102112
// ZERO means 0
@@ -194,6 +204,12 @@ service ABitOfEverythingService {
194204
get: "/v2/example/timeout",
195205
};
196206
}
207+
rpc GetMessageWithBody(MessageWithBody) returns (google.protobuf.Empty) {
208+
option (google.api.http) = {
209+
post: "/v2/example/withbody/{id}",
210+
body: "data"
211+
};
212+
}
197213
}
198214

199215
service AnotherServiceWithNoBindings {

examples/examplepb/a_bit_of_everything.swagger.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,38 @@
590590
"ABitOfEverythingService"
591591
]
592592
}
593+
},
594+
"/v2/example/withbody/{id}": {
595+
"post": {
596+
"operationId": "GetMessageWithBody",
597+
"responses": {
598+
"200": {
599+
"description": "",
600+
"schema": {
601+
"$ref": "#/definitions/protobufEmpty"
602+
}
603+
}
604+
},
605+
"parameters": [
606+
{
607+
"name": "id",
608+
"in": "path",
609+
"required": true,
610+
"type": "string"
611+
},
612+
{
613+
"name": "body",
614+
"in": "body",
615+
"required": true,
616+
"schema": {
617+
"$ref": "#/definitions/examplepbBody"
618+
}
619+
}
620+
],
621+
"tags": [
622+
"ABitOfEverythingService"
623+
]
624+
}
593625
}
594626
},
595627
"definitions": {
@@ -744,6 +776,14 @@
744776
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
745777
}
746778
},
779+
"examplepbBody": {
780+
"type": "object",
781+
"properties": {
782+
"name": {
783+
"type": "string"
784+
}
785+
}
786+
},
747787
"examplepbNumericEnum": {
748788
"type": "string",
749789
"enum": [

examples/examplepb/echo_service.pb.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/server/a_bit_of_everything.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"google.golang.org/grpc/codes"
1818
"google.golang.org/grpc/metadata"
1919
"google.golang.org/grpc/status"
20+
2021
)
2122

2223
// Implements of ABitOfEverythingServiceServer
@@ -246,3 +247,7 @@ func (s *_ABitOfEverythingServer) Timeout(ctx context.Context, msg *empty.Empty)
246247
return nil, ctx.Err()
247248
}
248249
}
250+
251+
func (s *_ABitOfEverythingServer) GetMessageWithBody(ctx context.Context, msg *examples.MessageWithBody) (*empty.Empty, error) {
252+
return &empty.Empty{}, nil
253+
}

protoc-gen-swagger/genswagger/template.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ func findServicesMessagesAndEnumerations(s []*descriptor.Service, reg *descripto
138138
// Request may be fully included in query
139139
if _, ok := refs[fmt.Sprintf("#/definitions/%s", fullyQualifiedNameToSwaggerName(meth.RequestType.FQMN(), reg))]; ok {
140140
m[fullyQualifiedNameToSwaggerName(meth.RequestType.FQMN(), reg)] = meth.RequestType
141-
findNestedMessagesAndEnumerations(meth.RequestType, reg, m, e)
142141
}
142+
findNestedMessagesAndEnumerations(meth.RequestType, reg, m, e)
143+
143144
m[fullyQualifiedNameToSwaggerName(meth.ResponseType.FQMN(), reg)] = meth.ResponseType
144145
findNestedMessagesAndEnumerations(meth.ResponseType, reg, m, e)
145146
}

0 commit comments

Comments
 (0)