Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1117: Support response examples. #1124

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2189,8 +2198,6 @@ definitions:
properties:
value:
type: "string"
example:
value: "value"
johanbrandhorst marked this conversation as resolved.
Show resolved Hide resolved
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.
johanbrandhorst marked this conversation as resolved.
Show resolved Hide resolved
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