Skip to content

Commit

Permalink
Merge branch 'v3' into fix/openapi_format
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael committed Jan 21, 2024
2 parents 9cefb4d + ea1451f commit 0011690
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 49 deletions.
8 changes: 4 additions & 4 deletions http/codegen/openapi/json_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func TypeSchemaWithPrefix(api *expr.APIExpr, t expr.DataType, prefix string) *Sc
case *expr.Object:
s.Type = Object
for _, nat := range *actual {
if !mustGenerate(nat.Attribute.Meta) {
if !MustGenerate(nat.Attribute.Meta) {
continue
}
prop := NewSchema()
Expand Down Expand Up @@ -529,7 +529,7 @@ func initAttributeValidation(s *Schema, at *expr.AttributeExpr) {
}
for _, v := range val.Required {
if a := at.Find(v); a != nil {
if !mustGenerate(a.Meta) {
if !MustGenerate(a.Meta) {
continue
}
}
Expand Down Expand Up @@ -580,9 +580,9 @@ func buildResultTypeSchema(api *expr.APIExpr, mt *expr.ResultTypeExpr, view stri
buildAttributeSchema(api, s, projected.AttributeExpr)
}

// mustGenerate returns true if the meta indicates that a OpenAPI specification should be
// MustGenerate returns true if the meta indicates that a OpenAPI specification should be
// generated, false otherwise.
func mustGenerate(meta expr.MetaExpr) bool {
func MustGenerate(meta expr.MetaExpr) bool {
m, ok := meta.Last("openapi:generate")
if !ok {
m, ok = meta.Last("swagger:generate")
Expand Down
27 changes: 7 additions & 20 deletions http/codegen/openapi/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewV2(root *expr.RootExpr, h *expr.HostExpr) (*V2, error) {
return nil, fmt.Errorf("failed to parse server URL: %s", err)
}
host := u.Host
if !mustGenerate(root.API.Servers[0].Meta) || !mustGenerate(h.Meta) {
if !openapi.MustGenerate(root.API.Servers[0].Meta) || !openapi.MustGenerate(h.Meta) {
host = ""
}

Expand Down Expand Up @@ -63,20 +63,20 @@ func NewV2(root *expr.RootExpr, h *expr.HostExpr) (*V2, error) {
ExternalDocs: openapi.DocsFromExpr(root.API.Docs, root.API.Meta),
}
for _, res := range root.API.HTTP.Services {
if !mustGenerate(res.Meta) || !mustGenerate(res.ServiceExpr.Meta) {
if !openapi.MustGenerate(res.Meta) || !openapi.MustGenerate(res.ServiceExpr.Meta) {
continue
}
for k, v := range openapi.ExtensionsFromExpr(res.Meta) {
s.Paths[k] = v
}
for _, fs := range res.FileServers {
if !mustGenerate(fs.Meta) || !mustGenerate(fs.Service.Meta) {
if !openapi.MustGenerate(fs.Meta) || !openapi.MustGenerate(fs.Service.Meta) {
continue
}
buildPathFromFileServer(s, root, fs)
}
for _, a := range res.HTTPEndpoints {
if !mustGenerate(a.Meta) || !mustGenerate(a.MethodExpr.Meta) {
if !openapi.MustGenerate(a.Meta) || !openapi.MustGenerate(a.MethodExpr.Meta) {
continue
}
for _, route := range a.Routes {
Expand Down Expand Up @@ -118,19 +118,6 @@ func defaultURI(h *expr.HostExpr) string {
return uri
}

// mustGenerate returns true if the meta indicates that a OpenAPI specification should be
// generated, false otherwise.
func mustGenerate(meta expr.MetaExpr) bool {
m, ok := meta.Last("openapi:generate")
if !ok {
m, ok = meta.Last("swagger:generate")
}
if ok && m == "false" {
return false
}
return true
}

// addScopeDescription generates and adds required scopes to the scheme's description.
func addScopeDescription(scopes []*expr.ScopeExpr, sd *SecurityDefinition) {
// Generate scopes to add to description
Expand Down Expand Up @@ -216,18 +203,18 @@ func securitySpecFromExpr(root *expr.RootExpr) map[string]*SecurityDefinition {
func hasAbsoluteRoutes(root *expr.RootExpr) bool {
hasAbsoluteRoutes := false
for _, res := range root.API.HTTP.Services {
if !mustGenerate(res.Meta) || !mustGenerate(res.ServiceExpr.Meta) {
if !openapi.MustGenerate(res.Meta) || !openapi.MustGenerate(res.ServiceExpr.Meta) {
continue
}
for _, fs := range res.FileServers {
if !mustGenerate(fs.Meta) || !mustGenerate(fs.Service.Meta) {
if !openapi.MustGenerate(fs.Meta) || !openapi.MustGenerate(fs.Service.Meta) {
continue
}
hasAbsoluteRoutes = true
break
}
for _, a := range res.HTTPEndpoints {
if !mustGenerate(a.Meta) || !mustGenerate(a.MethodExpr.Meta) {
if !openapi.MustGenerate(a.Meta) || !openapi.MustGenerate(a.MethodExpr.Meta) {
continue
}
for _, ro := range a.Routes {
Expand Down
27 changes: 7 additions & 20 deletions http/codegen/openapi/v3/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func buildComponents(root *expr.RootExpr, types map[string]*openapi.Schema) *Com
func buildPaths(h *expr.HTTPExpr, bodies map[string]map[string]*EndpointBodies, api *expr.APIExpr) map[string]*PathItem {
var paths = make(map[string]*PathItem)
for _, svc := range h.Services {
if !mustGenerate(svc.Meta) || !mustGenerate(svc.ServiceExpr.Meta) {
if !openapi.MustGenerate(svc.Meta) || !openapi.MustGenerate(svc.ServiceExpr.Meta) {
continue
}

Expand All @@ -128,7 +128,7 @@ func buildPaths(h *expr.HTTPExpr, bodies map[string]map[string]*EndpointBodies,

// endpoints
for _, e := range svc.HTTPEndpoints {
if !mustGenerate(e.Meta) || !mustGenerate(e.MethodExpr.Meta) {
if !openapi.MustGenerate(e.Meta) || !openapi.MustGenerate(e.MethodExpr.Meta) {
continue
}

Expand Down Expand Up @@ -172,7 +172,7 @@ func buildPaths(h *expr.HTTPExpr, bodies map[string]map[string]*EndpointBodies,

// file servers
for _, f := range svc.FileServers {
if !mustGenerate(f.Meta) || !mustGenerate(f.Service.Meta) {
if !openapi.MustGenerate(f.Meta) || !openapi.MustGenerate(f.Service.Meta) {
continue
}

Expand Down Expand Up @@ -482,12 +482,12 @@ func parseOperationIDTemplate(template, service, method string, routeIndex int)
func buildServers(servers []*expr.ServerExpr) []*Server {
var svrs []*Server
for _, svr := range servers {
if !mustGenerate(svr.Meta) {
if !openapi.MustGenerate(svr.Meta) {
continue
}
var server *Server
for _, host := range svr.Hosts {
if !mustGenerate(host.Meta) {
if !openapi.MustGenerate(host.Meta) {
continue
}

Expand Down Expand Up @@ -642,7 +642,7 @@ func buildTags(api *expr.APIExpr) []*openapi.Tag {
m[t.Name] = t
}
for _, s := range api.HTTP.Services {
if !mustGenerate(s.Meta) || !mustGenerate(s.ServiceExpr.Meta) {
if !openapi.MustGenerate(s.Meta) || !openapi.MustGenerate(s.ServiceExpr.Meta) {
continue
}
for _, t := range openapi.TagsFromExpr(s.Meta) {
Expand All @@ -667,7 +667,7 @@ func buildTags(api *expr.APIExpr) []*openapi.Tag {
// add service name and description to the tags since we tag every
// operation with service name when no custom tag is defined
for _, s := range api.HTTP.Services {
if !mustGenerate(s.Meta) || !mustGenerate(s.ServiceExpr.Meta) {
if !openapi.MustGenerate(s.Meta) || !openapi.MustGenerate(s.ServiceExpr.Meta) {
continue
}
tags = append(tags, &openapi.Tag{
Expand All @@ -679,16 +679,3 @@ func buildTags(api *expr.APIExpr) []*openapi.Tag {
}
return tags
}

// mustGenerate returns true if the meta indicates that a OpenAPI specification should be
// generated, false otherwise.
func mustGenerate(meta expr.MetaExpr) bool {
m, ok := meta.Last("openapi:generate")
if !ok {
m, ok = meta.Last("swagger:generate")
}
if ok && m == "false" {
return false
}
return true
}
10 changes: 5 additions & 5 deletions http/codegen/openapi/v3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func buildBodyTypes(api *expr.APIExpr) (map[string]map[string]*EndpointBodies, m
}

for _, s := range api.HTTP.Services {
if !mustGenerate(s.Meta) || !mustGenerate(s.ServiceExpr.Meta) {
if !openapi.MustGenerate(s.Meta) || !openapi.MustGenerate(s.ServiceExpr.Meta) {
continue
}

sbodies := make(map[string]*EndpointBodies, len(s.HTTPEndpoints))
for _, e := range s.HTTPEndpoints {
if !mustGenerate(e.Meta) || !mustGenerate(e.MethodExpr.Meta) {
if !openapi.MustGenerate(e.Meta) || !openapi.MustGenerate(e.MethodExpr.Meta) {
continue
}

Expand Down Expand Up @@ -187,7 +187,7 @@ func (sf *schemafier) schemafy(attr *expr.AttributeExpr, noref ...bool) *openapi
s.Type = openapi.Object
var itemNotes []string
for _, nat := range *t {
if !mustGenerate(nat.Attribute.Meta) {
if !openapi.MustGenerate(nat.Attribute.Meta) {
continue
}
s.Properties[nat.Name] = sf.schemafy(nat.Attribute)
Expand Down Expand Up @@ -297,7 +297,7 @@ func (sf *schemafier) schemafy(attr *expr.AttributeExpr, noref ...bool) *openapi
}
for _, v := range val.Required {
if a := attr.Find(v); a != nil {
if !mustGenerate(a.Meta) {
if !openapi.MustGenerate(a.Meta) {
continue
}
}
Expand Down Expand Up @@ -393,7 +393,7 @@ func hashAttribute(att *expr.AttributeExpr, h hash.Hash64, seen map[string]*uint
case expr.ObjectKind:
o := expr.AsObject(t)
for _, m := range *o {
if !mustGenerate(m.Attribute.Meta) {
if !openapi.MustGenerate(m.Attribute.Meta) {
continue
}
kh := hashString(m.Name, h)
Expand Down

0 comments on commit 0011690

Please sign in to comment.