diff --git a/protoc-gen-swagger/genswagger/template.go b/protoc-gen-swagger/genswagger/template.go index 3d67ee6973b..01746940e38 100644 --- a/protoc-gen-swagger/genswagger/template.go +++ b/protoc-gen-swagger/genswagger/template.go @@ -25,6 +25,29 @@ var wktSchemas = map[string]schemaCore{ ".google.protobuf.Duration": schemaCore{ Type: "string", }, + ".google.protobuf.StringValue": schemaCore{ + Type: "string", + }, + ".google.protobuf.Int32Value": schemaCore{ + Type: "integer", + Format: "int32", + }, + ".google.protobuf.Int64Value": schemaCore{ + Type: "integer", + Format: "int64", + }, + ".google.protobuf.FloatValue": schemaCore{ + Type: "number", + Format: "float", + }, + ".google.protobuf.DoubleValue": schemaCore{ + Type: "number", + Format: "double", + }, + ".google.protobuf.BoolValue": schemaCore{ + Type: "boolean", + Format: "boolean", + }, } func listEnumNames(enum *descriptor.Enum) (names []string) { diff --git a/protoc-gen-swagger/genswagger/template_test.go b/protoc-gen-swagger/genswagger/template_test.go index 4633a0bbac6..d24f978fbeb 100644 --- a/protoc-gen-swagger/genswagger/template_test.go +++ b/protoc-gen-swagger/genswagger/template_test.go @@ -787,3 +787,121 @@ func TestFQMNtoSwaggerName(t *testing.T) { } } } + +func TestSchemaOfField(t *testing.T) { + type test struct { + field *descriptor.Field + expected schemaCore + } + + tests := []test{ + { + field: &descriptor.Field{ + FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{ + Name: proto.String("primitive_field"), + Type: protodescriptor.FieldDescriptorProto_TYPE_STRING.Enum(), + }, + }, + expected: schemaCore{ + Type: "string", + }, + }, + { + field: &descriptor.Field{ + FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{ + Name: proto.String("repeated_primitive_field"), + Type: protodescriptor.FieldDescriptorProto_TYPE_STRING.Enum(), + Label: protodescriptor.FieldDescriptorProto_LABEL_REPEATED.Enum(), + }, + }, + expected: schemaCore{ + Type: "array", + Items: &swaggerItemsObject{ + Type: "string", + }, + }, + }, + { + field: &descriptor.Field{ + FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{ + Name: proto.String("wrapped_field"), + TypeName: proto.String(".google.protobuf.StringValue"), + Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(), + }, + }, + expected: schemaCore{ + Type: "string", + }, + }, + { + field: &descriptor.Field{ + FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{ + Name: proto.String("repeated_wrapped_field"), + TypeName: proto.String(".google.protobuf.StringValue"), + Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(), + Label: protodescriptor.FieldDescriptorProto_LABEL_REPEATED.Enum(), + }, + }, + expected: schemaCore{ + Type: "array", + Items: &swaggerItemsObject{ + Type: "string", + }, + }, + }, + { + field: &descriptor.Field{ + FieldDescriptorProto: &protodescriptor.FieldDescriptorProto{ + Name: proto.String("message_field"), + TypeName: proto.String(".example.Message"), + Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(), + }, + }, + expected: schemaCore{ + Ref: "#/definitions/exampleMessage", + }, + }, + } + + reg := descriptor.NewRegistry() + reg.Load(&plugin.CodeGeneratorRequest{ + ProtoFile: []*protodescriptor.FileDescriptorProto{ + { + SourceCodeInfo: &protodescriptor.SourceCodeInfo{}, + Name: proto.String("example.proto"), + Package: proto.String("example"), + Dependency: []string{}, + MessageType: []*protodescriptor.DescriptorProto{ + { + Name: proto.String("Message"), + Field: []*protodescriptor.FieldDescriptorProto{ + { + Name: proto.String("value"), + Type: protodescriptor.FieldDescriptorProto_TYPE_STRING.Enum(), + }, + }, + }, + }, + EnumType: []*protodescriptor.EnumDescriptorProto{ + { + Name: proto.String("Message"), + }, + }, + Service: []*protodescriptor.ServiceDescriptorProto{}, + }, + }, + }) + + for _, test := range tests { + actual := schemaOfField(test.field, reg) + if e, a := test.expected.Type, actual.Type; e != a { + t.Errorf("Expected schemaOfField(%v).Type = %s, actual: %s", test.field, e, a) + } + if e, a := test.expected.Ref, actual.Ref; e != a { + t.Errorf("Expected schemaOfField(%v).Ref = %s, actual: %s", test.field, e, a) + } + if e, a := test.expected.Items.getType(), actual.Items.getType(); e != a { + t.Errorf("Expected schemaOfField(%v).Items.Type = %v, actual.Type: %v", test.field, e, a) + } + } +} diff --git a/protoc-gen-swagger/genswagger/types.go b/protoc-gen-swagger/genswagger/types.go index 7263e66b7e0..35e8542083e 100644 --- a/protoc-gen-swagger/genswagger/types.go +++ b/protoc-gen-swagger/genswagger/types.go @@ -122,6 +122,13 @@ type schemaCore struct { type swaggerItemsObject schemaCore +func (o *swaggerItemsObject) getType() string { + if o == nil { + return "" + } + return o.Type +} + // http://swagger.io/specification/#responsesObject type swaggerResponsesObject map[string]swaggerResponseObject