Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add expand_slashed_path_patterns flag #4813

Merged
Prev Previous commit
Next Next commit
fix: templateToParts camel-cases also path-patterns
  • Loading branch information
czabaj committed Oct 18, 2024
commit 745b0f214dd5b4aedc473ca6813e36a42454e374
17 changes: 4 additions & 13 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,30 +998,24 @@ func templateToParts(path string, reg *descriptor.Registry, fields []*descriptor
var parts []string
depth := 0
buffer := ""
jsonBuffer := ""
Copy link
Contributor Author

@czabaj czabaj Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This second jsonBuffer was just a complexity gainer, I added a single branch to the } case, where it extracts the parameter name and camel-case it, when the camel-casing is enabled.

pathLoop:
for i, char := range path {
switch char {
case '{':
// Push on the stack
depth++
buffer += string(char)
jsonBuffer = ""
jsonBuffer += string(char)
case '}':
if depth == 0 {
panic("Encountered } without matching { before it.")
}
// Pop from the stack
depth--
buffer += string(char)
if reg.GetUseJSONNamesForFields() &&
len(jsonBuffer) > 1 {
jsonSnakeCaseName := jsonBuffer[1:]
jsonCamelCaseName := lowerCamelCase(jsonSnakeCaseName, fields, msgs)
prev := buffer[:len(buffer)-len(jsonSnakeCaseName)-2]
buffer = strings.Join([]string{prev, "{", jsonCamelCaseName, "}"}, "")
jsonBuffer = ""
if reg.GetUseJSONNamesForFields() {
paramNameProto := strings.SplitN(buffer[1:], "=", 2)[0]
paramNameCamelCase := lowerCamelCase(paramNameProto, fields, msgs)
buffer = strings.Join([]string{"{", paramNameCamelCase, buffer[len(paramNameProto)+1:]}, "")
}
case '/':
if depth == 0 {
Expand All @@ -1032,7 +1026,6 @@ pathLoop:
continue
}
buffer += string(char)
jsonBuffer += string(char)
case ':':
if depth == 0 {
// As soon as we find a ":" outside a variable,
Expand All @@ -1042,10 +1035,8 @@ pathLoop:
break pathLoop
}
buffer += string(char)
jsonBuffer += string(char)
default:
buffer += string(char)
jsonBuffer += string(char)
}
}

Expand Down