Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat servers variables of current spec #1612

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions parserv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/token"
"net/http"
"reflect"
"regexp"
"sort"
"strings"

Expand All @@ -32,6 +33,11 @@ func (p *Parser) GetOpenAPI() *spec.OpenAPI {
return p.openAPI
}

var (
serversURLPattern = regexp.MustCompile(`\{([^}]+)\}`)
serversVariablesPattern = regexp.MustCompile(`^(\w+)\s+(.+)$`)
)

func (p *Parser) parseGeneralAPIInfoV3(comments []string) error {
previousAttribute := ""

Expand Down Expand Up @@ -167,19 +173,64 @@ func (p *Parser) parseGeneralAPIInfoV3(comments []string) error {
case "@servers.url":
server := spec.NewServer()
server.Spec.URL = value
matches := serversURLPattern.FindAllStringSubmatch(value, -1)
server.Spec.Variables = make(map[string]*spec.Extendable[spec.ServerVariable])
for _, match := range matches {
server.Spec.Variables[match[1]] = spec.NewServerVariable()
}

p.openAPI.Servers = append(p.openAPI.Servers, server)
case "@servers.description":
server := p.openAPI.Servers[len(p.openAPI.Servers)-1]
server.Spec.Description = value
case "@servers.variables.enum":
p.debug.Printf("not yet implemented: @servers.variables.enum")
server := p.openAPI.Servers[len(p.openAPI.Servers)-1]
matches := serversVariablesPattern.FindStringSubmatch(value)
if len(matches) > 0 {
variable, ok := server.Spec.Variables[matches[1]]
if !ok {
p.debug.Printf("Variables are not detected.")
continue
}
variable.Spec.Enum = append(variable.Spec.Enum, matches[2])
}
case "@servers.variables.default":
p.debug.Printf("not yet implemented: @servers.variables.default")
server := p.openAPI.Servers[len(p.openAPI.Servers)-1]
matches := serversVariablesPattern.FindStringSubmatch(value)
if len(matches) > 0 {
variable, ok := server.Spec.Variables[matches[1]]
if !ok {
p.debug.Printf("Variables are not detected.")
continue
}
variable.Spec.Default = matches[2]
}
case "@servers.variables.description":
p.debug.Printf("not yet implemented: @servers.variables.description")
server := p.openAPI.Servers[len(p.openAPI.Servers)-1]
matches := serversVariablesPattern.FindStringSubmatch(value)
if len(matches) > 0 {
variable, ok := server.Spec.Variables[matches[1]]
if !ok {
p.debug.Printf("Variables are not detected.")
continue
}
variable.Spec.Default = matches[2]
}
case "@servers.variables.description.markdown":
p.debug.Printf("not yet implemented: @servers.variables.description.markdown")
server := p.openAPI.Servers[len(p.openAPI.Servers)-1]
matches := serversVariablesPattern.FindStringSubmatch(value)
if len(matches) > 0 {
variable, ok := server.Spec.Variables[matches[1]]
if !ok {
p.debug.Printf("Variables are not detected.")
continue
}
commentInfo, err := getMarkdownForTag(matches[1], p.markdownFileDir)
if err != nil {
return err
}
variable.Spec.Description = string(commentInfo)
}
default:
if strings.HasPrefix(attribute, "@x-") {
err := p.parseExtensionsV3(value, attribute)
Expand Down
7 changes: 6 additions & 1 deletion parserv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,14 @@ func TestParserParseServers(t *testing.T) {
require.NotNil(t, servers)

assert.Equal(t, 2, len(servers))
assert.Equal(t, "https://test.petstore.com/v3", servers[0].Spec.URL)
assert.Equal(t, "{scheme}://{host}:{port}", servers[0].Spec.URL)
assert.Equal(t, "Test Petstore server.", servers[0].Spec.Description)

assert.Equal(t, "https", servers[0].Spec.Variables["scheme"].Spec.Default)
assert.Equal(t, []string{"http", "https"}, servers[0].Spec.Variables["scheme"].Spec.Enum)
assert.Equal(t, "test.petstore.com", servers[0].Spec.Variables["host"].Spec.Default)
assert.Equal(t, "443", servers[0].Spec.Variables["port"].Spec.Default)

assert.Equal(t, "https://petstore.com/v3", servers[1].Spec.URL)
assert.Equal(t, "Production Petstore server.", servers[1].Spec.Description)

Expand Down
7 changes: 6 additions & 1 deletion testdata/v3/servers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ import (
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @servers.url https://test.petstore.com/v3
// @servers.url {scheme}://{host}:{port}
// @servers.description Test Petstore server.
// @servers.variables.enum scheme http
// @servers.variables.enum scheme https
// @servers.variables.default scheme https
// @servers.variables.default host test.petstore.com
// @servers.variables.default port 443

// @servers.url https://petstore.com/v3
// @servers.description Production Petstore server.
Expand Down