diff --git a/protoc-gen-openapiv2/internal/genopenapi/template.go b/protoc-gen-openapiv2/internal/genopenapi/template.go index 3d29394e13a..1856255fd99 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/template.go +++ b/protoc-gen-openapiv2/internal/genopenapi/template.go @@ -1208,6 +1208,7 @@ func expandPathPatterns(pathParts []string, pathParams []descriptor.Parameter, r Target: &descriptor.Field{ FieldDescriptorProto: &descriptorpb.FieldDescriptorProto{ Name: proto.String(paramName), + Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(), }, Message: pathParam.Target.Message, FieldMessage: pathParam.Target.FieldMessage, diff --git a/protoc-gen-openapiv2/internal/genopenapi/template_test.go b/protoc-gen-openapiv2/internal/genopenapi/template_test.go index 29592c77496..1e6f29f348e 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/template_test.go +++ b/protoc-gen-openapiv2/internal/genopenapi/template_test.go @@ -4126,8 +4126,16 @@ func getParameters(names []string) []descriptor.Parameter { Target: &descriptor.Field{ FieldDescriptorProto: &descriptorpb.FieldDescriptorProto{ Name: proto.String(name), + Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(), + }, + Message: &descriptor.Message{ + File: &descriptor.File{ + FileDescriptorProto: &descriptorpb.FileDescriptorProto{}, + }, + DescriptorProto: &descriptorpb.DescriptorProto{ + Name: proto.String(""), + }, }, - Message: &descriptor.Message{}, FieldMessage: nil, ForcePrefixedName: false, }, @@ -4170,7 +4178,31 @@ func TestTemplateToOpenAPIPathExpandSlashed(t *testing.T) { if !reflect.DeepEqual(data.expectedPathParams, pathParamsNames) { t.Errorf("Expected mutated path params in templateToOpenAPIPath(%v) = %v, actual: %v", data.input, data.expectedPathParams, data.pathParams) } + } +} +func TestExpandedPathParametersStringType(t *testing.T) { + var tests = []struct { + input string + }{ + {"/test/{name=test_cases/*}/"}, {"/v1/{name=projects/*/documents/*}:exportResults"}, + } + reg := descriptor.NewRegistry() + reg.SetExpandSlashedPathPatterns(true) + expectedParamType := openapiSchemaObject{ + schemaCore: schemaCore{ + Type: "string", + }, + } + for _, data := range tests { + _, actualParams := templateToExpandedPath(data.input, reg, generateFieldsForJSONReservedName(), generateMsgsForJSONReservedName(), getParameters([]string{"name"})) + for _, param := range actualParams { + refs := make(refMap) + actualParamType := schemaOfField(param.Target, reg, refs) + if !reflect.DeepEqual(actualParamType, expectedParamType) { + t.Errorf("Expected all path parameters to be type of 'string', actual: %#+v", actualParamType) + } + } } }