Skip to content

Commit

Permalink
fix genResp type in swagger annotions
Browse files Browse the repository at this point in the history
  • Loading branch information
waltcow committed Oct 28, 2022
1 parent 41a6c23 commit 932b7a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tools/goctl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ build:
goctls template init
$(if $(shell command -v upx), upx goctl)

build-and-install:
go build -ldflags="-s -w" goctl.go
mv goctl ~/go/bin/goctls
goctls template clean && goctls template init

mac:
GOOS=darwin go build -ldflags="-s -w" -o goctl-darwin goctl.go
$(if $(shell command -v upx), upx goctl-darwin)
Expand Down
14 changes: 13 additions & 1 deletion tools/goctl/api/gogen/gentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func writeType(writer io.Writer, tp spec.Type, config *config.Config) error {

// write doc for swagger
if config.AnnotateWithSwagger {
inBodyTagCount := 0
// if a response has more than one `in: body` tag, we should use `swagger:model` tag
for _, member := range structType.Members {
s := strings.Join(member.Docs, "")
if s != "" && strings.Contains(s, "in: body") {
inBodyTagCount++
}
}
stringBuilder := &strings.Builder{}
for _, v := range structType.Documents() {
stringBuilder.WriteString(fmt.Sprintf("\t%s\n", v))
Expand All @@ -86,7 +94,11 @@ func writeType(writer io.Writer, tp spec.Type, config *config.Config) error {
} else {
fmt.Fprintf(writer, "\t// The response data of %s \n", strings.TrimSuffix(tp.Name(), "Resp"))
}
fmt.Fprintf(writer, "\t// swagger:response %s\n", tp.Name())
if inBodyTagCount > 1 {
fmt.Fprintf(writer, "\t// swagger:model %s\n", tp.Name())
} else {
fmt.Fprintf(writer, "\t// swagger:response %s\n", tp.Name())
}
} else {
if stringBuilder.Len() > 0 {
fmt.Fprintf(writer, stringBuilder.String())
Expand Down

0 comments on commit 932b7a0

Please sign in to comment.