Skip to content

Commit

Permalink
fix: update openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Oct 30, 2023
1 parent 6d026b4 commit de33cde
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 7 additions & 3 deletions internal/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,26 @@ func main() {
srv.SetPrefix("/api/v1")
srv.AddSecurity(security.Basic{}, security.Bearer{})
srv.PostOf(func(op *opendoc.Operation) {
op.SetPath("article_create", "/articles")
op.SetPath("/articles")
op.SetOperation("article_create")
op.SetModel(new(TestQueryReq1), new(TestQueryRsp))
op.SetSummary("create article")
})

srv.GetOf(func(op *opendoc.Operation) {
op.SetPath("article_list", "/articles")
op.SetPath("/articles")
op.SetOperation("article_list")
op.SetModel(new(TestQueryReq), new(TestQueryRsp))
op.SetSummary("get article list")
op.AddResponse("Test", new(TestQueryReqAAA))
})

srv.PutOf(func(op *opendoc.Operation) {
op.SetPath("article_update", "/articles/{id}")
op.SetPath("/articles/{id}")
op.SetOperation("article_update")
op.SetModel(new(TestQueryReq1), new(TestQueryRsp))
op.SetSummary("update article")
op.AddResponse("error", &TestFileReq{})
})
})

Expand Down
7 changes: 2 additions & 5 deletions opendoc/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func (op *Operation) SetModel(req interface{}, rsp interface{}) *Operation {
return op
}

func (op *Operation) Openapi() *openapi3.PathItem {
func (op *Operation) Openapi(item *openapi3.PathItem) {
if op.exclude {
return nil
return
}

responses := genResponses(op.response, op.responseContentType...)
Expand All @@ -120,7 +120,6 @@ func (op *Operation) Openapi() *openapi3.PathItem {
Security: getSecurityRequirements(op.securities),
}

item := new(openapi3.PathItem)
switch op.method {
case http.MethodGet:
item.Get = operation
Expand All @@ -146,6 +145,4 @@ func (op *Operation) Openapi() *openapi3.PathItem {
case http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
operation.RequestBody = genRequestBody(op.request, op.requestContentType...)
}

return item
}
5 changes: 4 additions & 1 deletion opendoc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ func (s *Service) Openapi() map[string]*openapi3.PathItem {
var routes = make(map[string]*openapi3.PathItem)
for i := range s.operations {
op := s.operations[i]
routes[op.method+op.path] = op.Openapi()
if routes[op.path] == nil {
routes[op.path] = new(openapi3.PathItem)
}
op.Openapi(routes[op.path])
}
return routes
}

0 comments on commit de33cde

Please sign in to comment.