Skip to content

Commit

Permalink
Add openapi:generate Meta support to dsl.Host (#3406)
Browse files Browse the repository at this point in the history
* Add Meta to expr.Host

* Add openapi:generate Meta support to dsl.Host
  • Loading branch information
tchssk committed Nov 1, 2023
1 parent 42853eb commit 041a69d
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 2 deletions.
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 Server, services, methods and file
// generated. Defaults to true. Applicable to Server, Host, services, methods and file
// servers.
//
// var _ = Service("MyService", func() {
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions expr/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion 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) {
if !mustGenerate(root.API.Servers[0].Meta) || !mustGenerate(h.Meta) {
host = ""
}

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 @@ -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) {
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
4 changes: 4 additions & 0 deletions http/codegen/openapi/v3/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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},
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 @@ -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("/")
})
})
})
}

0 comments on commit 041a69d

Please sign in to comment.