Skip to content
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
2 changes: 1 addition & 1 deletion tools/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
golang 1.23.0
golang 1.23.6
2 changes: 1 addition & 1 deletion tools/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/mongodb/openapi/tools/cli

go 1.23

toolchain go1.23.1
toolchain go1.23.6

require (
github.com/getkin/kin-openapi v0.128.0
Expand Down
2 changes: 1 addition & 1 deletion tools/cli/internal/cli/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (o *Opts) versionsAsBytes(versions []string) ([]byte, error) {
}

func (o *Opts) PreRunE(_ []string) error {
o.stabilityLevel = strings.ToUpper(o.stabilityLevel)
o.stabilityLevel = strings.ToLower(o.stabilityLevel)
if o.stabilityLevel != "" && o.stabilityLevel != apiversion.PreviewStabilityLevel && o.stabilityLevel != apiversion.StableStabilityLevel {
return fmt.Errorf("stability level must be %q or %q, got %q", apiversion.PreviewStabilityLevel, apiversion.StableStabilityLevel, o.stabilityLevel)
}
Expand Down
113 changes: 108 additions & 5 deletions tools/cli/internal/cli/versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestVersion_RunWithEnv(t *testing.T) {
func TestVersion_RunWithPreview(t *testing.T) {
fs := afero.NewMemMapFs()
opts := &Opts{
basePath: "../../../test/data/base_spec_with_preview.json",
basePath: "../../../test/data/base_spec_with_private_preview.json",
outputPath: "foas.json",
fs: fs,
env: "staging",
Expand All @@ -75,10 +75,10 @@ func TestVersion_RunWithPreview(t *testing.T) {
assert.Contains(t, string(b), "preview")
}

func TestVersion_RunStabilityLevelPreview(t *testing.T) {
func TestVersion_RunStabilityLevelPreviewAndPrivatePreview(t *testing.T) {
fs := afero.NewMemMapFs()
opts := &Opts{
basePath: "../../../test/data/base_spec_with_preview.json",
basePath: "../../../test/data/base_spec_with_private_preview.json",
outputPath: "foas.json",
fs: fs,
env: "staging",
Expand All @@ -92,13 +92,33 @@ func TestVersion_RunStabilityLevelPreview(t *testing.T) {
// Check initial versions
assert.NotEmpty(t, b)
assert.NotContains(t, string(b), "2023-02-01")
assert.Contains(t, string(b), "private-preview")
}

func TestVersion_PreviewAndPublicPreview(t *testing.T) {
fs := afero.NewMemMapFs()
opts := &Opts{
basePath: "../../../test/data/base_spec_with_public_preview.json",
outputPath: "foas.json",
fs: fs,
env: "staging",
stabilityLevel: "PREVIEW",
}

require.NoError(t, opts.Run())
b, err := afero.ReadFile(fs, opts.outputPath)
require.NoError(t, err)

// Check initial versions
assert.NotEmpty(t, b)
assert.NotContains(t, string(b), "private-preview")
assert.Contains(t, string(b), "preview")
}

func TestVersion_RunStabilityLevelStable(t *testing.T) {
fs := afero.NewMemMapFs()
opts := &Opts{
basePath: "../../../test/data/base_spec_with_preview.json",
basePath: "../../../test/data/base_spec_with_private_preview.json",
outputPath: "foas.json",
fs: fs,
env: "staging",
Expand All @@ -112,5 +132,88 @@ func TestVersion_RunStabilityLevelStable(t *testing.T) {
// Check initial versions
assert.NotEmpty(t, b)
assert.Contains(t, string(b), "2023-02-01")
assert.NotContains(t, string(b), "preview")
assert.NotContains(t, string(b), "private-preview")
}

func TestVersion_PreRun(t *testing.T) {
t.Run("NoBasePath", func(t *testing.T) {
opts := &Opts{}
err := opts.PreRunE(nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "no OAS detected")
})

t.Run("InvalidOutputPath", func(t *testing.T) {
opts := &Opts{
basePath: "base",
outputPath: "output",
}
err := opts.PreRunE(nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "output file must be either a JSON or YAML file")
})

t.Run("InvalidFormat", func(t *testing.T) {
opts := &Opts{
basePath: "base",
outputPath: "output.json",
format: "invalid",
}
err := opts.PreRunE(nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "output format must be either 'json' or 'yaml'")
})

t.Run("ValidFormat", func(t *testing.T) {
opts := &Opts{
basePath: "base",
outputPath: "output.json",
format: "json",
}
err := opts.PreRunE(nil)
require.NoError(t, err)
})

t.Run("ValidFormatYAML", func(t *testing.T) {
opts := &Opts{
basePath: "base",
outputPath: "output.yaml",
format: "yaml",
}
err := opts.PreRunE(nil)
require.NoError(t, err)
})

t.Run("InvalidStabilityLevel", func(t *testing.T) {
opts := &Opts{
basePath: "base",
outputPath: "output.yaml",
format: "yaml",
stabilityLevel: "invalid",
}
err := opts.PreRunE(nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "stability level must be")
})
t.Run("ValidStabilityLevelPreview", func(t *testing.T) {
opts := &Opts{
basePath: "base",
outputPath: "output.yaml",
format: "yaml",
stabilityLevel: "preview",
}
err := opts.PreRunE(nil)
require.NoError(t, err)
})

t.Run("ValidStabilityLevelPreviewUppercase", func(t *testing.T) {
opts := &Opts{
basePath: "base",
outputPath: "output.yaml",
format: "yaml",
stabilityLevel: "PREVIEW",
}
err := opts.PreRunE(nil)
require.NoError(t, err)
})
}
6 changes: 5 additions & 1 deletion tools/cli/internal/openapi/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func extractVersions(oas *openapi3.T) ([]string, error) {
version, err = getPreviewVersionName(contentTypeValue)
if err != nil {
fmt.Printf("failed to parse preview version name: %v\n", err)
continue
return nil, err
}
}

Expand Down Expand Up @@ -137,6 +137,10 @@ func parsePreviewExtensionData(contentTypeValue *openapi3.MediaType) (public boo
name = nameV
}

if nameV == "" && publicV != "true" && publicV != "false" {
return false, "", errors.New("invalid value for 'public' field, only 'true' or 'false' are allowed")
}

return public, name, nil
}

Expand Down
16 changes: 16 additions & 0 deletions tools/cli/internal/openapi/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ func TestVersions_PublicPreview(t *testing.T) {
assert.Equal(t, []string{"2023-01-01", "2023-02-01", "preview"}, versions)
}

func TestVersions_InvalidPreviewData(t *testing.T) {
r := NewVersionedResponses(t)
// override the extension so something invalid like "public": true
r.Paths.Find("pathBase4").Post.Responses.Map()["200"].Value.
Content.Get("application/vnd.atlas.preview+json").
Extensions["x-xgen-preview"] = map[string]any{
"public": true,
}

_, err := ExtractVersionsWithEnv(r, "qa")
require.Error(t, err)
require.ErrorContains(t, err, "nvalid value for 'public' field")
}

func NewVersionedResponses(t *testing.T) *openapi3.T {
t.Helper()
inputPath := &openapi3.Paths{}
Expand Down Expand Up @@ -95,6 +109,7 @@ func NewVersionedResponses(t *testing.T) *openapi3.T {
},
})

// private preview version
extensionThree := map[string]any{
"x-xgen-version": "preview",
"x-xgen-preview": map[string]any{
Expand Down Expand Up @@ -133,6 +148,7 @@ func NewVersionedResponses(t *testing.T) *openapi3.T {
},
})

// public preview version
extensionFour := map[string]any{
"x-xgen-version": "preview",
"x-xgen-preview": map[string]any{
Expand Down
Loading
Loading