Skip to content

Commit

Permalink
fix: path parametrs of type number (#4866)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
czabaj authored Nov 1, 2024
1 parent 40e57f3 commit a31015c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 33 additions & 1 deletion protoc-gen-openapiv2/internal/genopenapi/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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)
}
}
}
}

Expand Down

0 comments on commit a31015c

Please sign in to comment.