Skip to content

Commit a31015c

Browse files
authored
fix: path parametrs of type number (#4866)
* fix: path parametrs of type number path parameters generated with `expand_slashed_path_patterns` are of type `number`, should be a `string` * test: inferred path params have a type string
1 parent 40e57f3 commit a31015c

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

protoc-gen-openapiv2/internal/genopenapi/template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,7 @@ func expandPathPatterns(pathParts []string, pathParams []descriptor.Parameter, r
12081208
Target: &descriptor.Field{
12091209
FieldDescriptorProto: &descriptorpb.FieldDescriptorProto{
12101210
Name: proto.String(paramName),
1211+
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
12111212
},
12121213
Message: pathParam.Target.Message,
12131214
FieldMessage: pathParam.Target.FieldMessage,

protoc-gen-openapiv2/internal/genopenapi/template_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4126,8 +4126,16 @@ func getParameters(names []string) []descriptor.Parameter {
41264126
Target: &descriptor.Field{
41274127
FieldDescriptorProto: &descriptorpb.FieldDescriptorProto{
41284128
Name: proto.String(name),
4129+
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
4130+
},
4131+
Message: &descriptor.Message{
4132+
File: &descriptor.File{
4133+
FileDescriptorProto: &descriptorpb.FileDescriptorProto{},
4134+
},
4135+
DescriptorProto: &descriptorpb.DescriptorProto{
4136+
Name: proto.String(""),
4137+
},
41294138
},
4130-
Message: &descriptor.Message{},
41314139
FieldMessage: nil,
41324140
ForcePrefixedName: false,
41334141
},
@@ -4170,7 +4178,31 @@ func TestTemplateToOpenAPIPathExpandSlashed(t *testing.T) {
41704178
if !reflect.DeepEqual(data.expectedPathParams, pathParamsNames) {
41714179
t.Errorf("Expected mutated path params in templateToOpenAPIPath(%v) = %v, actual: %v", data.input, data.expectedPathParams, data.pathParams)
41724180
}
4181+
}
4182+
}
41734183

4184+
func TestExpandedPathParametersStringType(t *testing.T) {
4185+
var tests = []struct {
4186+
input string
4187+
}{
4188+
{"/test/{name=test_cases/*}/"}, {"/v1/{name=projects/*/documents/*}:exportResults"},
4189+
}
4190+
reg := descriptor.NewRegistry()
4191+
reg.SetExpandSlashedPathPatterns(true)
4192+
expectedParamType := openapiSchemaObject{
4193+
schemaCore: schemaCore{
4194+
Type: "string",
4195+
},
4196+
}
4197+
for _, data := range tests {
4198+
_, actualParams := templateToExpandedPath(data.input, reg, generateFieldsForJSONReservedName(), generateMsgsForJSONReservedName(), getParameters([]string{"name"}))
4199+
for _, param := range actualParams {
4200+
refs := make(refMap)
4201+
actualParamType := schemaOfField(param.Target, reg, refs)
4202+
if !reflect.DeepEqual(actualParamType, expectedParamType) {
4203+
t.Errorf("Expected all path parameters to be type of 'string', actual: %#+v", actualParamType)
4204+
}
4205+
}
41744206
}
41754207
}
41764208

0 commit comments

Comments
 (0)