Skip to content

Commit

Permalink
Generate valid OpenAPI specifications
Browse files Browse the repository at this point in the history
When security requirements are empty (needs to be empty array and not nil).
Also take into consideration metadata set at service level to generate summary.
  • Loading branch information
raphael committed Jan 21, 2024
1 parent 476a8ea commit 9cefb4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 13 additions & 1 deletion http/codegen/openapi/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ func summaryFromExpr(name string, e *expr.HTTPEndpointExpr) string {
return mdata[0]
}
}
for n, mdata := range e.Service.ServiceExpr.Meta {
if (n == "openapi:summary" || n == "swagger:summary") && len(mdata) > 0 {
return mdata[0]
}
}
for n, mdata := range expr.Root.API.Meta {
if (n == "openapi:summary" || n == "swagger:summary") && len(mdata) > 0 {
return mdata[0]
}
}
return name
}

Expand Down Expand Up @@ -623,7 +633,9 @@ func buildPathFromExpr(s *V2, root *expr.RootExpr, h *expr.HostExpr, route *expr
requirement[s.Hash()] = []string{}
switch s.Kind {
case expr.OAuth2Kind:
requirement[s.Hash()] = append(requirement[s.Hash()], req.Scopes...)
if len(req.Scopes) > 0 {
requirement[s.Hash()] = req.Scopes
}
case expr.BasicAuthKind, expr.APIKeyKind, expr.JWTKind:
lines := make([]string, 0, len(req.Scopes))
for _, scope := range req.Scopes {
Expand Down
9 changes: 6 additions & 3 deletions http/codegen/openapi/v3/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func buildOperation(key string, r *expr.RouteExpr, bodies *EndpointBodies, rand
{
summary = fmt.Sprintf("%s %s", e.Name(), svc.Name())
setSummary(expr.Root.API.Meta)
setSummary(svc.ServiceExpr.Meta)
setSummary(r.Endpoint.Meta)
setSummary(m.Meta)
}
Expand Down Expand Up @@ -548,12 +549,14 @@ func buildSecurityRequirements(reqs []*expr.SecurityExpr) []map[string][]string
for i, req := range reqs {
sr := make(map[string][]string, len(req.Schemes))
for _, sch := range req.Schemes {
scopes := []string{}
switch sch.Kind {
case expr.BasicAuthKind, expr.APIKeyKind:
sr[sch.Hash()] = []string{}
case expr.OAuth2Kind, expr.JWTKind:
sr[sch.Hash()] = req.Scopes
if len(req.Scopes) > 0 {
scopes = req.Scopes
}
}
sr[sch.Hash()] = scopes
}
srs[i] = sr
}
Expand Down

0 comments on commit 9cefb4d

Please sign in to comment.