From 92cb2ec2834cbc3c4153686402c2b252e368739f Mon Sep 17 00:00:00 2001 From: caixw Date: Thu, 30 May 2024 12:53:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cmd/web/restdoc):=20=E7=94=9F=E6=88=90?= =?UTF-8?q?=E7=9A=84=E6=96=87=E6=A1=A3=E9=AA=8C=E8=AF=81=E4=B8=8D=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E4=B9=9F=E6=89=A7=E8=A1=8C=E4=BF=9D=E5=AD=98=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/web/locales/und.yaml | 3 --- cmd/web/locales/zh-CN.yaml | 3 --- cmd/web/restdoc/openapi/openapi.go | 3 +-- cmd/web/restdoc/openapi/openapi_test.go | 2 +- cmd/web/restdoc/openapi/schema_test.go | 10 ++++------ cmd/web/restdoc/parser/parser.go | 2 +- cmd/web/restdoc/schema/search.go | 11 ++++------- web.go | 2 +- 8 files changed, 12 insertions(+), 24 deletions(-) diff --git a/cmd/web/locales/und.yaml b/cmd/web/locales/und.yaml index 369dd674..09d9dab9 100755 --- a/cmd/web/locales/und.yaml +++ b/cmd/web/locales/und.yaml @@ -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 diff --git a/cmd/web/locales/zh-CN.yaml b/cmd/web/locales/zh-CN.yaml index de5f63a1..2606d1e8 100755 --- a/cmd/web/locales/zh-CN.yaml +++ b/cmd/web/locales/zh-CN.yaml @@ -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: 输出目录 diff --git a/cmd/web/restdoc/openapi/openapi.go b/cmd/web/restdoc/openapi/openapi.go index 51623f9a..1b006e01 100644 --- a/cmd/web/restdoc/openapi/openapi.go +++ b/cmd/web/restdoc/openapi/openapi.go @@ -13,7 +13,6 @@ import ( "sync" "github.com/getkin/kin-openapi/openapi3" - "github.com/issue9/web" "gopkg.in/yaml.v3" ) @@ -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()) diff --git a/cmd/web/restdoc/openapi/openapi_test.go b/cmd/web/restdoc/openapi/openapi_test.go index 9855a19c..6cbf03e6 100644 --- a/cmd/web/restdoc/openapi/openapi_test.go +++ b/cmd/web/restdoc/openapi/openapi_test.go @@ -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) { diff --git a/cmd/web/restdoc/openapi/schema_test.go b/cmd/web/restdoc/openapi/schema_test.go index 243605e7..df2724c2 100644 --- a/cmd/web/restdoc/openapi/schema_test.go +++ b/cmd/web/restdoc/openapi/schema_test.go @@ -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") @@ -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") } diff --git a/cmd/web/restdoc/parser/parser.go b/cmd/web/restdoc/parser/parser.go index 2a4bc859..2be8a070 100644 --- a/cmd/web/restdoc/parser/parser.go +++ b/cmd/web/restdoc/parser/parser.go @@ -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 diff --git a/cmd/web/restdoc/schema/search.go b/cmd/web/restdoc/schema/search.go index 4aa9c398..ec077f02 100644 --- a/cmd/web/restdoc/schema/search.go +++ b/cmd/web/restdoc/schema/search.go @@ -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) { @@ -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 } } diff --git a/web.go b/web.go index f94231fc..c9b92c2f 100644 --- a/web.go +++ b/web.go @@ -24,7 +24,7 @@ import ( ) // Version 当前框架的版本 -const Version = "0.96.1" +const Version = "0.96.2" type ( Logger = logs.Logger