From de7c864678ae7e7e3db2c77c10fc1756dc03d403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 30 Sep 2025 17:07:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8D=87=E7=BA=A7=E9=80=82=E9=85=8Dapi-?= =?UTF-8?q?doc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 ++-- go.sum | 4 ++++ router/doc.go | 36 +++++++++++++++++++++++++++++++++++- router/server.go | 5 +++-- router/server_test.go | 3 ++- 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index d84553c..d872409 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.24.1 toolchain go1.24.2 require ( - git.zhangdeman.cn/zhangdeman/api-doc v1.0.2 + git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20250930090005-69177ebcd195 git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 git.zhangdeman.cn/zhangdeman/dynamic-struct v0.0.0-20250429065800-fc340b9417cf git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250510123912-a0d52fc093ab @@ -13,7 +13,7 @@ require ( git.zhangdeman.cn/zhangdeman/logger v0.0.0-20250817142254-a501f79e7894 git.zhangdeman.cn/zhangdeman/network v0.0.0-20250726060351-78810e906bfa git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9 - git.zhangdeman.cn/zhangdeman/trace v0.0.0-20250412104923-c1ecb1bfe8d5 + git.zhangdeman.cn/zhangdeman/trace v0.0.0-20250830115953-2779077bc555 git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20250321102712-1cbfbe959740 github.com/gin-contrib/pprof v1.5.3 github.com/gin-gonic/gin v1.11.0 diff --git a/go.sum b/go.sum index b6e8353..31f6a8c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.zhangdeman.cn/zhangdeman/api-doc v1.0.2 h1:zLbHBlRQwg3iXqj/40gMiv4AO0pGs/WzTr/2icFmz+4= git.zhangdeman.cn/zhangdeman/api-doc v1.0.2/go.mod h1:PgZIhEk8yK/YU2xfj6OmtH/6XGxUJ3K2cmr34CzYjyM= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20250930090005-69177ebcd195 h1:S1DqbZdJxKxzEbanmG2I/yycScml7dJbYR+VrXzeuo0= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20250930090005-69177ebcd195/go.mod h1:tMqiqByNQ5WmFieruKEcEVJv0g2QqA5XSqV0S9WvOZU= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 h1:Yo1ur3LnDF5s7F7tpJsNrdUSF8LwYKnN9TdQU32F3eU= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc= git.zhangdeman.cn/zhangdeman/dynamic-struct v0.0.0-20250429065800-fc340b9417cf h1:xCPM3U6i62UvLo9VNvDP45Ue3dPl7ratHu1rSEJRE2k= @@ -22,6 +24,8 @@ git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9 h1:/GL git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9/go.mod h1:ABJ655C5QenQNOzf7LjCe4sSB52CXvaWLX2Zg4uwDJY= git.zhangdeman.cn/zhangdeman/trace v0.0.0-20250412104923-c1ecb1bfe8d5 h1:dD1Q/MIrRmIhKqfYPH+y167ca9CKwTPuQt3c1hXWGJ8= git.zhangdeman.cn/zhangdeman/trace v0.0.0-20250412104923-c1ecb1bfe8d5/go.mod h1:PB486NC82nuvn5yi+U2i48ogX/9EAETWAHd8O9TwY9k= +git.zhangdeman.cn/zhangdeman/trace v0.0.0-20250830115953-2779077bc555 h1:XQgYVEui0QKl5NXOvYpAg48zSc81obXF9cOinU8JuqQ= +git.zhangdeman.cn/zhangdeman/trace v0.0.0-20250830115953-2779077bc555/go.mod h1:+tLRnfL+ddqljSTOacSJoIv7TpbdWih3efd8Yvk/Hh8= git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI= git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI= git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20241125101541-c5ea194c9c1e h1:YE2Gi+M03UDImIpWa3I7jzSesyfu2RL8x/4ONs5v0oE= diff --git a/router/doc.go b/router/doc.go index 16fc1bd..acbaa25 100644 --- a/router/doc.go +++ b/router/doc.go @@ -15,8 +15,42 @@ import ( ) func NewDoc(info *define.Info, servers []*define.ServerItem) *Doc { + if nil == info { + info = &define.Info{ + Description: "", + Title: "", + TermsOfService: "", + Contact: &define.Contact{ + Name: "", + Url: "", + Email: "", + }, + License: nil, + Version: "", + } + } + if nil == info.Contact { + info.Contact = &define.Contact{ + Name: "", + Url: "", + Email: "", + } + } + if nil == info.License { + info.License = &define.License{ + Name: "", + Url: "", + } + } return &Doc{ - instance: apiDoc.NewOpenapiDoc(info, servers), + instance: apiDoc.NewOpenapiDoc( + apiDoc.WithDocDescription(info.Description), + apiDoc.WithDocTitle(info.Title), + apiDoc.WithDocContactEmail(info.Contact.Email), + apiDoc.WithDocContactName(info.Contact.Name), + apiDoc.WithDocLicense(info.License.Name), + apiDoc.WithDocServers(servers), + ), } } diff --git a/router/server.go b/router/server.go index e1b9c4d..bf8cb32 100644 --- a/router/server.go +++ b/router/server.go @@ -23,12 +23,13 @@ import ( "github.com/gin-contrib/pprof" apiDocDefine "git.zhangdeman.cn/zhangdeman/api-doc/define" + apiDocEnum "git.zhangdeman.cn/zhangdeman/api-doc/enums" "github.com/gin-gonic/gin" ) func newServerOption(port int, optionList ...SetServerOptionFunc) *serverOption { option := &serverOption{ - swaggerUiTheme: apiDocDefine.SwaggerUIThemeRedocFree, + swaggerUiTheme: apiDocEnum.SwaggerUIThemeRedocFree.String(), swaggerBaseUri: "/doc/swagger", globalMiddlewareList: nil, disableSwaggerDoc: false, @@ -125,7 +126,7 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server { } return &server{ router: r, - uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, option.swaggerUiTheme), + uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, apiDocEnum.SwaggerUITheme(option.swaggerUiTheme)), port: port, option: option, } diff --git a/router/server_test.go b/router/server_test.go index 5c5a4b8..c1233d7 100644 --- a/router/server_test.go +++ b/router/server_test.go @@ -8,8 +8,9 @@ package router import ( - "github.com/gin-gonic/gin" "testing" + + "github.com/gin-gonic/gin" ) func TestNewServer(t *testing.T) {