Skip to content

Commit

Permalink
refactor(openapi): MarkdownProblems 可以自定义标题的级别
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Dec 2, 2024
1 parent f869aa1 commit d39e28b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
2 changes: 2 additions & 0 deletions openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/text/language"

"github.com/issue9/web"
"github.com/issue9/web/locales"
"github.com/issue9/web/server"
)

Expand All @@ -21,6 +22,7 @@ func newServer(a *assert.Assertion) web.Server {
})
a.NotError(err).NotNil(s)

s.Locale().LoadMessages("*.yaml", locales.Locales...)
a.NotError(s.Locale().SetString(language.SimplifiedChinese, "lang", "简体"))
a.NotError(s.Locale().SetString(language.TraditionalChinese, "lang", "繁体"))

Expand Down
28 changes: 24 additions & 4 deletions openapi/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@ import (

type Option func(*Document)

// WithHead 是否生成 HEAD 接口请求
func WithHead(enable bool) Option {
// WithOptions 合并多个 [Option] 为一个
func WithOptions(o ...Option) Option {
return func(d *Document) {
for _, opt := range o {
opt(d)
}
}
}

// WithHeadMethod 是否生成 HEAD 接口请求
func WithHeadMethod(enable bool) Option {
return func(d *Document) { d.enableHead = enable }
}

// WithOptions 是否生成 OPTIONS 请求
func WithOptions(enable bool) Option {
// WithOptionsMethod 是否生成 OPTIONS 请求
func WithOptionsMethod(enable bool) Option {
return func(d *Document) { d.enableOptions = enable }
}

Expand Down Expand Up @@ -76,6 +85,17 @@ func WithProblemResponse() Option {
}, "4XX", "5XX")
}

// WithClassicResponse 提供框架一些常用的 [Response] 对象
//
// 包含了 4XX 和 5XX 的错误对象;
// 包含了一个 ref 为 empty 的空对象;
func WithClassicResponse() Option {
return WithOptions(
WithProblemResponse(),
WithResponse(&Response{Ref: &Ref{Ref: "empty"}}),
)
}

// WithMediaType 指定所有接口可用的媒体类型
//
// t 用于指定支持的媒体类型,必须是 [web.Server] 实例支持的类型。
Expand Down
9 changes: 9 additions & 0 deletions openapi/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ import (
"github.com/issue9/web/mimetype/json"
)

func TestWithOptions(t *testing.T) {
a := assert.New(t, false)

ss := newServer(a)
d := New(ss, web.Phrase("title"), WithOptions(WithHeadMethod(true), WithOptionsMethod(true)))
a.True(d.enableHead).
True(d.enableOptions)
}

func TestWithHTML(t *testing.T) {
a := assert.New(t, false)

Expand Down
9 changes: 6 additions & 3 deletions openapi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@ func getPathParams(path string) []string {
}

// MarkdownProblems 将 problems 的内容生成为 markdown
func MarkdownProblems(s web.Server) web.LocaleStringer {
//
// titleLevlel 标题的级别,1-6;
func MarkdownProblems(s web.Server, titleLevlel int) web.LocaleStringer {
buf := &errwrap.Buffer{}

ss := strings.Repeat("#", titleLevlel)
args := make([]any, 0, 30)
s.Problems().Visit(func(status int, lp *web.LocaleProblem) {
buf.WString("## %s \n\n").
buf.Printf("%s %s ", ss, lp.Type()).
WString("%s\n\n").
WString("%s\n\n")
args = append(args, lp.Type(), lp.Title, lp.Detail)
args = append(args, lp.Title, lp.Detail)
})

return web.Phrase(buf.String(), args...)
Expand Down
19 changes: 19 additions & 0 deletions openapi/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package openapi

import (
"strings"
"testing"

"github.com/issue9/assert/v4"
Expand Down Expand Up @@ -47,3 +48,21 @@ func TestGetPathParams(t *testing.T) {
a.Equal(getPathParams("/path/{id}/{id2}"), []string{"id", "id2"})
a.Equal(getPathParams("/path/{id:number}/{id2}"), []string{"id:number", "id2"})
}

func TestMarkdownProblems(t *testing.T) {
a := assert.New(t, false)
ss := newServer(a)
p := ss.Locale().NewPrinter(language.SimplifiedChinese)

txt := MarkdownProblems(ss, 2)
lines := strings.Split(txt.LocaleString(p), "\n\n")
a.Equal(lines[0], "## 400 Bad Request").
Equal(lines[1], "表示客户端错误,比如,错误的请求语法、无效的请求消息帧或欺骗性的请求路由等,服务器无法或不会处理该请求。").
Equal(lines[2], "## 401 Unauthorized")

txt = MarkdownProblems(ss, 3)
lines = strings.Split(txt.LocaleString(p), "\n\n")
a.Equal(lines[0], "### 400 Bad Request").
Equal(lines[1], "表示客户端错误,比如,错误的请求语法、无效的请求消息帧或欺骗性的请求路由等,服务器无法或不会处理该请求。").
Equal(lines[2], "### 401 Unauthorized")
}

0 comments on commit d39e28b

Please sign in to comment.