Repository: goadesign/goa
Pull request #3406 (2023-11-01 11:21:10)

Add openapi:generate Meta support to dsl.Host
Related to https://github.com/goadesign/goa/pull/3405

raphael: Thank you!

Patch:
diff --git a/dsl/meta.go b/dsl/meta.go
index 9a3a53df..cd5f884e 100644
--- a/dsl/meta.go
+++ b/dsl/meta.go
@@ -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 Server, services, methods and file
+// generated. Defaults to true. Applicable to Server, Host, services, methods and file
 // servers.
 //
 //	var _ = Service("MyService", func() {
@@ -236,6 +236,8 @@ func Meta(name string, value ...string) {
 		e.Meta = appendMeta(e.Meta, name, value...)
 	case *expr.ServerExpr:
 		e.Meta = appendMeta(e.Meta, name, value...)
+	case *expr.HostExpr:
+		e.Meta = appendMeta(e.Meta, name, value...)
 	case *expr.AttributeExpr:
 		e.Meta = appendMeta(e.Meta, name, value...)
 	case *expr.ResultTypeExpr:
diff --git a/expr/server.go b/expr/server.go
index ad32c527..8898d819 100644
--- a/expr/server.go
+++ b/expr/server.go
@@ -42,6 +42,8 @@ type (
 		URIs []URIExpr
 		// Variables defines the URI variables if any.
 		Variables *AttributeExpr
+		// Meta is a set of key/value pairs.
+		Meta MetaExpr
 	}
 
 	// URIExpr represents a parameterized URI.
diff --git a/http/codegen/openapi/v2/builder.go b/http/codegen/openapi/v2/builder.go
index 459d0357..ddfd2fcf 100644
--- a/http/codegen/openapi/v2/builder.go
+++ b/http/codegen/openapi/v2/builder.go
@@ -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) {
+	if !mustGenerate(root.API.Servers[0].Meta) || !mustGenerate(h.Meta) {
 		host = ""
 	}
 
diff --git a/http/codegen/openapi/v3/builder.go b/http/codegen/openapi/v3/builder.go
index 958003c5..ff032f9e 100644
--- a/http/codegen/openapi/v3/builder.go
+++ b/http/codegen/openapi/v3/builder.go
@@ -490,6 +490,10 @@ func buildServers(servers []*expr.ServerExpr) []*Server {
 		}
 		var server *Server
 		for _, host := range svr.Hosts {
+			if !mustGenerate(host.Meta) {
+				continue
+			}
+
 			var (
 				serverVariable   = make(map[string]*ServerVariable)
 				defaultValue     any


Test patch:
diff --git a/http/codegen/openapi/v2/files_test.go b/http/codegen/openapi/v2/files_test.go
index 005cdf13..e5b0f007 100644
--- a/http/codegen/openapi/v2/files_test.go
+++ b/http/codegen/openapi/v2/files_test.go
@@ -41,6 +41,7 @@ func TestSections(t *testing.T) {
 		{"with-map", testdata.WithMapDSL},
 		{"path-with-wildcards", testdata.PathWithWildcardDSL},
 		{"not-generate-server", testdata.NotGenerateServerDSL},
+		{"not-generate-host", testdata.NotGenerateHostDSL},
 	}
 	for _, c := range cases {
 		t.Run(c.Name, func(t *testing.T) {
diff --git a/http/codegen/openapi/v2/testdata/TestSections/not-generate-host_file0.golden b/http/codegen/openapi/v2/testdata/TestSections/not-generate-host_file0.golden
new file mode 100644
index 00000000..49331c7d
--- /dev/null
+++ b/http/codegen/openapi/v2/testdata/TestSections/not-generate-host_file0.golden
@@ -0,0 +1,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"]}}}}
\ No newline at end of file
diff --git a/http/codegen/openapi/v2/testdata/TestSections/not-generate-host_file1.golden b/http/codegen/openapi/v2/testdata/TestSections/not-generate-host_file1.golden
new file mode 100644
index 00000000..91737a72
--- /dev/null
+++ b/http/codegen/openapi/v2/testdata/TestSections/not-generate-host_file1.golden
@@ -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
diff --git a/http/codegen/openapi/v3/files_test.go b/http/codegen/openapi/v3/files_test.go
index 31f8a9db..47a46df8 100644
--- a/http/codegen/openapi/v3/files_test.go
+++ b/http/codegen/openapi/v3/files_test.go
@@ -46,6 +46,7 @@ func TestFiles(t *testing.T) {
 		{"with-tags-swagger", testdata.WithTagsSwaggerDSL},
 		{"typename", testdata.TypenameDSL},
 		{"not-generate-server", testdata.NotGenerateServerDSL},
+		{"not-generate-host", testdata.NotGenerateHostDSL},
 		// TestEndpoints
 		{"endpoint", testdata.ExtensionDSL},
 		{"endpoint-swagger", testdata.ExtensionSwaggerDSL},
diff --git a/http/codegen/openapi/v3/testdata/golden/not-generate-host_file0.golden b/http/codegen/openapi/v3/testdata/golden/not-generate-host_file0.golden
new file mode 100644
index 00000000..d6c2a6b5
--- /dev/null
+++ b/http/codegen/openapi/v3/testdata/golden/not-generate-host_file0.golden
@@ -0,0 +1,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"}]}
\ No newline at end of file
diff --git a/http/codegen/openapi/v3/testdata/golden/not-generate-host_file1.golden b/http/codegen/openapi/v3/testdata/golden/not-generate-host_file1.golden
new file mode 100644
index 00000000..8bc6e693
--- /dev/null
+++ b/http/codegen/openapi/v3/testdata/golden/not-generate-host_file1.golden
@@ -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
diff --git a/http/codegen/testdata/openapi_dsls.go b/http/codegen/testdata/openapi_dsls.go
index dae8f1f4..c3280424 100644
--- a/http/codegen/testdata/openapi_dsls.go
+++ b/http/codegen/testdata/openapi_dsls.go
@@ -737,3 +737,22 @@ var NotGenerateServerDSL = func() {
 		})
 	})
 }
+
+var NotGenerateHostDSL = 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("/")
+			})
+		})
+	})
+}