Skip to content

Commit

Permalink
Merge pull request #55 from link-duan/feature/security-group
Browse files Browse the repository at this point in the history
feat: support for @security annotation in block comment
  • Loading branch information
link-duan authored Feb 17, 2023
2 parents 4060f59 + 51d4f36 commit d85f449
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
6 changes: 5 additions & 1 deletion comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,12 @@ func (c *Comment) Security() *spec.SecurityRequirements {
return nil
}

return convertSecAnnotationToSecurityRequirements(c.Annotations)
}

func convertSecAnnotationToSecurityRequirements(annotations []annotation.Annotation) *spec.SecurityRequirements {
ret := spec.NewSecurityRequirements()
for _, annot := range c.Annotations {
for _, annot := range annotations {
annot, ok := annot.(*annotation.SecurityAnnotation)
if ok {
ret.With(spec.NewSecurityRequirement().Authenticate(annot.Name, annot.Params...))
Expand Down
5 changes: 1 addition & 4 deletions plugins/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,10 @@ func (e *Plugin) parseAPI(ctx *eapi.Context, callExpr *ast.CallExpr) (api *eapi.
fullPath := path.Join(prefix, e.normalizePath(strings.Trim(arg0.Value, "\"")))
method := selExpr.Sel.Name
api = eapi.NewAPI(method, fullPath)
api.Spec.LoadFromFuncDecl(ctx.Package().Fset, handlerFnDef.Decl)
api.Spec.LoadFromFuncDecl(ctx, handlerFnDef.Decl)
if api.Spec.OperationID == "" {
api.Spec.OperationID = handlerFnDef.Pkg().Name + "." + handlerFnDef.Decl.Name.Name
}
if len(api.Spec.Tags) == 0 {
api.Spec.Tags = ctx.Env.LookupTags()
}
newHandlerAnalyzer(
ctx.NewEnv().WithPackage(handlerFnDef.Pkg()).WithFile(handlerFnDef.File()),
api,
Expand Down
5 changes: 1 addition & 4 deletions plugins/gin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,10 @@ func (e *Plugin) parseAPI(ctx *analyzer.Context, callExpr *ast.CallExpr) (api *a
fullPath := path.Join(prefix, e.normalizePath(strings.Trim(arg0.Value, "\"")))
method := selExpr.Sel.Name
api = analyzer.NewAPI(method, fullPath)
api.Spec.LoadFromFuncDecl(ctx.Package().Fset, handlerFnDef.Decl)
api.Spec.LoadFromFuncDecl(ctx, handlerFnDef.Decl)
if api.Spec.OperationID == "" {
api.Spec.OperationID = handlerFnDef.Pkg().Name + "." + handlerFnDef.Decl.Name.Name
}
if len(api.Spec.Tags) == 0 {
api.Spec.Tags = ctx.Env.LookupTags()
}
newHandlerParser(
ctx.NewEnv().WithPackage(handlerFnDef.Pkg()).WithFile(handlerFnDef.File()),
api,
Expand Down
23 changes: 23 additions & 0 deletions plugins/gin/testdata/server/docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@
"description": "参数无效"
}
},
"security": [
{
"oauth2": [
"goods:read",
"goods:write"
]
}
],
"summary": "创建商品",
"tags": [
"Goods"
Expand Down Expand Up @@ -345,6 +353,14 @@
}
}
},
"security": [
{
"oauth2": [
"goods:read",
"goods:write"
]
}
],
"summary": "下架商品",
"tags": [
"Goods"
Expand Down Expand Up @@ -377,6 +393,13 @@
}
}
},
"security": [
{
"oauth2": [
"goods:read"
]
}
],
"summary": "商品详情",
"tags": [
"Goods"
Expand Down
8 changes: 6 additions & 2 deletions plugins/gin/testdata/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ func ServeHttp() *gin.Engine {
g := r.Group("/api")

// @tags Goods
// @security oauth2 goods:read
{
g.POST("/goods", shop.GoodsCreate)
g.POST("/goods/:guid/down", shop.GoodsDown)
// @security oauth2 goods:read goods:write
{
g.POST("/goods", shop.GoodsCreate)
g.POST("/goods/:guid/down", shop.GoodsDown)
}
g = g.Group("/v2")
g.GET("/goods/:guid", shop.GoodsInfo)
}
Expand Down
11 changes: 8 additions & 3 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package eapi

import (
"go/ast"
"go/token"
"net/http"
"strings"

"github.com/gotomicro/eapi/annotation"
"github.com/gotomicro/eapi/spec"
)

Expand Down Expand Up @@ -74,9 +74,9 @@ func NewAPISpec() *APISpec {
}

// LoadFromFuncDecl load annotations/description from comments of handler function
func (s *APISpec) LoadFromFuncDecl(fSet *token.FileSet, funcDecl *ast.FuncDecl) {
func (s *APISpec) LoadFromFuncDecl(ctx *Context, funcDecl *ast.FuncDecl) {
cg := funcDecl.Doc
comment := ParseComment(cg, fSet)
comment := ParseComment(cg, ctx.Package().Fset)
if comment != nil {
s.Summary = comment.Summary()
s.Description = strings.TrimSpace(comment.TrimPrefix(funcDecl.Name.Name))
Expand All @@ -86,10 +86,15 @@ func (s *APISpec) LoadFromFuncDecl(fSet *token.FileSet, funcDecl *ast.FuncDecl)
tags := comment.Tags()
if len(tags) > 0 {
s.Tags = comment.Tags()
} else {
s.Tags = ctx.Env.LookupTags()
}
s.OperationID = comment.ID()
s.Consumes = append(s.Consumes, comment.Consumes()...)
s.Deprecated = comment.Deprecated()
s.Security = comment.Security()
if s.Security == nil {
s.Security = convertSecAnnotationToSecurityRequirements(ctx.Env.LookupAnnotations(annotation.Security))
}
}
}

0 comments on commit d85f449

Please sign in to comment.