Skip to content

Commit

Permalink
refactor(cmd/web/restdoc): @resp 覆盖全局定义时会有警告提示
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Sep 23, 2023
1 parent c97789c commit 7efa83e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
3 changes: 3 additions & 0 deletions cmd/web/locales/en-US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ messages:
- key: out dir
message:
msg: out dir
- key: override global response %s
message:
msg: override global response %s
- key: package %s with the same name.
message:
msg: package %s with the same name.
Expand Down
3 changes: 3 additions & 0 deletions cmd/web/locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ messages:
- key: out dir
message:
msg: 输出目录
- key: override global response %s
message:
msg: %s 与定义的全局对象相同,在当前接口中将覆盖全局定义。
- key: package %s with the same name.
message:
msg: '%s 已经拥有相同的名称'
Expand Down
14 changes: 9 additions & 5 deletions cmd/web/restdoc/parser/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/getkin/kin-openapi/openapi3"
"github.com/issue9/web"

"github.com/issue9/web/cmd/web/restdoc/openapi"
"github.com/issue9/web/cmd/web/restdoc/schema"
Expand Down Expand Up @@ -105,15 +106,18 @@ func (p *Parser) addResponses(o *openapi3.Operation, resps map[string]*openapi3.
o.Responses = make(openapi3.Responses, l)
}

for status, r := range resps {
o.Responses[status] = &openapi3.ResponseRef{Value: r}
}

if g {
if g { // 全局的定义在前,才会被本地定义覆盖。
for key, resp := range p.resps {
o.Responses[key] = &openapi3.ResponseRef{Ref: responsesRef + key, Value: resp}
}
}

for status, r := range resps {
if _, found := o.Responses[status]; found {
p.l.Warning(web.Phrase("override global response %s", status))
}
o.Responses[status] = &openapi3.ResponseRef{Value: r}
}
}

// 当 media 为空时则直接采用 [Parser.media]
Expand Down
5 changes: 3 additions & 2 deletions serializer/html/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"testing"
"time"

"github.com/issue9/assert/v3"
"golang.org/x/text/language"
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestInstallView(t *testing.T) {
InstallView(s, false, os.DirFS("./testdata/view"), "*.tpl")

defer servertest.Run(a, s)()
defer s.Close(0)
defer s.Close(500 * time.Millisecond)

type obj struct {
HTMLName struct{} `html:"t"`
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestInstallDirView(t *testing.T) {
instalDirView(s, os.DirFS("./testdata/dir"), "*.tpl")

defer servertest.Run(a, s)()
defer s.Close(0)
defer s.Close(500 * time.Millisecond)

type obj struct {
HTMLName struct{} `html:"t"`
Expand Down

0 comments on commit 7efa83e

Please sign in to comment.