Skip to content

Commit c7b796d

Browse files
NerzalTobias Theel
andauthored
V2: Add Servers param (#1573)
* Fix template delims * go mod tidy * Add basic implementation of the servers object --------- Co-authored-by: Tobias Theel <[email protected]>
1 parent 9a872fb commit c7b796d

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,14 @@ When a short string in your documentation is insufficient, or you need images, c
407407
| tag.name | Name of a tag.| // @tag.name This is the name of the tag |
408408
| tag.description.markdown | Description of the tag this is an alternative to tag.description. The description will be read from a file named like tagname.md | // @tag.description.markdown |
409409
410+
## Open API V3.1.0+
411+
412+
The following annotations are only available if you set the -v3.1 flag in the CLI.
413+
414+
| annotation | description | example |
415+
|-------------|--------------------------------------------|---------------------------------|
416+
| servers.url | The URL of a server| // @servers.url https://petstore.example.com/api/v1 |
417+
| servers.description | The description of a server| // @servers.description Production API |
410418
411419
## API Operation
412420

parserv3.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,22 @@ func (p *Parser) parseGeneralAPIInfoV3(comments []string) error {
164164
}
165165

166166
p.openAPI.Info.Extensions[originalAttribute[1:]] = valueJSON
167+
case "@servers.url":
168+
server := spec.NewServer()
169+
server.Spec.URL = value
170+
171+
p.openAPI.Servers = append(p.openAPI.Servers, server)
172+
case "@servers.description":
173+
server := p.openAPI.Servers[len(p.openAPI.Servers)-1]
174+
server.Spec.Description = value
175+
case "@servers.variables.enum":
176+
p.debug.Printf("not yet implemented: @servers.variables.enum")
177+
case "@servers.variables.default":
178+
p.debug.Printf("not yet implemented: @servers.variables.default")
179+
case "@servers.variables.description":
180+
p.debug.Printf("not yet implemented: @servers.variables.description")
181+
case "@servers.variables.description.markdown":
182+
p.debug.Printf("not yet implemented: @servers.variables.description.markdown")
167183
default:
168184
if strings.HasPrefix(attribute, "@x-") {
169185
err := p.parseExtensionsV3(value, attribute)

parserv3_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
910
)
1011

1112
func TestOverridesGetTypeSchemaV3(t *testing.T) {
@@ -370,3 +371,25 @@ func TestParseSimpleApiV3(t *testing.T) {
370371
assert.NotNil(t, path.RequestBody)
371372
//TODO add asserts
372373
}
374+
375+
func TestParserParseServers(t *testing.T) {
376+
t.Parallel()
377+
378+
searchDir := "testdata/v3/servers"
379+
p := New(GenerateOpenAPI3Doc(true))
380+
p.PropNamingStrategy = PascalCase
381+
382+
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
383+
assert.NoError(t, err)
384+
385+
servers := p.openAPI.Servers
386+
require.NotNil(t, servers)
387+
388+
assert.Equal(t, 2, len(servers))
389+
assert.Equal(t, "https://test.petstore.com/v3", servers[0].Spec.URL)
390+
assert.Equal(t, "Test Petstore server.", servers[0].Spec.Description)
391+
392+
assert.Equal(t, "https://petstore.com/v3", servers[1].Spec.URL)
393+
assert.Equal(t, "Production Petstore server.", servers[1].Spec.Description)
394+
395+
}

testdata/v3/servers/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/swaggo/swag/v2/testdata/v3/simple/api"
7+
)
8+
9+
// @title Swagger Example API
10+
// @version 1.0
11+
// @description This is a sample server Petstore server.
12+
// @termsOfService http://swagger.io/terms/
13+
14+
// @contact.name API Support
15+
// @contact.url http://www.swagger.io/support
16+
// @contact.email [email protected]
17+
18+
// @license.name Apache 2.0
19+
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
20+
21+
// @servers.url https://test.petstore.com/v3
22+
// @servers.description Test Petstore server.
23+
24+
// @servers.url https://petstore.com/v3
25+
// @servers.description Production Petstore server.
26+
func main() {
27+
http.HandleFunc("/testapi/get-string-by-int/", api.GetStringByInt)
28+
http.HandleFunc("/testapi/get-struct-array-by-string/", api.GetStructArrayByString)
29+
http.HandleFunc("/testapi/upload", api.Upload)
30+
31+
http.ListenAndServe(":8080", nil)
32+
}

0 commit comments

Comments
 (0)