Skip to content

Commit

Permalink
feat: support boolean for status code response
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy committed Sep 9, 2024
1 parent 6c2a961 commit 8ebafea
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 73 deletions.
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
module github.com/speakeasy-api/sdk-gen-config

go 1.21.5

toolchain go1.22.0
go 1.23

require (
github.com/AlekSi/pointer v1.2.0
github.com/daveshanley/vacuum v0.9.12
github.com/google/uuid v1.5.0
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.9.0
github.com/wk8/go-ordered-map/v2 v2.1.8
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -26,9 +25,11 @@ require (
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.6.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

// Can be removed once https://github.com/wk8/go-ordered-map/pull/41 is merged and the dependency updated in libopenapi
replace github.com/wk8/go-ordered-map/v2 => github.com/speakeasy-api/go-ordered-map/v2 v2.0.0-20240813202817-2f1629387283
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/speakeasy-api/go-ordered-map/v2 v2.0.0-20240813202817-2f1629387283 h1:QPZc0Mne/K4/R0giVVay0YODjMwP/BMSpYnQm5kWBgE=
github.com/speakeasy-api/go-ordered-map/v2 v2.0.0-20240813202817-2f1629387283/go.mod h1:DbzwytT4g/odXquuOCqroKvtxxldI4nb3nuesHF/Exo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
74 changes: 22 additions & 52 deletions io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"path/filepath"
"testing"

"github.com/speakeasy-api/sdk-gen-config/testutils"
"github.com/speakeasy-api/sdk-gen-config/workspace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const testDir = "gen/test"
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestLoad_Success(t *testing.T) {
langs: []string{"go"},
configDir: testDir,
targetDir: testDir,
genYaml: readTestFile(t, "pre-v100-gen.yaml"),
genYaml: testutils.ReadTestFile(t, "pre-v100-gen.yaml"),
},
want: &Config{
Config: &Configuration{
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestLoad_Success(t *testing.T) {
langs: []string{"go"},
configDir: testDir,
targetDir: testDir,
genYaml: readTestFile(t, "v100-gen.yaml"),
genYaml: testutils.ReadTestFile(t, "v100-gen.yaml"),
configSubDir: ".speakeasy",
},
want: &Config{
Expand Down Expand Up @@ -187,8 +187,8 @@ func TestLoad_Success(t *testing.T) {
langs: []string{"go"},
configDir: testDir,
targetDir: testDir,
genYaml: readTestFile(t, "v200-gen.yaml"),
lockFile: readTestFile(t, "v200-gen.lock"),
genYaml: testutils.ReadTestFile(t, "v200-gen.yaml"),
lockFile: testutils.ReadTestFile(t, "v200-gen.lock"),
configSubDir: ".speakeasy",
},
want: &Config{
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestLoad_Success(t *testing.T) {
langs: []string{"go"},
configDir: testDir,
targetDir: testDir,
genYaml: readTestFile(t, "v200-gen.yaml"),
genYaml: testutils.ReadTestFile(t, "v200-gen.yaml"),
configSubDir: ".speakeasy",
},
want: &Config{
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestLoad_Success(t *testing.T) {
langs: []string{"go"},
configDir: testDir,
targetDir: testDir,
genYaml: readTestFile(t, "v200-gen.yaml"),
genYaml: testutils.ReadTestFile(t, "v200-gen.yaml"),
configSubDir: ".gen",
},
want: &Config{
Expand Down Expand Up @@ -343,8 +343,8 @@ func TestLoad_Success(t *testing.T) {
langs: []string{"go"},
configDir: filepath.Dir(testDir),
targetDir: testDir,
genYaml: readTestFile(t, "v200-gen.yaml"),
lockFile: readTestFile(t, "v200-gen.lock"),
genYaml: testutils.ReadTestFile(t, "v200-gen.yaml"),
lockFile: testutils.ReadTestFile(t, "v200-gen.lock"),
},
want: &Config{
Config: &Configuration{
Expand Down Expand Up @@ -398,7 +398,7 @@ func TestLoad_Success(t *testing.T) {
langs: []string{"go", "typescript"},
configDir: testDir,
targetDir: testDir,
genYaml: readTestFile(t, "v100-gen.yaml"),
genYaml: testutils.ReadTestFile(t, "v100-gen.yaml"),
},
want: &Config{
Config: &Configuration{
Expand Down Expand Up @@ -457,8 +457,8 @@ func TestLoad_Success(t *testing.T) {
langs: []string{"go", "typescript"},
configDir: testDir,
targetDir: testDir,
genYaml: readTestFile(t, "v200-gen.yaml"),
lockFile: readTestFile(t, "v200-gen.lock"),
genYaml: testutils.ReadTestFile(t, "v200-gen.yaml"),
lockFile: testutils.ReadTestFile(t, "v200-gen.lock"),
configSubDir: ".speakeasy",
},
want: &Config{
Expand Down Expand Up @@ -518,8 +518,8 @@ func TestLoad_Success(t *testing.T) {
langs: []string{"go"},
configDir: testDir,
targetDir: testDir,
genYaml: readTestFile(t, "v200-generation-mockserver-disabled.yaml"),
lockFile: readTestFile(t, "v200-gen.lock"),
genYaml: testutils.ReadTestFile(t, "v200-generation-mockserver-disabled.yaml"),
lockFile: testutils.ReadTestFile(t, "v200-gen.lock"),
configSubDir: ".speakeasy",
},
want: &Config{
Expand Down Expand Up @@ -585,11 +585,8 @@ func TestLoad_Success(t *testing.T) {
lockFileDir = filepath.Join(targetDir, ".speakeasy")
}

err := createTempFile(configDir, "gen.yaml", tt.args.genYaml)
require.NoError(t, err)

err = createTempFile(lockFileDir, "gen.lock", tt.args.lockFile)
require.NoError(t, err)
testutils.CreateTempFile(t, configDir, "gen.yaml", tt.args.genYaml)
testutils.CreateTempFile(t, lockFileDir, "gen.lock", tt.args.lockFile)

defer os.RemoveAll(configDir)
defer os.RemoveAll(targetDir)
Expand Down Expand Up @@ -620,13 +617,11 @@ func TestLoad_BackwardsCompatibility_Success(t *testing.T) {

// Create new config file in .speakeasy dir
speakeasyDir := filepath.Join(os.TempDir(), testDir, workspace.SpeakeasyFolder)
err := createTempFile(speakeasyDir, "gen.yaml", readTestFile(t, "v200-gen.yaml"))
require.NoError(t, err)
testutils.CreateTempFile(t, speakeasyDir, "gen.yaml", testutils.ReadTestFile(t, "v200-gen.yaml"))

// Create old config file in root dir
rootDir := filepath.Join(os.TempDir(), testDir)
err = createTempFile(rootDir, "gen.yaml", readTestFile(t, "v100-gen.yaml"))
require.NoError(t, err)
testutils.CreateTempFile(t, rootDir, "gen.yaml", testutils.ReadTestFile(t, "v100-gen.yaml"))

defer os.RemoveAll(speakeasyDir)
defer os.RemoveAll(rootDir)
Expand Down Expand Up @@ -722,7 +717,7 @@ func TestSaveConfig(t *testing.T) {
speakeasyPath := filepath.Join(tempDir, ".speakeasy")
configPath := filepath.Join(speakeasyPath, "gen.yaml")

err := os.Mkdir(speakeasyPath, 0755)
err := os.Mkdir(speakeasyPath, 0o755)
assert.NoError(t, err)

err = SaveConfig(tempDir, testCase.cfg, testCase.opts...)
Expand All @@ -738,7 +733,7 @@ func TestSaveConfig(t *testing.T) {

assert.NoError(t, err)
assert.Equal(t, false, fileInfo.IsDir())
assert.Equal(t, fs.FileMode(0644), fileInfo.Mode())
assert.Equal(t, fs.FileMode(0o644), fileInfo.Mode())

contents, err := os.ReadFile(configPath)
assert.NoError(t, err)
Expand Down Expand Up @@ -782,7 +777,7 @@ management: {}
speakeasyPath := filepath.Join(tempDir, ".speakeasy")
configPath := filepath.Join(speakeasyPath, "gen.lock")

err := os.Mkdir(speakeasyPath, 0755)
err := os.Mkdir(speakeasyPath, 0o755)
assert.NoError(t, err)

err = SaveLockFile(tempDir, testCase.lf, testCase.opts...)
Expand All @@ -798,36 +793,11 @@ management: {}

assert.NoError(t, err)
assert.Equal(t, false, fileInfo.IsDir())
assert.Equal(t, fs.FileMode(0644), fileInfo.Mode())
assert.Equal(t, fs.FileMode(0o644), fileInfo.Mode())

contents, err := os.ReadFile(configPath)
assert.NoError(t, err)
assert.Equal(t, testCase.expected, contents)
})
}
}

func createTempFile(dir string, fileName, contents string) error {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}

if contents != "" {
tmpFile := filepath.Join(dir, fileName)
if err := os.WriteFile(tmpFile, []byte(contents), os.ModePerm); err != nil {
return err
}
}

return nil
}

func readTestFile(t *testing.T, file string) string {
t.Helper()
data, err := os.ReadFile(filepath.Join("testdata", file))
if err != nil {
t.Fatal(err)
}

return string(data)
}
69 changes: 54 additions & 15 deletions tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ const (
)

type Tests struct {
Version string `yaml:"testsVersion"`
Tests map[string][]Test `yaml:"tests"`
Version string `yaml:"testsVersion"`
Tests *orderedmap.OrderedMap[string, []Test] `yaml:"tests"`
}

type Test struct {
Name string `yaml:"name"`
Description string `yaml:"description,omitempty"`
Targets []string `yaml:"targets,omitempty"`
Server string `yaml:"server,omitempty"`
Security yaml.Node `yaml:"security,omitempty"`
Parameters *Parameters `yaml:"parameters,omitempty"`
RequestBody *orderedmap.OrderedMap[string, yaml.Node] `yaml:"requestBody,omitempty"`
Responses *orderedmap.OrderedMap[string, *orderedmap.OrderedMap[string, yaml.Node]] `yaml:"responses,omitempty"`
Name string `yaml:"name"`
Description string `yaml:"description,omitempty"`
Targets []string `yaml:"targets,omitempty"`
Server string `yaml:"server,omitempty"`
Security yaml.Node `yaml:"security,omitempty"`
Parameters *Parameters `yaml:"parameters,omitempty"`
RequestBody *orderedmap.OrderedMap[string, yaml.Node] `yaml:"requestBody,omitempty"`
Responses *orderedmap.OrderedMap[string, yaml.Node] `yaml:"responses,omitempty"`

// Internal use only
InternalID string `yaml:"internalId,omitempty"`
Expand Down Expand Up @@ -72,7 +72,7 @@ func (t Tests) Validate() error {
return fmt.Errorf("unsupported tests version: %s", t.Version)
}

for operationID, tests := range t.Tests {
for operationID, tests := range t.Tests.FromOldest() {
if operationID == "" {
return fmt.Errorf("empty operationId found")
}
Expand All @@ -88,15 +88,32 @@ func (t Tests) Validate() error {
return fmt.Errorf("test %s has more than one request body", name)
}

if test.Responses.Len() == 0 {
return fmt.Errorf("test %s should defined at least one response", name)
}

if test.Responses.Len() > 1 {
return fmt.Errorf("test %s has more than one response code", name)
}

if test.Responses != nil {
for pair := test.Responses.Oldest(); pair != nil; pair = pair.Next() {
responseBody := pair.Value
if responseBody.Len() > 1 {
return fmt.Errorf("test %s has more than one response body", name)
for _, responseBody := range test.Responses.FromOldest() {
switch responseBody.Kind {
case yaml.ScalarNode:
if responseBody.Tag != "!!bool" {
return fmt.Errorf("test %s has invalid response body", name)
}
case yaml.MappingNode:
var contentTypes *orderedmap.OrderedMap[string, yaml.Node]
if err := responseBody.Decode(&contentTypes); err != nil {
return fmt.Errorf("failed to decode response body: %w", err)
}

if contentTypes.Len() > 1 {
return fmt.Errorf("test %s has more than one response body", name)
}
default:
return fmt.Errorf("test %s has invalid response body", name)
}
}
}
Expand All @@ -105,3 +122,25 @@ func (t Tests) Validate() error {

return nil
}

func (t Test) GetResponse(statusCode string) (*orderedmap.OrderedMap[string, yaml.Node], bool, error) {
if t.Responses == nil {
return nil, false, nil
}
resNode, ok := t.Responses.Get(statusCode)
if !ok {
return nil, false, nil
}

var res *orderedmap.OrderedMap[string, yaml.Node]
if err := resNode.Decode(&res); err == nil {
return res, false, nil
}

var assertStatusCode bool
if err := resNode.Decode(&assertStatusCode); err != nil {
return nil, false, fmt.Errorf("failed to decode response body: %w", err)
}

return nil, assertStatusCode, nil
}
Loading

0 comments on commit 8ebafea

Please sign in to comment.