Skip to content

Commit

Permalink
protoc-gen-openapiv2: Use the canonical camelCase converter for proto…
Browse files Browse the repository at this point in the history
…buf (#2599)

* Use the canonical camelCase converter for protobuf. Fixes issue #2363.

* Revert change that should go in separate PR
  • Loading branch information
oyvindwe committed Mar 21, 2022
1 parent 20bc08a commit 65d4342
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/casing/LICENSE.md
@@ -1,4 +1,4 @@
Copyright 2010 The Go Authors. All rights reserved.
Copyright 2010, 2019 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
Expand Down
8 changes: 5 additions & 3 deletions internal/casing/README.md
@@ -1,5 +1,7 @@
# Case conversion

This package contains a single function, copied from the
`github.com/golang/protobuf/protoc-gen-go/generator` package. That
modules LICENSE is referenced in its entirety in this package.
This package contains two functions:
- `Camel` copied from the `github.com/golang/protobuf/protoc-gen-go/generator` package.
- `JSONCamelCase` copied from the `github.com/protocolbuffers/protobuf-go/internal/strs` package.

Both these modules are licensed by The Go Authors, as reflected in this package's [LICENSE.md].
18 changes: 18 additions & 0 deletions internal/casing/camel.go
Expand Up @@ -50,6 +50,24 @@ func Camel(s string) string {
return string(t)
}

// JSONCamelCase converts a snake_case identifier to a camelCase identifier,
// according to the protobuf JSON specification.
func JSONCamelCase(s string) string {
var b []byte
var wasUnderscore bool
for i := 0; i < len(s); i++ { // proto identifiers are always ASCII
c := s[i]
if c != '_' {
if wasUnderscore && isASCIILower(c) {
c -= 'a' - 'A' // convert to uppercase
}
b = append(b, c)
}
wasUnderscore = c == '_'
}
return string(b)
}

// And now lots of helper functions.

// Is c an ASCII lower-case letter?
Expand Down
14 changes: 3 additions & 11 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Expand Up @@ -2523,7 +2523,7 @@ func updateswaggerObjectFromJSONSchema(s *openapiSchemaObject, j *openapi_option
if reg.GetUseJSONNamesForFields() {
for i, r := range s.Required {
// TODO(oyvindwe): Look up field and use field.GetJsonName()?
s.Required[i] = doCamelCase(r)
s.Required[i] = casing.JSONCamelCase(r)
}
}
s.Enum = j.GetEnum()
Expand Down Expand Up @@ -2707,23 +2707,15 @@ func lowerCamelCase(fieldName string, fields []*descriptor.Field, msgs []*descri
fieldNames := strings.Split(fieldName, ".")
fieldNamesWithCamelCase := make([]string, 0)
for i := 0; i < len(fieldNames)-1; i++ {
fieldNamesWithCamelCase = append(fieldNamesWithCamelCase, doCamelCase(string(fieldNames[i])))
fieldNamesWithCamelCase = append(fieldNamesWithCamelCase, casing.JSONCamelCase(string(fieldNames[i])))
}
prefix := strings.Join(fieldNamesWithCamelCase, ".")
reservedJSONName := getReservedJSONName(fieldName, messageNameToFieldsToJSONName, fieldNameToType)
if reservedJSONName != "" {
return prefix + "." + reservedJSONName
}
}
return doCamelCase(fieldName)
}

func doCamelCase(input string) string {
parameterString := casing.Camel(input)
builder := &strings.Builder{}
builder.WriteString(strings.ToLower(string(parameterString[0])))
builder.WriteString(parameterString[1:])
return builder.String()
return casing.JSONCamelCase(fieldName)
}

func getReservedJSONName(fieldName string, messageNameToFieldsToJSONName map[string]map[string]string, fieldNameToType map[string]string) string {
Expand Down

0 comments on commit 65d4342

Please sign in to comment.