Skip to content

Commit 5d43d04

Browse files
authored
bug openapi: fix path params parsing (#155)
1 parent fe1f68a commit 5d43d04

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ require (
6262
github.com/spf13/afero v1.8.0 // indirect
6363
github.com/spf13/cast v1.4.1 // indirect
6464
github.com/spf13/jwalterweatherman v1.1.0 // indirect
65+
github.com/stretchr/objx v0.4.0 // indirect
6566
github.com/subosito/gotenv v1.2.0 // indirect
6667
github.com/xanzy/ssh-agent v0.3.0 // indirect
6768
go.uber.org/atomic v1.9.0 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8q
413413
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
414414
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
415415
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
416+
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
416417
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
417418
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
418419
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

pkg/generator/lib/endpoints/env_helper.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ func SnakeCaseToCamelCase(inputUnderScoreStr string, capitalize bool) (camelCase
4444
}
4545
return
4646
}
47+
48+
func CamelCaseToSnakeCase(inputCamelCaseStr string) string {
49+
var outputSnakeCaseStr string
50+
for i, r := range inputCamelCaseStr {
51+
if unicode.IsUpper(r) {
52+
if i > 0 && unicode.IsLower(rune(inputCamelCaseStr[i-1])) {
53+
outputSnakeCaseStr += "_"
54+
}
55+
outputSnakeCaseStr += string(unicode.ToLower(r))
56+
} else {
57+
outputSnakeCaseStr += string(r)
58+
}
59+
}
60+
return outputSnakeCaseStr
61+
}

pkg/generator/steps/openapi/processors/golang.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ func (p *goPostProcessor) PopulateServerHandlers(ctx *gencontext.GenContext, pat
9898
}
9999
pathsSet := map[string]string{}
100100
for _, path := range paths {
101-
path = strings.ReplaceAll(path, "{", "")
102-
path = strings.ReplaceAll(path, "}", "")
101+
ctx.Logger.Infof("pre path: %s", path)
103102
pathsSet[toAPIFilename(path)] = path
104103
}
105104
ctx.Logger.Infof("paths: %v", pathsSet)
@@ -218,22 +217,17 @@ func isReservedFilename(name string) bool {
218217
}
219218

220219
var (
221-
capitalLetterPattern = regexp.MustCompile(`([A-Z]+)([A-Z][a-z][a-z]+)`)
222-
lowercasePattern = regexp.MustCompile(`([a-z\d])([A-Z])`)
223220
pkgSeparatorPattern = regexp.MustCompile(`\.`)
224221
dollarPattern = regexp.MustCompile(`\$`)
225222
)
226223

227224
// taken from openapi-generator
228225
func underscore(word string) string {
229-
replacementPattern := "$1_$2"
230226
// Replace package separator with slash.
231-
result := pkgSeparatorPattern.ReplaceAllString(word, "/")
227+
result := pkgSeparatorPattern.ReplaceAllString(word, "_")
232228
// Replace $ with two underscores for inner classes.
233229
result = dollarPattern.ReplaceAllString(result, "__")
234-
// Replace capital letter with _ plus lowercase letter.
235-
result = capitalLetterPattern.ReplaceAllString(result, replacementPattern)
236-
result = lowercasePattern.ReplaceAllString(result, replacementPattern)
230+
result = endpoints.CamelCaseToSnakeCase(result)
237231
result = strings.ReplaceAll(result, "-", "_")
238232
// replace space with underscore
239233
result = strings.ReplaceAll(result, " ", "_")
@@ -246,7 +240,9 @@ func toAPIFilename(name string) string {
246240
// NOTE: openapi-generator transforms tag to camelCase, we don't do that here
247241
// we just remove slashes from path and then use openapi-generator logic
248242
// to convert this path to filename.
249-
api := strings.TrimPrefix(name, "/")
243+
api := strings.ReplaceAll(name, "{", "")
244+
api = strings.ReplaceAll(api, "}", "")
245+
api = strings.TrimPrefix(api, "/")
250246
api = strings.TrimSuffix(api, "/")
251247
api = strings.ReplaceAll(api, "/", "_")
252248
// replace - with _ e.g. created-at => created_at
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package processors
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestToApiFilename(t *testing.T) {
10+
assert.Equal(t, "api_path_to_api", toAPIFilename("/path/to/api"))
11+
assert.Equal(t, "api_path_to_api", toAPIFilename("/path/to/api/"))
12+
assert.Equal(t, "api_path_to_api_minus", toAPIFilename("/path/to/api-minus/"))
13+
assert.Equal(t, "api_path_to_api_param_test_other", toAPIFilename("/path/to/api/{param}.{test}/{other}"))
14+
15+
// reserved filename
16+
assert.Equal(t, "api_api_v1_test_", toAPIFilename("/api/v1/test"))
17+
}

0 commit comments

Comments
 (0)