Skip to content

Commit

Permalink
openAPI3 template T class add 2 +/- server func
Browse files Browse the repository at this point in the history
  • Loading branch information
spdrcd committed Oct 27, 2023
1 parent c446426 commit 8c01c8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions openapi3/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ func (doc *T) AddServer(server *Server) {
doc.Servers = append(doc.Servers, server)
}

func (doc *T) AddServers(server []*Server) {
doc.Servers = append(doc.Servers, server...)
}

func (doc *T) RemoveAllServers() {
doc.Servers = Servers{}
}

// Validate returns an error if T does not comply with the OpenAPI spec.
// Validations Options can be provided to modify the validation behavior.
func (doc *T) Validate(ctx context.Context, opts ...ValidationOption) error {
Expand Down
26 changes: 26 additions & 0 deletions openapi3/openapi3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/invopop/yaml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -468,3 +469,28 @@ components:
})
}
}

func TestAddRemoveServer(t *testing.T) {
testServerLines := []*Server{{URL: "test0.com"}, {URL: "test1.com"}, {URL: "test3.com"}}

doc3 := &T{
OpenAPI: "3.0.3",
Components: &Components{},
}

assert.Empty(t, doc3.Servers)

doc3.AddServer(&Server{URL: "testserver1.com"})

assert.NotEmpty(t, doc3.Servers)
assert.Len(t, doc3.Servers, 1)

doc3.RemoveAllServers()

assert.Empty(t, doc3.Servers)

doc3.AddServers(testServerLines)

assert.NotEmpty(t, doc3.Servers)
assert.Len(t, doc3.Servers, 3)
}

0 comments on commit 8c01c8b

Please sign in to comment.