lerenn/asyncapi-codegen

panic in DefaultNamifier

Closed this issue · 3 comments

sentence = strings.ToUpper(sentence[:1]) + sentence[1:]

go: downloading github.com/lerenn/asyncapi-codegen v0.41.3
panic: runtime error: slice bounds out of range [:1] with length 0

goroutine 1 [running]:
github.com/lerenn/asyncapi-codegen/pkg/utils/template.DefaultNamifier({0x10331f418, 0x1})
	/Users/nickchen/go/pkg/mod/github.com/lerenn/asyncapi-codegen@v0.41.3/pkg/utils/template/helpers.go:66 +0x30c
github.com/lerenn/asyncapi-codegen/pkg/utils/template.Namify(...)

Propose fix:

diff --git a/pkg/utils/template/helpers.go b/pkg/utils/template/helpers.go
index 0d5c21c..2491a2e 100644
--- a/pkg/utils/template/helpers.go
+++ b/pkg/utils/template/helpers.go
@@ -61,7 +61,9 @@ func DefaultNamifier(sentence string) string {
        // Remove leading numbers
        re = regexp.MustCompile("^[0-9]+")
        sentence = string(re.ReplaceAll([]byte(sentence), []byte("")))
-
+       if len(sentence) == 0 {
+               return sentence
+       }
        // Upper first letter
        sentence = strings.ToUpper(sentence[:1]) + sentence[1:]

Hello @nickchen ! Thanks for reporting this bug !
The fix looks great !

I can create the MR and merge it for you, but if you prefer to do it and be credited in the Github contributors, let me know !

#236 here you go. Ran into couple of other issues, will open separate PR for them.

Thanks for opening those issues/PR. It's really helpful for the project :)

I'll close this issue, as you solved it in a now merged PR.