Skip to content

Commit

Permalink
Merge pull request grpc-ecosystem#209 from YotpoLtd/improve-template-…
Browse files Browse the repository at this point in the history
…supported-types

improve(genswagger:template):added support for google.protobuf.Timestamp
  • Loading branch information
achew22 authored Aug 24, 2016
2 parents 8d72fd8 + c50ea33 commit 45e6bd8
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 75 deletions.
2 changes: 2 additions & 0 deletions examples/clients/abe/ExamplepbABitOfEverything.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package abe

import (
"time"
)

type ExamplepbABitOfEverything struct {
Expand All @@ -26,6 +27,7 @@ type ExamplepbABitOfEverything struct {
Sint32Value int32 `json:"sint32_value,omitempty"`
Sint64Value string `json:"sint64_value,omitempty"`
StringValue string `json:"string_value,omitempty"`
TimestampValue time.Time `json:"timestamp_value,omitempty"`
Uint32Value int64 `json:"uint32_value,omitempty"`
Uint64Value string `json:"uint64_value,omitempty"`
Uuid string `json:"uuid,omitempty"`
Expand Down
155 changes: 83 additions & 72 deletions examples/examplepb/a_bit_of_everything.pb.go

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

3 changes: 3 additions & 0 deletions examples/examplepb/a_bit_of_everything.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "google/api/annotations.proto";
import "google/protobuf/empty.proto";
import "examples/sub/message.proto";
import "examples/sub2/message.proto";
import "google/protobuf/timestamp.proto";

// Intentionaly complicated message type to cover much features of Protobuf.
// NEXT ID: 27
Expand Down Expand Up @@ -55,6 +56,8 @@ message ABitOfEverything {
map<string, Nested> mapped_nested_value = 24;

string nonConventionalNameValue = 26;

google.protobuf.Timestamp timestamp_value = 27;
}

// NumericEnum is one or zero.
Expand Down
4 changes: 4 additions & 0 deletions examples/examplepb/a_bit_of_everything.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@
"type": "string",
"format": "string"
},
"timestamp_value": {
"type": "string",
"format": "date-time"
},
"uint32_value": {
"type": "integer",
"format": "int64",
Expand Down
17 changes: 14 additions & 3 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func findNestedMessagesAndEnumerations(message *descriptor.Message, reg *descrip
}

func renderMessagesAsDefinition(messages messageMap, d swaggerDefinitionsObject, reg *descriptor.Registry) {
for _, msg := range messages {
for name, msg := range messages {
switch name {
case ".google.protobuf.Timestamp":
continue
}
if opt := msg.GetOptions(); opt != nil && opt.MapEntry != nil && *opt.MapEntry {
continue
}
Expand Down Expand Up @@ -105,8 +109,15 @@ func schemaOfField(f *descriptor.Field, reg *descriptor.Registry) swaggerSchemaO

switch ft := fd.GetType(); ft {
case pbdescriptor.FieldDescriptorProto_TYPE_ENUM, pbdescriptor.FieldDescriptorProto_TYPE_MESSAGE, pbdescriptor.FieldDescriptorProto_TYPE_GROUP:
core = schemaCore{
Ref: "#/definitions/" + fullyQualifiedNameToSwaggerName(fd.GetTypeName(), reg),
if fd.GetTypeName() == ".google.protobuf.Timestamp" && pbdescriptor.FieldDescriptorProto_TYPE_MESSAGE == ft {
core = schemaCore{
Type: "string",
Format: "date-time",
}
} else {
core = schemaCore{
Ref: "#/definitions/" + fullyQualifiedNameToSwaggerName(fd.GetTypeName(), reg),
}
}
default:
ftype, format, ok := primitiveSchema(ft)
Expand Down

0 comments on commit 45e6bd8

Please sign in to comment.