Skip to content

Commit

Permalink
refactor(oepnapi): Schema 可以为 nil 添加了 MarkdownProblems
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Dec 2, 2024
1 parent 56ce646 commit f869aa1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/web/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/issue9/logs/v7 v7.6.4
github.com/issue9/source v0.11.6
github.com/issue9/term/v3 v3.3.2
github.com/issue9/web v0.98.0
github.com/issue9/web v0.99.3
golang.org/x/text v0.20.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
5 changes: 3 additions & 2 deletions openapi/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func WithHTML(tpl, assets, logo string) Option {
// 一般用于指定非正常状态的返回对象,比如 400 状态码的对象。
//
// resp 返回对象,需要指定 resp.Ref.Ref,其它接口可以通过 Ref 引用该对象;
// status: 状态码,可以是 4XX 的形式,如果该值不为空,那么将当前对象以此状态码应用到所有的接口;
// status: 状态码,可以是 4XX 的形式,如果该值不为空,那么将当前对象以此状态码应用到所有的接口,
// 否则仅仅是写入 components/responses,后续需要用户手动引用;
//
// NOTE: 多次调用会依次添加
func WithResponse(resp *Response, status ...string) Option {
Expand Down Expand Up @@ -311,7 +312,7 @@ func WithTag(name string, desc web.LocaleStringer, extDocURL string, extDocDesc
// WithSecurityScheme 指定验证方案
//
// s 需要添加的验证方案;
// scope 如果指定了该值,那么会以 s.ID 为名称,scope 为值添加至 openapi.securiy
// scope 如果指定了该值,那么会以 s.ID 为名称,scope 为值添加至 openapi.security
// scope 如果是多个参数,每个参数应该都是不同的;
//
// NOTE: 多次调用会依次添加
Expand Down
12 changes: 8 additions & 4 deletions openapi/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func AllOfSchema(title, desc web.LocaleStringer, v ...any) *Schema {
return xOfSchema(2, title, desc, v...)
}

// - 0 anyof
// - 1 oneof
// - 2 allof
// - 0 AnyOf
// - 1 OneOf
// - 2 AllOf
func xOfSchema(typ int, title, desc web.LocaleStringer, v ...any) *Schema {
if len(v) == 0 {
panic("参数 v 必不可少")
Expand Down Expand Up @@ -138,6 +138,10 @@ func xOfSchema(typ int, title, desc web.LocaleStringer, v ...any) *Schema {
}

func newSchema(d *Document, v any, title, desc web.LocaleStringer) *Schema {
if v == nil {
return nil
}

s := &Schema{
Title: title,
Description: desc,
Expand All @@ -147,8 +151,8 @@ func newSchema(d *Document, v any, title, desc web.LocaleStringer) *Schema {
if !rv.IsZero() {
s.Default = v
}

schemaFromType(d, rv.Type(), true, "", s)

return s
}

Expand Down
16 changes: 16 additions & 0 deletions openapi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"slices"
"strings"

"github.com/issue9/errwrap"
orderedmap "github.com/wk8/go-ordered-map/v2"
"golang.org/x/text/message"

Expand Down Expand Up @@ -92,3 +93,18 @@ func getPathParams(path string) []string {

return ret
}

// MarkdownProblems 将 problems 的内容生成为 markdown
func MarkdownProblems(s web.Server) web.LocaleStringer {
buf := &errwrap.Buffer{}

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

return web.Phrase(buf.String(), args...)
}

0 comments on commit f869aa1

Please sign in to comment.