Skip to content

Commit

Permalink
Add openapi:generate Meta support to dsl.Server
Browse files Browse the repository at this point in the history
  • Loading branch information
tchssk committed Oct 30, 2023
1 parent 8379f79 commit cc6e7b9
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dsl/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ import (
// - "swagger:generate" DEPRECATED, use "openapi:generate" instead.
//
// - "openapi:generate" specifies whether OpenAPI specification should be
// generated. Defaults to true. Applicable to services, methods and file
// generated. Defaults to true. Applicable to Server, services, methods and file
// servers.
//
// var _ = Service("MyService", func() {
Expand Down Expand Up @@ -234,6 +234,8 @@ func Meta(name string, value ...string) {
switch e := eval.Current().(type) {
case *expr.APIExpr:
e.Meta = appendMeta(e.Meta, name, value...)
case *expr.ServerExpr:
e.Meta = appendMeta(e.Meta, name, value...)
case *expr.AttributeExpr:
e.Meta = appendMeta(e.Meta, name, value...)
case *expr.ResultTypeExpr:
Expand Down
3 changes: 3 additions & 0 deletions http/codegen/openapi/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ 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) {
host = ""
}

basePath := root.API.HTTP.Path
if hasAbsoluteRoutes(root) {
Expand Down
1 change: 1 addition & 0 deletions http/codegen/openapi/v2/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestSections(t *testing.T) {
{"with-spaces", testdata.WithSpacesDSL},
{"with-map", testdata.WithMapDSL},
{"path-with-wildcards", testdata.PathWithWildcardDSL},
{"not-generate-server", testdata.NotGenerateServerDSL},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"swagger":"2.0","info":{"title":"","version":""},"consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/":{"get":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","responses":{"200":{"description":"OK response.","schema":{"type":"string"}}},"schemes":["https"]}}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
swagger: "2.0"
info:
title: ""
version: ""
consumes:
- application/json
- application/xml
- application/gob
produces:
- application/json
- application/xml
- application/gob
paths:
/:
get:
tags:
- testService
summary: testEndpoint testService
operationId: testService#testEndpoint
responses:
"200":
description: OK response.
schema:
type: string
schemes:
- https
3 changes: 3 additions & 0 deletions http/codegen/openapi/v3/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ 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) {
continue
}
var server *Server
for _, host := range svr.Hosts {
var (
Expand Down
1 change: 1 addition & 0 deletions http/codegen/openapi/v3/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestFiles(t *testing.T) {
{"with-tags", testdata.WithTagsDSL},
{"with-tags-swagger", testdata.WithTagsSwaggerDSL},
{"typename", testdata.TypenameDSL},
{"not-generate-server", testdata.NotGenerateServerDSL},
// TestEndpoints
{"endpoint", testdata.ExtensionDSL},
{"endpoint-swagger", testdata.ExtensionSwaggerDSL},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"openapi":"3.0.3","info":{"title":"Goa API","version":"1.0"},"paths":{"/":{"get":{"tags":["testService"],"summary":"testEndpoint testService","operationId":"testService#testEndpoint","responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Beatae non id consequatur."},"example":"Aut sed ducimus repudiandae sit explicabo asperiores."}}}}}}},"components":{},"tags":[{"name":"testService"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
openapi: 3.0.3
info:
title: Goa API
version: "1.0"
paths:
/:
get:
tags:
- testService
summary: testEndpoint testService
operationId: testService#testEndpoint
responses:
"200":
description: OK response.
content:
application/json:
schema:
type: string
example: Beatae non id consequatur.
example: Aut sed ducimus repudiandae sit explicabo asperiores.
components: {}
tags:
- name: testService
19 changes: 19 additions & 0 deletions http/codegen/testdata/openapi_dsls.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,22 @@ var SkipResponseBodyEncodeDecodeDSL = func() {
})
})
}

var NotGenerateServerDSL = func() {
var _ = API("test", func() {
Server("test", func() {
Host("localhost", func() {
URI("https://goa.design")
})
Meta("openapi:generate", "false")
})
})
Service("testService", func() {
Method("testEndpoint", func() {
Result(String)
HTTP(func() {
GET("/")
})
})
})
}

0 comments on commit cc6e7b9

Please sign in to comment.