From d430f2f52e48b3e6cf3cf4f1a527d6a49a88a4e8 Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 8 Oct 2021 11:40:47 +0800 Subject: [PATCH] openapi --- protocol/goai/goai.go | 1 + protocol/goai/goai_path.go | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/protocol/goai/goai.go b/protocol/goai/goai.go index d52723946..4e951912b 100644 --- a/protocol/goai/goai.go +++ b/protocol/goai/goai.go @@ -99,6 +99,7 @@ func New() *OpenApiV3 { // AddInput is the structured parameter for function OpenApiV3.Add. type AddInput struct { Path string // Path specifies the custom path if this is not configured in Meta of struct tag. + Prefix string // Prefix specifies the custom route path prefix, which will be added with the path tag in Meta of struct tag. Method string // Method specifies the custom HTTP method if this is not configured in Meta of struct tag. Object interface{} // Object can be an instance of struct or a route function. } diff --git a/protocol/goai/goai_path.go b/protocol/goai/goai_path.go index 7e3798ffa..6fa73d224 100644 --- a/protocol/goai/goai_path.go +++ b/protocol/goai/goai_path.go @@ -41,9 +41,10 @@ const ( ) type addPathInput struct { - Path string - Method string - Function interface{} + Path string // Precise route path. + Prefix string // Route path prefix. + Method string // Route method. + Function interface{} // Uniformed function. } func (oai *OpenApiV3) addPath(in addPathInput) error { @@ -97,6 +98,9 @@ func (oai *OpenApiV3) addPath(in addPathInput) error { // Path check. if in.Path == "" { in.Path = gmeta.Get(inputObject.Interface(), TagNamePath).String() + if in.Prefix != "" { + in.Path = gstr.TrimRight(in.Prefix, "/") + "/" + gstr.TrimLeft(in.Path, "/") + } } if in.Path == "" { return gerror.NewCodef( @@ -105,6 +109,7 @@ func (oai *OpenApiV3) addPath(in addPathInput) error { TagNamePath, inputStructTypeName, ) } + // Method check. if in.Method == "" { in.Method = gmeta.Get(inputObject.Interface(), TagNameMethod).String()