Skip to content

Commit

Permalink
Fix grpc-ecosystem#1117: Support response examples.
Browse files Browse the repository at this point in the history
Add support for examples in operation annotation responses. Merge
custom response data into the default 200 response.
  • Loading branch information
jgiles committed Jan 22, 2020
1 parent 4cb25e1 commit a086fed
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 351 deletions.
11 changes: 9 additions & 2 deletions examples/clients/abe/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ paths:
responses:
200:
description: "A successful response."
examples:
application/json:
value: "the input value"
schema:
$ref: "#/definitions/subStringMessage"
403:
Expand Down Expand Up @@ -1560,6 +1563,9 @@ paths:
responses:
200:
description: "A successful response."
examples:
application/json:
value: "the input value"
schema:
$ref: "#/definitions/subStringMessage"
403:
Expand Down Expand Up @@ -1598,6 +1604,9 @@ paths:
responses:
200:
description: "A successful response."
examples:
application/json:
value: "the input value"
schema:
$ref: "#/definitions/subStringMessage"
403:
Expand Down Expand Up @@ -2191,8 +2200,6 @@ definitions:
properties:
value:
type: "string"
example:
value: "value"
externalDocs:
description: "More about gRPC-Gateway"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
Expand Down
445 changes: 223 additions & 222 deletions examples/proto/examplepb/a_bit_of_everything.pb.go

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions examples/proto/examplepb/a_bit_of_everything.proto
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ service ABitOfEverythingService {
url: "https://github.com/grpc-ecosystem/grpc-gateway";
description: "Find out more Echo";
}
responses: {
key: "200"
value: {
examples: {
key: "application/json"
value: '{"value": "the input value"}'
}
}
}
responses: {
key: "503";
value: {
Expand Down
15 changes: 15 additions & 0 deletions examples/proto/examplepb/a_bit_of_everything.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/subStringMessage"
},
"examples": {
"application/json": {
"value": "the input value"
}
}
},
"403": {
Expand Down Expand Up @@ -1830,6 +1835,11 @@
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/subStringMessage"
},
"examples": {
"application/json": {
"value": "the input value"
}
}
},
"403": {
Expand Down Expand Up @@ -1880,6 +1890,11 @@
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/subStringMessage"
},
"examples": {
"application/json": {
"value": "the input value"
}
}
},
"403": {
Expand Down
32 changes: 29 additions & 3 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,9 +944,16 @@ func renderServices(services []*descriptor.Service, paths swaggerPathsObject, re
}
if opts.Responses != nil {
for name, resp := range opts.Responses {
respObj := swaggerResponseObject{
Description: resp.Description,
Schema: swaggerSchemaFromProtoSchema(resp.Schema, reg, customRefs, meth),
// Merge response data into default response if available.
respObj := operationObject.Responses[name]
if resp.Description != "" {
respObj.Description = resp.Description
}
if resp.Schema != nil {
respObj.Schema = swaggerSchemaFromProtoSchema(resp.Schema, reg, customRefs, meth)
}
if resp.Examples != nil {
respObj.Examples = swaggerExamplesFromProtoExamples(resp.Examples)
}
if resp.Extensions != nil {
exts, err := processExtensions(resp.Extensions)
Expand Down Expand Up @@ -1232,6 +1239,7 @@ func applyTemplate(p param) (*swaggerObject, error) {
respMap[k] = swaggerResponseObject{
Description: v.Description,
Schema: swaggerSchemaFromProtoSchema(v.Schema, p.reg, customRefs, nil),
Examples: swaggerExamplesFromProtoExamples(v.Examples),
}
}
}
Expand Down Expand Up @@ -1703,6 +1711,24 @@ func swaggerSchemaFromProtoSchema(s *swagger_options.Schema, reg *descriptor.Reg
return ret
}

func swaggerExamplesFromProtoExamples(in map[string]string) map[string]interface{} {
if len(in) == 0 {
return nil
}
out := make(map[string]interface{})
for mimeType, exampleStr := range in {
switch mimeType {
case "application/json":
// JSON example objects are rendered raw.
out[mimeType] = json.RawMessage(exampleStr)
default:
// All other mimetype examples are rendered as strings.
out[mimeType] = exampleStr
}
}
return out
}

func protoJSONSchemaTypeToFormat(in []swagger_options.JSONSchema_JSONSchemaSimpleTypes) (string, string) {
if len(in) == 0 {
return "", ""
Expand Down
5 changes: 3 additions & 2 deletions protoc-gen-swagger/genswagger/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ type swaggerResponsesObject map[string]swaggerResponseObject

// http://swagger.io/specification/#responseObject
type swaggerResponseObject struct {
Description string `json:"description"`
Schema swaggerSchemaObject `json:"schema"`
Description string `json:"description"`
Schema swaggerSchemaObject `json:"schema"`
Examples map[string]interface{} `json:"examples,omitempty"`

extensions []extension
}
Expand Down
Loading

0 comments on commit a086fed

Please sign in to comment.