Skip to content

Commit d85f449

Browse files
authored
Merge pull request #55 from link-duan/feature/security-group
feat: support for @security annotation in block comment
2 parents 4060f59 + 51d4f36 commit d85f449

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

comment.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,12 @@ func (c *Comment) Security() *spec.SecurityRequirements {
159159
return nil
160160
}
161161

162+
return convertSecAnnotationToSecurityRequirements(c.Annotations)
163+
}
164+
165+
func convertSecAnnotationToSecurityRequirements(annotations []annotation.Annotation) *spec.SecurityRequirements {
162166
ret := spec.NewSecurityRequirements()
163-
for _, annot := range c.Annotations {
167+
for _, annot := range annotations {
164168
annot, ok := annot.(*annotation.SecurityAnnotation)
165169
if ok {
166170
ret.With(spec.NewSecurityRequirement().Authenticate(annot.Name, annot.Params...))

plugins/echo/echo.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,10 @@ func (e *Plugin) parseAPI(ctx *eapi.Context, callExpr *ast.CallExpr) (api *eapi.
162162
fullPath := path.Join(prefix, e.normalizePath(strings.Trim(arg0.Value, "\"")))
163163
method := selExpr.Sel.Name
164164
api = eapi.NewAPI(method, fullPath)
165-
api.Spec.LoadFromFuncDecl(ctx.Package().Fset, handlerFnDef.Decl)
165+
api.Spec.LoadFromFuncDecl(ctx, handlerFnDef.Decl)
166166
if api.Spec.OperationID == "" {
167167
api.Spec.OperationID = handlerFnDef.Pkg().Name + "." + handlerFnDef.Decl.Name.Name
168168
}
169-
if len(api.Spec.Tags) == 0 {
170-
api.Spec.Tags = ctx.Env.LookupTags()
171-
}
172169
newHandlerAnalyzer(
173170
ctx.NewEnv().WithPackage(handlerFnDef.Pkg()).WithFile(handlerFnDef.File()),
174171
api,

plugins/gin/gin.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,10 @@ func (e *Plugin) parseAPI(ctx *analyzer.Context, callExpr *ast.CallExpr) (api *a
175175
fullPath := path.Join(prefix, e.normalizePath(strings.Trim(arg0.Value, "\"")))
176176
method := selExpr.Sel.Name
177177
api = analyzer.NewAPI(method, fullPath)
178-
api.Spec.LoadFromFuncDecl(ctx.Package().Fset, handlerFnDef.Decl)
178+
api.Spec.LoadFromFuncDecl(ctx, handlerFnDef.Decl)
179179
if api.Spec.OperationID == "" {
180180
api.Spec.OperationID = handlerFnDef.Pkg().Name + "." + handlerFnDef.Decl.Name.Name
181181
}
182-
if len(api.Spec.Tags) == 0 {
183-
api.Spec.Tags = ctx.Env.LookupTags()
184-
}
185182
newHandlerParser(
186183
ctx.NewEnv().WithPackage(handlerFnDef.Pkg()).WithFile(handlerFnDef.File()),
187184
api,

plugins/gin/testdata/server/docs/openapi.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@
245245
"description": "参数无效"
246246
}
247247
},
248+
"security": [
249+
{
250+
"oauth2": [
251+
"goods:read",
252+
"goods:write"
253+
]
254+
}
255+
],
248256
"summary": "创建商品",
249257
"tags": [
250258
"Goods"
@@ -345,6 +353,14 @@
345353
}
346354
}
347355
},
356+
"security": [
357+
{
358+
"oauth2": [
359+
"goods:read",
360+
"goods:write"
361+
]
362+
}
363+
],
348364
"summary": "下架商品",
349365
"tags": [
350366
"Goods"
@@ -377,6 +393,13 @@
377393
}
378394
}
379395
},
396+
"security": [
397+
{
398+
"oauth2": [
399+
"goods:read"
400+
]
401+
}
402+
],
380403
"summary": "商品详情",
381404
"tags": [
382405
"Goods"

plugins/gin/testdata/server/router.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ func ServeHttp() *gin.Engine {
2626
g := r.Group("/api")
2727

2828
// @tags Goods
29+
// @security oauth2 goods:read
2930
{
30-
g.POST("/goods", shop.GoodsCreate)
31-
g.POST("/goods/:guid/down", shop.GoodsDown)
31+
// @security oauth2 goods:read goods:write
32+
{
33+
g.POST("/goods", shop.GoodsCreate)
34+
g.POST("/goods/:guid/down", shop.GoodsDown)
35+
}
3236
g = g.Group("/v2")
3337
g.GET("/goods/:guid", shop.GoodsInfo)
3438
}

route.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package eapi
22

33
import (
44
"go/ast"
5-
"go/token"
65
"net/http"
76
"strings"
87

8+
"github.com/gotomicro/eapi/annotation"
99
"github.com/gotomicro/eapi/spec"
1010
)
1111

@@ -74,9 +74,9 @@ func NewAPISpec() *APISpec {
7474
}
7575

7676
// LoadFromFuncDecl load annotations/description from comments of handler function
77-
func (s *APISpec) LoadFromFuncDecl(fSet *token.FileSet, funcDecl *ast.FuncDecl) {
77+
func (s *APISpec) LoadFromFuncDecl(ctx *Context, funcDecl *ast.FuncDecl) {
7878
cg := funcDecl.Doc
79-
comment := ParseComment(cg, fSet)
79+
comment := ParseComment(cg, ctx.Package().Fset)
8080
if comment != nil {
8181
s.Summary = comment.Summary()
8282
s.Description = strings.TrimSpace(comment.TrimPrefix(funcDecl.Name.Name))
@@ -86,10 +86,15 @@ func (s *APISpec) LoadFromFuncDecl(fSet *token.FileSet, funcDecl *ast.FuncDecl)
8686
tags := comment.Tags()
8787
if len(tags) > 0 {
8888
s.Tags = comment.Tags()
89+
} else {
90+
s.Tags = ctx.Env.LookupTags()
8991
}
9092
s.OperationID = comment.ID()
9193
s.Consumes = append(s.Consumes, comment.Consumes()...)
9294
s.Deprecated = comment.Deprecated()
9395
s.Security = comment.Security()
96+
if s.Security == nil {
97+
s.Security = convertSecAnnotationToSecurityRequirements(ctx.Env.LookupAnnotations(annotation.Security))
98+
}
9499
}
95100
}

0 commit comments

Comments
 (0)