Skip to content

Commit

Permalink
Backing out 'swagger extras' JSON code, per [request][1].
Browse files Browse the repository at this point in the history
  • Loading branch information
ivucica committed May 7, 2016
1 parent f45fe56 commit 562956f
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 133 deletions.
46 changes: 0 additions & 46 deletions examples/examplepb/echo_service.pb.go

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

37 changes: 0 additions & 37 deletions examples/examplepb/echo_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,11 @@ option go_package = "examplepb";
//
// Echo Service API consists of a single service which returns
// a message.
//
// <!-- swagger extras start
// {
// "info": {
// "title": "Echo Service",
// "version": "1.0",
// "contact": {
// "name": "gRPC-Gateway project",
// "url": "https://github.com/gengo/grpc-gateway",
// "email": "none@example.com"
// }
// },
// "host": "localhost",
// "externalDocs": {
// "url": "https://github.com/gengo/grpc-gateway",
// "description": "More about gRPC-Gateway"
// }
// }
// swagger extras end -->
package gengo.grpc.gateway.examples.examplepb;

import "google/api/annotations.proto";

// SimpleMessage represents a simple message sent to the Echo service.
//
// <!-- swagger extras start
// {
// "externalDocs": {
// "url": "http://github.com/gengo/grpc-gateway",
// "description": "Find out more about EchoService"
// }
// }
// swagger extras end -->
message SimpleMessage {
// Id represents the message identifier.
string id = 1;
Expand All @@ -49,15 +21,6 @@ service EchoService {
//
// The message posted as the id parameter will also be
// returned.
//
// <!-- swagger extras start
// {
// "externalDocs": {
// "url": "http://github.com/gengo/grpc-gateway",
// "description": "Find out more about EchoService"
// }
// }
// swagger extras end -->
rpc Echo(SimpleMessage) returns (SimpleMessage) {
option (google.api.http) = {
post: "/v1/example/echo/{id}"
Expand Down
14 changes: 2 additions & 12 deletions examples/examplepb/echo_service.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
"info": {
"title": "Echo Service",
"description": "Echo Service API consists of a single service which returns\na message.",
"version": "1.0",
"contact": {
"name": "gRPC-Gateway project",
"url": "https://github.com/gengo/grpc-gateway",
"email": "none@example.com"
}
"version": "version not set"
},
"host": "localhost",
"schemes": [
"http",
"https"
Expand Down Expand Up @@ -46,11 +40,7 @@
],
"tags": [
"EchoService"
],
"externalDocs": {
"description": "Find out more about EchoService",
"url": "http://github.com/gengo/grpc-gateway"
}
]
}
},
"/v1/example/echo_body": {
Expand Down
39 changes: 1 addition & 38 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
pbdescriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
)

var swaggerExtrasRegexp = regexp.MustCompile(`(?s)^(.*[^\s])[\s]*<!-- swagger extras start(.*)swagger extras end -->[\s]*(.*)$`)

// findServicesMessagesAndEnumerations discovers all messages and enums defined in the RPC methods of the service.
func findServicesMessagesAndEnumerations(s []*descriptor.Service, reg *descriptor.Registry, m messageMap, e enumMap) {
for _, svc := range s {
Expand Down Expand Up @@ -541,55 +539,20 @@ func applyTemplate(p param) (string, error) {
// updateSwaggerDataFromComments updates a Swagger object based on a comment
// from the proto file.
//
// As a first step, a section matching:
//
// <!-- swagger extras start.*swagger extras end-->
//
// where .* contains valid JSON will be stored for later processing, and then
// removed from the passed string.
// (Implementation note: Currently, the JSON gets immediately applied and
// thus cannot override summary and description.)
//
// First paragraph of a comment is used for summary. Remaining paragraphs of a
// comment are used for description. If 'Summary' field is not present on the
// passed swaggerObject, the summary and description are joined by \n\n.
//
// If there is a field named 'Info', its 'Summary' and 'Description' fields
// will be updated instead. (JSON always gets applied directly to the passed
// object.)
// will be updated instead.
//
// If there is no 'Summary', the same behavior will be attempted on 'Title',
// but only if the last character is not a period.
//
// To apply additional Swagger properties, one can pass valid JSON as described
// before. This JSON gets parsed and applied to the passed swaggerObject
// directly. This lets users easily apply custom properties such as contact
// details, API base path, et al.
func updateSwaggerDataFromComments(swaggerObject interface{}, comment string) error {
if len(comment) == 0 {
return nil
}

// Find a section containing additional Swagger metadata.
matches := swaggerExtrasRegexp.FindStringSubmatch(comment)

if len(matches) > 0 {
// If found, before further processing, replace the
// comment with a version that does not contain the
// extras.
comment = matches[1]
if len(matches[3]) > 0 {
comment += "\n\n" + matches[3]
}

// Parse the JSON and apply it.
// TODO(ivucica): apply extras /after/ applying summary
// and description.
if err := json.Unmarshal([]byte(matches[2]), swaggerObject); err != nil {
return fmt.Errorf("error: %s, parsing: %s", err.Error(), matches[2])
}
}

// Figure out what to apply changes to.
swaggerObjectValue := reflect.ValueOf(swaggerObject)
infoObjectValue := swaggerObjectValue.Elem().FieldByName("Info")
Expand Down

0 comments on commit 562956f

Please sign in to comment.