Skip to content

Commit

Permalink
fix(cmd/web/restdoc): 保存 schema 时会正确删除 Ref 字段的内容
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Feb 1, 2024
1 parent 4c6cd0b commit 34661ed
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 21 deletions.
11 changes: 2 additions & 9 deletions cmd/web/restdoc/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (
)

type Logger struct {
logs *logs.Logs
count int
hasErr bool
logs *logs.Logs
count int
}

func New(l *logs.Logs) *Logger { return &Logger{logs: l} }
Expand All @@ -29,8 +28,6 @@ func (l *Logger) Info(msg any) { l.log(logs.LevelInfo, msg, "", 0) }
// Warning 输出警告信息
func (l *Logger) Warning(msg any) { l.log(logs.LevelWarn, msg, "", 0) }

func (l *Logger) HasError() bool { return l.hasErr }

// Error 输出错误信息
//
// 如果 msg 包含了定位信息,则 filename 和 line 将被忽略
Expand Down Expand Up @@ -76,10 +73,6 @@ func (l *Logger) log(lv logs.Level, msg any, filename string, line int) {

l.count++ // 只有真正输出时,才需要+1。

if !l.hasErr && (lv == logs.LevelError || lv == logs.LevelFatal) {
l.hasErr = true
}

if filename != "" {
m = web.Phrase("%s at %s:%d", m, filename, line)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/web/restdoc/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestLogger(t *testing.T) {
e1 := &scanner.Error{Pos: token.Position{Filename: "f1.go"}, Msg: "e1"}
e2 := &scanner.Error{Pos: token.Position{Filename: "f1.go"}, Msg: "e2"}
l.Error(e1, "f1.go", 0)
a.Equal(1, l.Count()).True(buf.Len() > 0).True(l.HasError())
a.Equal(1, l.Count()).True(buf.Len() > 0)

list := scanner.ErrorList{e1, e2}
l.Error(list, "f1.go", 0)
Expand Down
3 changes: 1 addition & 2 deletions cmd/web/restdoc/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (

// OpenAPI 协程安全的 OpenAPI 对象
type OpenAPI struct {
doc *openapi3.T

doc *openapi3.T
pathLocker sync.Mutex
schemaLocker sync.RWMutex
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/web/restdoc/openapi/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (doc *OpenAPI) AddSchema(schema *openapi3.SchemaRef) {
}
panic(fmt.Sprintf("添加同名的对象 %s,但是值不相同:\n%+v\n%+v", ref, schema.Value, s.Value))
}

schema.Ref = ""
doc.doc.Components.Schemas[ref] = schema
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/web/restdoc/parser/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import (

func (p *Parser) parseAPI(ctx context.Context, t *openapi.OpenAPI, currPath, suffix string, lines []string, ln int, filename string) {
defer func() {
// NOTE: recover 用于处理 openapi3 的 panic,但是不带行号信息。
// 应当尽量在此之前查出错误。
// NOTE: recover 用于处理 openapi3 的 panic,但是不带行号信息。应当尽量在此之前查出错误。
if msg := recover(); msg != nil {
p.l.Error(msg, filename, ln)
p.l.Fatal(msg)
}
}()

Expand Down Expand Up @@ -94,6 +93,7 @@ LOOP:
}
p.addResponses(opt, resps, true)
t.AddAPI(p.prefix+path, opt, method)
p.l.Info(web.NewLocaleError("add API %s %s", method, p.prefix+path))
}

func (p *Parser) addQuery(ctx context.Context, t *openapi.OpenAPI, opt *openapi3.Operation, currPath, suffix, filename string, ln int) {
Expand Down
4 changes: 0 additions & 4 deletions cmd/web/restdoc/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ func (p *Parser) Parse(ctx context.Context) *openapi.OpenAPI {
}
wg.Wait()

if p.l.HasError() {
return nil
}

if err := t.Doc().Validate(ctx); err != nil {
p.l.Error(err, "", 0)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/web/restdoc/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestParser_Parse(t *testing.T) {
a.NotNil(d).
Length(l.Records[logs.LevelError], 0).
Length(l.Records[logs.LevelWarn], 0).
Length(l.Records[logs.LevelInfo], 1) // scan dir 的提示
Length(l.Records[logs.LevelInfo], 2) // scan dir/ add api 的提示

a.NotNil(d.Doc().Info).
Equal(d.Doc().Info.Version, "1.0.0")
Expand Down
2 changes: 1 addition & 1 deletion cmd/web/restdoc/restdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
const defaultOutput = "./restdoc.json"

func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
opt.New("doc", title.LocaleString(p), usage.LocaleString(p), func(fs *flag.FlagSet) cmdopt.DoFunc {
opt.New("restdoc", title.LocaleString(p), usage.LocaleString(p), func(fs *flag.FlagSet) cmdopt.DoFunc {
o := fs.String("o", defaultOutput, outputUsage.LocaleString(p))
r := fs.Bool("r", true, recursiveUsage.LocaleString(p))
t := fs.String("t", "", tagUsage.LocaleString(p))
Expand Down

0 comments on commit 34661ed

Please sign in to comment.