Skip to content

Commit

Permalink
refactor(cmd/web/restdoc): 生成的文档验证不通过也执行保存操作
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed May 30, 2024
1 parent d0cd5c5 commit 92cb2ec
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 24 deletions.
3 changes: 0 additions & 3 deletions cmd/web/locales/und.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ messages:
- key: not show ignore message
message:
msg: not show ignore message
- key: only support yaml and json
message:
msg: only support yaml and json
- key: out dir
message:
msg: out dir
Expand Down
3 changes: 0 additions & 3 deletions cmd/web/locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ messages:
- key: not show ignore message
message:
msg: 不显示可忽略的消息
- key: only support yaml and json
message:
msg: 仅支持 YAML 和 JSON
- key: out dir
message:
msg: 输出目录
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 @@ -13,7 +13,6 @@ import (
"sync"

"github.com/getkin/kin-openapi/openapi3"
"github.com/issue9/web"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -53,7 +52,7 @@ func (doc *OpenAPI) SaveAs(path string) error {
case ".json":
m = func(v any) ([]byte, error) { return json.MarshalIndent(v, "", "\t") }
default:
return web.NewLocaleError("only support yaml and json")
panic("仅支持 YAML 或 JSON 格式") // 这应该是代码级别的错误了,直接 panic。
}

data, err := m(doc.Doc())
Expand Down
2 changes: 1 addition & 1 deletion cmd/web/restdoc/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestOpenAPI_SaveAs(t *testing.T) {

a.NotError(doc.SaveAs("./openapi.out.yaml")).
NotError(doc.SaveAs("./openapi.out.json")).
ErrorString(doc.SaveAs("./openapi.out.xml"), "only support yaml and json")
PanicString(func() { doc.SaveAs("./openapi.out.xml") }, "仅支持 YAML 或 JSON 格式")
}

func TestOpenAPI_Merge(t *testing.T) {
Expand Down
10 changes: 4 additions & 6 deletions cmd/web/restdoc/openapi/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestOpenapi_Schema(t *testing.T) {
o.AddSchema(ref)
a.Length(o.doc.Components.Schemas, 0) // 空的 refID,添加不成功

ref = NewSchemaRef("abc", nil)
ref = NewSchemaRef("abc", openapi3.NewSchema())
o.AddSchema(ref)
a.Length(o.doc.Components.Schemas, 1).
Equal(ref.Ref, ComponentSchemaPrefix+"abc")
Expand All @@ -63,15 +63,13 @@ func TestOpenapi_Schema(t *testing.T) {
Equal(v1, v2). // v1,v2 指向同一个对象
Equal(v1.Ref, ComponentSchemaPrefix+"abc")

// 同名,但都是 nil
// 同名
a.NotPanic(func() {
ref = NewSchemaRef(ComponentSchemaPrefix+"abc", nil)
o.AddSchema(ref)
o.AddSchema(NewSchemaRef(ComponentSchemaPrefix+"abc", openapi3.NewSchema()))
})

a.PanicString(func() {
ref = NewSchemaRef(ComponentSchemaPrefix+"abc", &openapi3.Schema{})
o.AddSchema(ref)
o.AddSchema(NewSchemaRef(ComponentSchemaPrefix+"abc", openapi3.NewBoolSchema()))
}, "添加同名的对象 abc")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/web/restdoc/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ func (p *Parser) Parse(ctx context.Context) *openapi.OpenAPI {
}
wg.Wait()

// 验证数据,只提示错误信息,并不会返回空对象。
if err := t.Doc().Validate(ctx); err != nil {
p.l.Error(err, "", 0)
return nil
}

return t
Expand Down
11 changes: 4 additions & 7 deletions cmd/web/restdoc/schema/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
//
// typePath 表示需要查找的类型名,一般格式为 [path.]name,path 为包的路径,name 为类型名。
// 如果是内置类型可省略 path 部分。
// 如果 typePath 以 #components/schemas 开头,则从 t.Components.Schemas 下查找。
// q 是否用于查询参数,如果是查询参数,那么字段名的获取将采用 json。
// 如果 typePath 以 #components/schemas 开头,则从 [openapi3.T.Components.Schemas] 下查找。
// q 是否用于查询参数,如果是查询参数,那么字段名的获取将采用 json 且不会保存整个对象至 [openapi3.T.Components.Schemas] 之下
//
// 可能返回的错误值为 [Error]
func (s *Schema) New(ctx context.Context, t *openapi.OpenAPI, typePath string, q bool) (*openapi3.SchemaRef, error) {
Expand Down Expand Up @@ -125,12 +125,9 @@ func (s *Schema) fromType(t *openapi.OpenAPI, xmlName string, typ types.Type, st
if err != nil {
return nil, false, err
}
if openapi.RefEqual(ref.Ref, refID) && ref.Value == nil { // 从 *pkg.Struct 返回
} else {
if !basic && (!openapi.RefEqual(ref.Ref, refID) || ref.Value != nil) { // 不是从 *pkg.Struct 获得的数据
ref = openapi.NewDocSchemaRef(ref, title, desc)
if !basic {
ref.Ref = refID
}
ref.Ref = refID
}
}

Expand Down
2 changes: 1 addition & 1 deletion web.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

// Version 当前框架的版本
const Version = "0.96.1"
const Version = "0.96.2"

type (
Logger = logs.Logger
Expand Down

0 comments on commit 92cb2ec

Please sign in to comment.