Skip to content

Commit

Permalink
protoc-gen-swagger: support all well-known wrapper types
Browse files Browse the repository at this point in the history
There were a few well-known wrapper types missing from
the wkSchemas map. In specific UInt32Value, UInt64Value
and BytesValue. This change handles them (and maps them to
the same swagger types as the non-wrapped values)

This also fixes the mapping of Int64Value. The Int64Value handling
maps the value to a swagger integer. The documentation for
Int64Value indicates that it should be mapped to a JSON
string (also the mapping for normal int64 in protoc-gen-swagger
maps it to a string, so this was inconsistent.)
  • Loading branch information
jriecken authored and johanbrandhorst committed Jul 24, 2018
1 parent d724c4f commit 209d7ce
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 29 deletions.
81 changes: 54 additions & 27 deletions examples/proto/examplepb/wrappers.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/proto/examplepb/wrappers.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ message Wrappers {
google.protobuf.FloatValue float_value = 4;
google.protobuf.DoubleValue double_value = 5;
google.protobuf.BoolValue bool_value = 6;
google.protobuf.UInt32Value uint32_value = 7;
google.protobuf.UInt64Value uint64_value = 8;
google.protobuf.BytesValue bytes_value = 9;
}

service WrappersService {
Expand Down
14 changes: 13 additions & 1 deletion examples/proto/examplepb/wrappers.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"format": "int32"
},
"int64_value": {
"type": "integer",
"type": "string",
"format": "int64"
},
"float_value": {
Expand All @@ -68,6 +68,18 @@
"bool_value": {
"type": "boolean",
"format": "boolean"
},
"uint32_value": {
"type": "integer",
"format": "int64"
},
"uint64_value": {
"type": "string",
"format": "uint64"
},
"bytes_value": {
"type": "string",
"format": "byte"
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@ var wktSchemas = map[string]schemaCore{
".google.protobuf.StringValue": schemaCore{
Type: "string",
},
".google.protobuf.BytesValue": schemaCore{
Type: "string",
Format: "byte",
},
".google.protobuf.Int32Value": schemaCore{
Type: "integer",
Format: "int32",
},
".google.protobuf.Int64Value": schemaCore{
".google.protobuf.UInt32Value": schemaCore{
Type: "integer",
Format: "int64",
},
".google.protobuf.Int64Value": schemaCore{
Type: "string",
Format: "int64",
},
".google.protobuf.UInt64Value": schemaCore{
Type: "string",
Format: "uint64",
},
".google.protobuf.FloatValue": schemaCore{
Type: "number",
Format: "float",
Expand Down Expand Up @@ -201,10 +213,16 @@ func renderMessagesAsDefinition(messages messageMap, d swaggerDefinitionsObject,
continue
case ".google.protobuf.StringValue":
continue
case ".google.protobuf.BytesValue":
continue
case ".google.protobuf.Int32Value":
continue
case ".google.protobuf.UInt32Value":
continue
case ".google.protobuf.Int64Value":
continue
case ".google.protobuf.UInt64Value":
continue
case ".google.protobuf.FloatValue":
continue
case ".google.protobuf.DoubleValue":
Expand Down
112 changes: 112 additions & 0 deletions protoc-gen-swagger/genswagger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,118 @@ func TestSchemaOfField(t *testing.T) {
},
},
},
{
field: &descriptor.Field{
FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{
Name: proto.String("wrapped_field"),
TypeName: proto.String(".google.protobuf.BytesValue"),
Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
},
},
refs: make(refMap),
expected: schemaCore{
Type: "string",
Format: "bytes",
},
},
{
field: &descriptor.Field{
FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{
Name: proto.String("wrapped_field"),
TypeName: proto.String(".google.protobuf.Int32Value"),
Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
},
},
refs: make(refMap),
expected: schemaCore{
Type: "integer",
Format: "int32",
},
},
{
field: &descriptor.Field{
FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{
Name: proto.String("wrapped_field"),
TypeName: proto.String(".google.protobuf.UInt32Value"),
Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
},
},
refs: make(refMap),
expected: schemaCore{
Type: "integer",
Format: "int64",
},
},
{
field: &descriptor.Field{
FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{
Name: proto.String("wrapped_field"),
TypeName: proto.String(".google.protobuf.Int64Value"),
Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
},
},
refs: make(refMap),
expected: schemaCore{
Type: "string",
Format: "int64",
},
},
{
field: &descriptor.Field{
FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{
Name: proto.String("wrapped_field"),
TypeName: proto.String(".google.protobuf.UInt64Value"),
Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
},
},
refs: make(refMap),
expected: schemaCore{
Type: "string",
Format: "uint64",
},
},
{
field: &descriptor.Field{
FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{
Name: proto.String("wrapped_field"),
TypeName: proto.String(".google.protobuf.FloatValue"),
Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
},
},
refs: make(refMap),
expected: schemaCore{
Type: "number",
Format: "float",
},
},
{
field: &descriptor.Field{
FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{
Name: proto.String("wrapped_field"),
TypeName: proto.String(".google.protobuf.DoubleValue"),
Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
},
},
refs: make(refMap),
expected: schemaCore{
Type: "number",
Format: "double",
},
},
{
field: &descriptor.Field{
FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{
Name: proto.String("wrapped_field"),
TypeName: proto.String(".google.protobuf.BoolValue"),
Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
},
},
refs: make(refMap),
expected: schemaCore{
Type: "boolean",
Format: "boolean",
},
},
{
field: &descriptor.Field{
FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{
Expand Down

0 comments on commit 209d7ce

Please sign in to comment.