From 42853eb9f7fccf72129cde52f3d50e7b34d2bd48 Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Wed, 1 Nov 2023 12:37:31 +0900 Subject: [PATCH 1/3] Add openapi:generate Meta support to dsl.Server (#3405) * Add Meta to expr.ServerExpr * Add openapi:generate Meta support to dsl.Server --- dsl/meta.go | 4 ++- expr/server.go | 2 ++ http/codegen/openapi/v2/builder.go | 3 +++ http/codegen/openapi/v2/files_test.go | 1 + .../not-generate-server_file0.golden | 1 + .../not-generate-server_file1.golden | 26 +++++++++++++++++++ http/codegen/openapi/v3/builder.go | 3 +++ http/codegen/openapi/v3/files_test.go | 1 + .../golden/not-generate-server_file0.golden | 1 + .../golden/not-generate-server_file1.golden | 23 ++++++++++++++++ http/codegen/testdata/openapi_dsls.go | 19 ++++++++++++++ 11 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 http/codegen/openapi/v2/testdata/TestSections/not-generate-server_file0.golden create mode 100644 http/codegen/openapi/v2/testdata/TestSections/not-generate-server_file1.golden create mode 100644 http/codegen/openapi/v3/testdata/golden/not-generate-server_file0.golden create mode 100644 http/codegen/openapi/v3/testdata/golden/not-generate-server_file1.golden diff --git a/dsl/meta.go b/dsl/meta.go index 42c3b38149..9a3a53df38 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 services, methods and file +// generated. Defaults to true. Applicable to Server, services, methods and file // servers. // // var _ = Service("MyService", func() { @@ -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: diff --git a/expr/server.go b/expr/server.go index cf5f742acc..ad32c527a4 100644 --- a/expr/server.go +++ b/expr/server.go @@ -25,6 +25,8 @@ type ( Services []string // Hosts list the server hosts. Hosts []*HostExpr + // Meta is a set of key/value pairs. + Meta MetaExpr } // HostExpr describes a server host. diff --git a/http/codegen/openapi/v2/builder.go b/http/codegen/openapi/v2/builder.go index 359e2a8a7f..459d0357a1 100644 --- a/http/codegen/openapi/v2/builder.go +++ b/http/codegen/openapi/v2/builder.go @@ -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) { diff --git a/http/codegen/openapi/v2/files_test.go b/http/codegen/openapi/v2/files_test.go index c591a8f17d..005cdf136b 100644 --- a/http/codegen/openapi/v2/files_test.go +++ b/http/codegen/openapi/v2/files_test.go @@ -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) { diff --git a/http/codegen/openapi/v2/testdata/TestSections/not-generate-server_file0.golden b/http/codegen/openapi/v2/testdata/TestSections/not-generate-server_file0.golden new file mode 100644 index 0000000000..49331c7d51 --- /dev/null +++ b/http/codegen/openapi/v2/testdata/TestSections/not-generate-server_file0.golden @@ -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"]}}}} \ No newline at end of file diff --git a/http/codegen/openapi/v2/testdata/TestSections/not-generate-server_file1.golden b/http/codegen/openapi/v2/testdata/TestSections/not-generate-server_file1.golden new file mode 100644 index 0000000000..91737a7228 --- /dev/null +++ b/http/codegen/openapi/v2/testdata/TestSections/not-generate-server_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/builder.go b/http/codegen/openapi/v3/builder.go index 550349273f..958003c5c2 100644 --- a/http/codegen/openapi/v3/builder.go +++ b/http/codegen/openapi/v3/builder.go @@ -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 ( diff --git a/http/codegen/openapi/v3/files_test.go b/http/codegen/openapi/v3/files_test.go index 82fdfc6234..31f8a9db7a 100644 --- a/http/codegen/openapi/v3/files_test.go +++ b/http/codegen/openapi/v3/files_test.go @@ -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}, diff --git a/http/codegen/openapi/v3/testdata/golden/not-generate-server_file0.golden b/http/codegen/openapi/v3/testdata/golden/not-generate-server_file0.golden new file mode 100644 index 0000000000..d6c2a6b530 --- /dev/null +++ b/http/codegen/openapi/v3/testdata/golden/not-generate-server_file0.golden @@ -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"}]} \ No newline at end of file diff --git a/http/codegen/openapi/v3/testdata/golden/not-generate-server_file1.golden b/http/codegen/openapi/v3/testdata/golden/not-generate-server_file1.golden new file mode 100644 index 0000000000..8bc6e69307 --- /dev/null +++ b/http/codegen/openapi/v3/testdata/golden/not-generate-server_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 29ded6c77f..dae8f1f483 100644 --- a/http/codegen/testdata/openapi_dsls.go +++ b/http/codegen/testdata/openapi_dsls.go @@ -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("/") + }) + }) + }) +} From 041a69ddddbfe7ac838d8f35136438b2ee32585b Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Thu, 2 Nov 2023 06:38:35 +0900 Subject: [PATCH 2/3] Add openapi:generate Meta support to dsl.Host (#3406) * Add Meta to expr.Host * Add openapi:generate Meta support to dsl.Host --- dsl/meta.go | 4 ++- expr/server.go | 2 ++ http/codegen/openapi/v2/builder.go | 2 +- http/codegen/openapi/v2/files_test.go | 1 + .../not-generate-host_file0.golden | 1 + .../not-generate-host_file1.golden | 26 +++++++++++++++++++ http/codegen/openapi/v3/builder.go | 4 +++ http/codegen/openapi/v3/files_test.go | 1 + .../golden/not-generate-host_file0.golden | 1 + .../golden/not-generate-host_file1.golden | 23 ++++++++++++++++ http/codegen/testdata/openapi_dsls.go | 19 ++++++++++++++ 11 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 http/codegen/openapi/v2/testdata/TestSections/not-generate-host_file0.golden create mode 100644 http/codegen/openapi/v2/testdata/TestSections/not-generate-host_file1.golden create mode 100644 http/codegen/openapi/v3/testdata/golden/not-generate-host_file0.golden create mode 100644 http/codegen/openapi/v3/testdata/golden/not-generate-host_file1.golden diff --git a/dsl/meta.go b/dsl/meta.go index 9a3a53df38..cd5f884e53 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 ad32c527a4..8898d8194c 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 459d0357a1..ddfd2fcfde 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/v2/files_test.go b/http/codegen/openapi/v2/files_test.go index 005cdf136b..e5b0f007f9 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 0000000000..49331c7d51 --- /dev/null +++ b/http/codegen/openapi/v2/testdata/TestSections/not-generate-host_file0.golden @@ -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"]}}}} \ 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 0000000000..91737a7228 --- /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/builder.go b/http/codegen/openapi/v3/builder.go index 958003c5c2..ff032f9e8f 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 diff --git a/http/codegen/openapi/v3/files_test.go b/http/codegen/openapi/v3/files_test.go index 31f8a9db7a..47a46df88a 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 0000000000..d6c2a6b530 --- /dev/null +++ b/http/codegen/openapi/v3/testdata/golden/not-generate-host_file0.golden @@ -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"}]} \ 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 0000000000..8bc6e69307 --- /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 dae8f1f483..c3280424b5 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("/") + }) + }) + }) +} From 86ae5d5defea2dfba010652a405a5b6ab8adbd6b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:32:08 -0700 Subject: [PATCH 3/3] Bump github.com/google/uuid from 1.3.1 to 1.4.0 (#3404) Bumps [github.com/google/uuid](https://github.com/google/uuid) from 1.3.1 to 1.4.0. - [Release notes](https://github.com/google/uuid/releases) - [Changelog](https://github.com/google/uuid/blob/master/CHANGELOG.md) - [Commits](https://github.com/google/uuid/compare/v1.3.1...v1.4.0) --- updated-dependencies: - dependency-name: github.com/google/uuid dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Raphael Simon --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index b08d193be5..36152d962f 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/dimfeld/httppath v0.0.0-20170720192232-ee938bf73598 github.com/getkin/kin-openapi v0.120.0 github.com/go-chi/chi/v5 v5.0.10 - github.com/google/uuid v1.3.1 + github.com/google/uuid v1.4.0 github.com/gorilla/websocket v1.5.0 github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index eb646c70cb..f5e9d01654 100644 --- a/go.sum +++ b/go.sum @@ -21,8 +21,8 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= -github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=